Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3638 Accepted Submission(s): 1432
1 2 3 4 1 1 2 3 0 1 2 3 0 0 0 0
1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3124 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 1123 1132 1213 1231 1312 1321 2113 2131 2311 3112 3121 3211 1023 1032 1203 1230 1302 1320 2013 2031 2103 2130 2301 2310 3012 3021 3102 3120 3201 3210
简单DFS, (┬_┬)某渣dfs一直不会写,每次搜索题都用bfs,这次终于写出 一次简单的dfs了(不过也幸亏有月神的指点啊!!)
#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> #include<limits.h> using namespace std; int hash[5]; int a[5]; int sum; int one,two,three,four; struct node { int b[5]; }list[11111]; int hash2[11111]; void dfs(int depth) { int be= 1; if(depth==1&&a[1]==0) be= 2; for(int i= be; i<= 4; i++) if(hash[i]==0) { if(depth==1) one= i; if(depth==2) two= i; if(depth==3) three= i; if(depth==4) { four= i; sum++; int num; num= a[one] * 1000+ a[two] * 100 + a[three] * 10 + a[four]; hash2[num]= 1; } hash[i]= 1; dfs(depth+1); hash[i]= 0; //如果这里没有把下一步的hash标记值初始化,那么搜索一次以后搜索就停止了 // 例如输入1,2,3,4 当depth= 4; i=4 的时候 hash表中的值就全为1了 } } int main() { int T=0; while(scanf("%d %d %d %d",&a[1],&a[2],&a[3],&a[4])!=EOF&&a[1]+a[2]+a[3]+a[4]) { T++; if(T!=1) printf("\n"); sum=0; memset(hash,0,sizeof(hash)); memset(hash2,0,sizeof(hash2)); sort(a+1,a+5); dfs(1); for(int i= 1; i<= 9; i++) { int flag= 0; for(int j= 1000*i; j<= 1000*i+ 999; j++) { if(hash2[j]) { if(flag==0) { printf("%d",j); flag= 1; } else printf(" %d",j); } } if(flag) printf("\n"); } } return 0; }