[关闭]
@laofang 2016-07-03T02:00:43.000000Z 字数 736 阅读 738

dfs

c++


来源: http://demo.netfoucs.com/qq_33171970/article/category/5987785

整理代码:

  1. #include<iostream>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cstdio>
  5. #include<math.h>
  6. using namespace std;
  7. const int maxn=10;
  8. int n,m;
  9. int map[maxn][maxn];
  10. int visit[maxn][maxn];
  11. int vis[maxn];
  12. bool ans=0;
  13. int t;
  14. void dfs(int x,int y){
  15. if(ans==1)
  16. return;
  17. if(x==9){
  18. ans=1;
  19. return;
  20. }
  21. for(int j=y;j<=y+1;j++){
  22. if(vis[map[x+1][j]]==0 && visit[x+1][j]==0){
  23. vis[map[x+1][j]]=x;
  24. visit[x+1][j]=1;
  25. dfs(x+1,j);
  26. visit[x+1][j]=0;
  27. vis[map[x+1][j]]=0;
  28. }
  29. }
  30. }
  31. int main(){
  32. int count=1;
  33. cin>>t;
  34. while(t--){
  35. for(int i=1;i<=9;i++){
  36. for(int j=1;j<=i;j++){
  37. visit[i][j]=0;
  38. cin>>map[i][j];
  39. }
  40. vis[i]=0;
  41. }
  42. ans=0;
  43. vis[map[1][1]]=1;
  44. dfs(1,1);
  45. cout<<"Case "<<count++<<":"<<endl;
  46. if(ans==1)
  47. cout<<"Possible"<<endl;
  48. else
  49. cout<<"Impossible"<<endl;
  50. }
  51. return 0;
  52. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注