hdu 2181暴搜

#include<stdio.h>
#include<string.h>
#define N 30
int map[N][4],total;
void dfs(int n,int count,int end,int path[N],int h[N]) {
int i;
path[count]=n;//记录
for(i=0;i<3;i++)    {
if(map[n][i]==end&&count==19) {//如果下一个点就是起始点并且是19就输出
  path[20]=end;
 printf("%d: ",++total);
      for(i=0;i<=20;i++)
printf(" %d",path[i]);
 printf("\n");
 return ;
}
if(h[map[n][i]]==0) {//如果没走过
h[n]=1;
dfs(map[n][i],count+1,end,path,h);
h[n]=0;//还原为下一个路径输出做准备
}
}
return ;
}
int main() {
int i,m,a,b,c,t,path[N],count,h[N];
for(i=1;i<=20;i++) {
scanf("%d%d%d",&a,&b,&c);
if(a>b) {
t=a;a=b;b=t;
}
if(a>c) {
t=a;a=c;c=t;
}
if(b>c) {
t=b;b=c;c=t;
}
map[i][0]=a;
map[i][1]=b;
map[i][2]=c;
}
while(scanf("%d",&m),m) {
count=0;//记录已走过的数目
total=0;//记录路径数
memset(h,0,sizeof(h));//记录已走过的路径避免重复走刚开始这里没弄总是不出答案
memset(path,0,sizeof(path));//记录路径
dfs(m,count,m,path,h);
}
return 0;
}

转载于:https://www.cnblogs.com/thefirstfeeling/p/4410925.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值