标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2181
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2362 Accepted Submission(s): 1490
1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 #define N 25 8 int mp[N][N]; 9 bool vis[N]; 10 int fa[N]; 11 int ans[N]; 12 void dfs(int tm, int s, int sum, int &cnt) 13 { 14 if( sum==20 ){ 15 if(!mp[s][tm]) return; 16 printf("%d: ",cnt); 17 int f = s; 18 //int c = 1; 19 for(int i = 1; i< 20; i++){ 20 ans[i] = fa[f]; 21 f = fa[f]; 22 } 23 for(int i = 19; i > 0; i--){ 24 printf("%d ",ans[i]); 25 } 26 printf("%d %d\n",s,tm); 27 cnt = cnt+1; 28 return; 29 } 30 for(int i = 1; i <= 20; i++){ 31 if(!vis[i]&&mp[s][i]){ 32 // puts("haha"); 33 vis[i] = 1; 34 fa[i] = s; 35 dfs(tm,i,sum+1,cnt); 36 vis[i] = 0; 37 } 38 } 39 return; 40 } 41 int main() 42 { 43 memset(mp,0,sizeof(mp)); 44 for(int i = 1; i <= 20; i++) 45 { 46 int x, y, z; 47 scanf("%d%d%d",&x,&y,&z); 48 mp[i][x] = mp[x][i] = 1; 49 mp[i][y] = mp[y][i] = 1; 50 mp[i][z] = mp[z][i] = 1; 51 } 52 int s; 53 while(~scanf("%d",&s)) 54 { 55 if(s==0) return 0; 56 memset(vis,0,sizeof(vis)); 57 memset(fa,0,sizeof(fa)); 58 memset(ans,0,sizeof(ans)); 59 int cnt = 1; 60 vis[s] = 1; 61 fa[s] = s; 62 dfs(s,s,1,cnt); 63 } 64 return 0; 65 }
标签:
原文地址:http://www.cnblogs.com/shanyr/p/5209056.html