poj1321+poj2251

心已赠人 2022-05-30 04:08 249阅读 0赞

poj1321代码实现(dfs):

  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. int n,k;
  5. char str[10][10];
  6. int flag[10];
  7. int sum;
  8. void dfs(int i)
  9. {
  10. if(i>n)
  11. return;
  12. int temp=0;
  13. //cout<<"flag"<<endl;
  14. for(int t=0; t<n; t++)
  15. {
  16. //cout<<flag[t]<<endl;
  17. temp+=flag[t];
  18. }
  19. if(temp==k)
  20. {
  21. sum++;
  22. //cout<<"sum== "<<sum<<endl;
  23. return;
  24. }
  25. for(int j=0; j<n; j++)
  26. {
  27. if(str[i][j]=='#' && flag[j]==0)
  28. {
  29. flag[j]=1;
  30. dfs(i+1);
  31. flag[j]=0;
  32. }
  33. }
  34. dfs(i+1);
  35. }
  36. int main()
  37. {
  38. while(cin>>n>>k)
  39. {
  40. if(n==-1 && k==-1)
  41. {
  42. break;
  43. }
  44. memset(str,0,sizeof(str));
  45. memset(flag,0,sizeof(flag));
  46. for(int i=0; i<n; i++)
  47. {
  48. cin>>str[i];
  49. }
  50. /*for(int i=0;i<n;i++)
  51. {
  52. cout<<str[i]<<endl;
  53. }*/
  54. sum=0;
  55. dfs(0);
  56. cout<<sum<<endl;
  57. }
  58. return 0;
  59. }

poj2251(bfs):

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5. using namespace std;
  6. const int maxn=35;
  7. const int INF=1000000;
  8. int L,R,C;
  9. char str[maxn][maxn][maxn];
  10. int x[6]= { 0, 0, 0, 0, 1,-1};
  11. int y[6]= { 0, 0, 1,-1, 0, 0};
  12. int z[6]= {-1, 1, 0, 0, 0, 0};
  13. struct node
  14. {
  15. int i,j,k;
  16. int dis;
  17. };
  18. queue<node>que;
  19. int bfs(node a)
  20. {
  21. que.push(a);
  22. str[a.i][a.j][a.k]='#';
  23. while(!que.empty())
  24. {
  25. node ah=que.front();
  26. que.pop();
  27. if(str[ah.i][ah.j][ah.k]=='E')
  28. {
  29. return ah.dis;
  30. }
  31. node aa1;
  32. for(int t=0; t<6; t++)
  33. {
  34. int ih=ah.i+x[t];
  35. int jh=ah.j+y[t];
  36. int kh=ah.k+z[t];
  37. if(str[ih][jh][kh]!='#' && 0<=ih && ih<L && 0<=jh && jh<R && 0<=kh && kh<C )
  38. {
  39. if(str[ih][jh][kh]!='E')
  40. str[ih][jh][kh]='#';
  41. aa1.i=ih;
  42. aa1.j=jh;
  43. aa1.k=kh;
  44. aa1.dis=ah.dis+1;
  45. que.push(aa1);
  46. }
  47. }
  48. }
  49. return INF;
  50. }
  51. int main()
  52. {
  53. while(cin>>L>>R>>C)
  54. {
  55. if(L==0 && R==0 && C==0)
  56. {
  57. break;
  58. }
  59. memset(str,0,sizeof(str));
  60. for(int i=0; i<L; i++)
  61. {
  62. for(int j=0; j<R; j++)
  63. {
  64. cin>>str[i][j];
  65. }
  66. }
  67. while(que.size()!=0)
  68. que.pop();
  69. node a1;
  70. for(int i=0; i<L; i++)
  71. {
  72. for(int j=0; j<R; j++)
  73. {
  74. for(int k=0; k<C; k++)
  75. {
  76. if(str[i][j][k]=='S')
  77. {
  78. a1.i=i;
  79. a1.j=j;
  80. a1.k=k;
  81. a1.dis=0;
  82. }
  83. }
  84. }
  85. }
  86. int kkkk=bfs(a1);
  87. if(kkkk!=INF)
  88. {
  89. cout<<"Escaped in "<<kkkk<<" minute(s)."<<endl;
  90. }
  91. else
  92. {
  93. cout<<"Trapped!"<<endl;
  94. }
  95. }
  96. return 0;
  97. }

发表评论

表情:
评论列表 (有 0 条评论,249人围观)

还没有评论,来说两句吧...

相关阅读

    相关 poj1321-棋盘问题

    棋盘问题 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,