[关闭]
@wwwqeqeqeqe 2019-05-05T10:12:08.000000Z 字数 9441 阅读 752

搜索问题讨论(2019.5.7)

搜索


A 生日蛋糕(POJ 1190)

题目:

7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体。设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri, 高度为Hi的圆柱。当i < M时,要求Ri > Ri+1且Hi > Hi+1。由于要在蛋糕上抹奶油,为尽可能节约经费,我们希望蛋糕外表面(最下一层的下底面除外)的面积Q最小。 令Q = Sπ 请编程对给出的N和M,找出蛋糕的制作方案(适当的Ri和Hi的值),使S最小。(除Q外,以上所有数据皆为正整数) 

解题思路:

因为题目说了所有数据都是整数,所以,我们可以通过穷举法来进行操作。因为我们的蛋糕一共有m层,而且从上往下是一定递增的。那么,从第1、2、3、······、m层的最小半径和高度都是当前层数的数值。因此,我们就找到了下界。同时,因为n最大为10000,当h=1时,r最大为100,当r=1时,h最大为10000,因此我们找到了上界。我们可以通过枚举来进行查找。
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. const int INF=0x3f3f3f3f;
  8. int n,m;
  9. int ans;
  10. void dfs(int t,int sumS,int sumV,int r,int h)
  11. {
  12. if(t == 0)
  13. {
  14. if(sumV == n)
  15. ans=min(sumS,ans);
  16. return;
  17. }
  18. for(int i=r-1;i>=t;i--)
  19. {
  20. if(t == m)
  21. sumS = i*i;
  22. int mh=h-1;
  23. for(int j=mh;j>=t;j--)
  24. {
  25. dfs(t-1,sumS+i*j*2,sumV+i*i*j,i,j);
  26. }
  27. }
  28. }
  29. int main()
  30. {
  31. cin >> n >> m;
  32. ans=INF;
  33. dfs(m,0,0,100,10000);
  34. if(ans == INF)
  35. ans=0;
  36. cout << ans << endl;
  37. }
我们就通过题目大意得到了这样一个代码。但是,很显然,如果我们直接交上去,肯定会TLE,因为我们这个代码是没有进行剪枝的。那要如果进行剪枝呢?首先我们知道,我们要求的是最小的表面积,那么,当我们在计算过程中得到的表面积已经比我们得到的最小表面积大了,那么,这个答案肯定不是我们需要的那个答案,可以直接return。同时,当我们的体积已经比n大的时候,很显然我们最后的结果sumV!=n,也直接return。同时,因为2*r*h=S,r*r*h=V,那么,S=V*2/r,r越大,S越小。所以,我们把剩下的体积(n-sumV)全部只用来做成一个圆柱体,他所增加的表面积是最小的。所以,当我们把这个剩余体积全部换成表面积加上之前的表面积大于ans的话,也是直接return掉的。于是,我们得到了最后的代码。

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. using namespace std;
  7. const int INF=0x3f3f3f3f;
  8. int n,m;
  9. int ans;
  10. int mnS[50],mnV[50];
  11. void dfs(int t,int sumS,int sumV,int r,int h)
  12. {
  13. if(t == 0)
  14. {
  15. if(sumV == n)
  16. ans=min(sumS,ans);
  17. return;
  18. }
  19. if(sumS+mnS[t] > ans || sumV+mnV[t] > n)
  20. return;
  21. if(2*(n-sumV)/r+sumS >=ans)
  22. return;
  23. for(int i=r-1;i>=t;i--)
  24. {
  25. if(t == m)
  26. sumS = i*i;
  27. int mh=h-1;
  28. for(int j=mh;j>=t;j--)
  29. {
  30. dfs(t-1,sumS+i*j*2,sumV+i*i*j,i,j);
  31. }
  32. }
  33. }
  34. int main()
  35. {
  36. mnS[0]=0;
  37. mnV[0]=0;
  38. for(int i=1;i<22;i++)
  39. {
  40. mnS[i]=mnS[i-1]+i*i*2;
  41. mnV[i]=mnV[i-1]+i*i*i;
  42. }
  43. cin >> n >> m;
  44. ans=INF;
  45. dfs(m,0,0,100,10000);
  46. if(ans == INF)
  47. ans=0;
  48. cout << ans << endl;
  49. }

B Eight(POJ 1077)

题目大意:

题目给你一个3X3的格子,里面有1~8 八个数以及一个字母X,每次将X和旁边的一个数交换,问是否能够得到最后的序列使得图形满足从左往右,从上往下依次为1~8,最后一格为X。如果存在,则输出X运动的轨迹。如果没有,输出unsolvable。

解题思路:

因为一共只有9个格子,我们可以通过康拓展开获取当前状态的一个hash值,接着使用BFS对状态进行搜索,在搜索的过程中保存状态的转移过程。当我们的hash值为1的时候,那么我们就找到了我们需要的答案,return true即可。如果我们没有找到满足条件的答案,则return false。最后根据返回结果来进行答案的输出。

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn=1e6+5;
  9. const int dx[]={-1,1,0,0};
  10. const int dy[]={0,0,-1,1};
  11. const int fac[]={1,1,2,6,24,120,720,5040,40320,362880};
  12. const char d[]={'u','d','l','r'};
  13. char path[maxn];
  14. int pre[maxn];
  15. int vis[maxn];
  16. char str[55];
  17. struct node
  18. {
  19. int status;
  20. int s[15];
  21. int loc;
  22. };
  23. int cantor(int s[])
  24. {
  25. int sum=0;
  26. for(int i=0;i<9;i++)
  27. {
  28. int num=0;
  29. for(int j=i+1;j<9;j++)
  30. {
  31. if(s[j]<s[i])
  32. num++;
  33. }
  34. sum+=num*fac[8-i];
  35. }
  36. return sum+1;
  37. }
  38. bool bfs(node now)
  39. {
  40. memset(vis,0,sizeof(vis));
  41. queue<node>q;
  42. now.status=cantor(now.s);
  43. pre[now.status]=-1;
  44. vis[now.status]=1;
  45. node t;
  46. q.push(now);
  47. while(q.size())
  48. {
  49. now=q.front();
  50. q.pop();
  51. if(now.status == 1)
  52. return true;
  53. int x=now.loc/3;
  54. int y=now.loc%3;
  55. for(int i=0;i<4;i++)
  56. {
  57. int xx=x+dx[i];
  58. int yy=y+dy[i];
  59. if(xx>=0 && xx<=2 && yy>=0 && yy<=2)
  60. {
  61. t=now;
  62. t.s[x*3+y]=t.s[xx*3+yy];
  63. t.s[xx*3+yy]=9;
  64. t.status=cantor(t.s);
  65. if(!vis[t.status])
  66. {
  67. vis[t.status]=1;
  68. t.loc=xx*3+yy;
  69. pre[t.status]=now.status;
  70. path[t.status]=d[i];
  71. q.push(t);
  72. }
  73. }
  74. }
  75. }
  76. return false;
  77. }
  78. void Print(int status)
  79. {
  80. if(pre[status] == -1)
  81. return;
  82. Print(pre[status]);
  83. cout << path[status] ;
  84. }
  85. int main()
  86. {
  87. scanf("%[^\n]s",str);
  88. node now;
  89. int cnt=0;
  90. for(int i=0;str[i]!='\0';i++)
  91. {
  92. if(str[i] == ' ')
  93. continue;
  94. if(str[i] == 'x')
  95. {
  96. now.s[cnt]=9;
  97. now.loc=cnt++;
  98. }
  99. else
  100. {
  101. now.s[cnt++]=str[i]-'0';
  102. }
  103. }
  104. if(!bfs(now))
  105. {
  106. cout << "unsolvable" <<endl;
  107. }
  108. else
  109. {
  110. Print(1);
  111. cout <<endl;
  112. }
  113. return 0;
  114. }

C Robot(POJ 1376)

题目大意:

给出一张图,图中有一个机器人,给出机器人的起点和终点以及初始朝向,算出机器人从起点左上角到达终点左上角的最小花费时间。每花费一个单位时间,可以朝当前方向前进1~3格,或者向左或向右旋转一次。

解题思路:

因为题目给出的地图是按照格子给出的,但是机器人是在方格的线上进行移动的,因此,在判断当前方格是否有障碍时需要注意判断一下,另外,地图边界也不能走。其他的就和普通BFS一样做。

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. using namespace std;
  8. const int maxn=1e2+5;
  9. const int dx[]= {-1,0,1,0};
  10. const int dy[]= {0,1,0,-1};
  11. struct node
  12. {
  13. int x,y;
  14. int time;
  15. int pos;
  16. };
  17. int n,m,mp[maxn][maxn];
  18. int vis[maxn][maxn][5];
  19. int getpos(char *str)
  20. {
  21. if(!strcmp(str,"north"))
  22. return 0;
  23. if(!strcmp(str,"east"))
  24. return 1;
  25. if(!strcmp(str,"south"))
  26. return 2;
  27. return 3;
  28. }
  29. bool check(int x,int y)
  30. {
  31. if(x<1||x>=n||y<1||y>=m)
  32. return false;
  33. if(mp[x][y]||mp[x+1][y]||mp[x][y+1]||mp[x+1][y+1])
  34. return false;
  35. return true;
  36. }
  37. int main()
  38. {
  39. while(cin >> n >> m &&(n || m))
  40. {
  41. for(int i=1; i<=n; i++)
  42. {
  43. for(int j=1; j<=m; j++)
  44. {
  45. cin >> mp[i][j];
  46. }
  47. }
  48. node bg,ed;
  49. memset(vis,0,sizeof(vis));
  50. cin >> bg.x >> bg.y;
  51. cin >> ed.x >> ed.y;
  52. char op[25];
  53. cin >> op;
  54. bg.pos=getpos(op);
  55. bg.time=0;
  56. queue<node> a;
  57. a.push(bg);
  58. vis[bg.x][bg.y][bg.pos]=1;
  59. int ans=-1;
  60. if(bg.x == ed.x && bg.y == ed.y)
  61. {
  62. cout << 0 << endl;
  63. continue;
  64. }
  65. if(!check(ed.x,ed.y))
  66. {
  67. cout << -1 << endl;
  68. continue;
  69. }
  70. while(a.size())
  71. {
  72. node aa=a.front();
  73. a.pop();
  74. int xx=aa.x;
  75. int yy=aa.y;
  76. for(int i=1; i<=3; i++)
  77. {
  78. xx+=dx[aa.pos];
  79. yy+=dy[aa.pos];
  80. if(!check(xx,yy))
  81. break;
  82. if(xx == ed.x && yy==ed.y)
  83. {
  84. ans=aa.time+1;
  85. break;
  86. }
  87. if(!vis[xx][yy][aa.pos])
  88. {
  89. node ab;
  90. ab.x=xx;
  91. ab.y=yy;
  92. ab.time=aa.time+1;
  93. ab.pos=aa.pos;
  94. vis[xx][yy][ab.pos]=1;
  95. a.push(ab);
  96. }
  97. }
  98. for(int i=0; i<4; i++)
  99. {
  100. if(max(aa.pos,i)-min(aa.pos,i) == 2)
  101. continue;
  102. if(vis[aa.x][aa.y][i])
  103. continue;
  104. node ab=aa;
  105. ab.time=aa.time+1;
  106. ab.pos=i;
  107. vis[ab.x][ab.y][ab.pos]=1;
  108. a.push(ab);
  109. }
  110. if(ans!=-1)
  111. break;
  112. }
  113. cout << ans << endl;
  114. }
  115. return 0;
  116. }

D Pushing Boxes (POJ 1475)

题目大意:

题目给出一张地图,地图里面给出人的位置,箱子的位置以及箱子到达的目的地。让你设计一种方案,是我们可以推最少次数的箱子让箱子到达目的地。如果没有能将箱子推向目的地的方案,则输出Impossible.,否则,在自己单独行动的时候输出小写的方位,在推箱子的时候输出大写的方位。

解题思路:

我们优先对箱子进行BFS,每移动一次箱子,就对人进行一次BFS,找到人从当前路径走到推箱子的下一步所需要的位置的最短路径。这个位置的具体位置通过箱子对应的下一步移动的相反方向来查找,具体在箱子移动方向相反位置的那个格子内,如果相反方向是墙或者人到不了的地方,就返回false,进而这次箱子的移动无效。

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. #include<string>
  8. using namespace std;
  9. const int dx[]= {-1,0,1,0};
  10. const int dy[]= {0,1,0,-1};
  11. const char dpathB[4] = {'N', 'E', 'S', 'W'};
  12. const char dpathP[4] = {'n', 'e', 's', 'w'};
  13. int n,m;
  14. char mp[25][25];
  15. int sx, sy;
  16. int bx, by;
  17. string ans;
  18. struct person
  19. {
  20. int x,y;
  21. string path;
  22. };
  23. struct status
  24. {
  25. int px,py,bx,by;
  26. string path;
  27. };
  28. bool check(int x, int y)
  29. {
  30. if(x<0 || x>=n || y<0 || y>=m)
  31. return false;
  32. if(mp[x][y]=='#')
  33. return false;
  34. return true;
  35. }
  36. bool bfs2(int ppx, int ppy, int bbx, int bby, int kx, int ky, string &path)
  37. {
  38. int vis[25][25];
  39. memset(vis, 0, sizeof(vis));
  40. vis[ppx][ppy] = 1;
  41. vis[kx][ky] = 1;
  42. person first;
  43. first.x = ppx;
  44. first.y = ppy;
  45. first.path = "";
  46. queue<person> myPerson;
  47. myPerson.push(first);
  48. while(!myPerson.empty())
  49. {
  50. person now = myPerson.front();
  51. myPerson.pop();
  52. if(now.x==bbx && now.y==bby)
  53. {
  54. path = now.path;
  55. return true;
  56. }
  57. for(int i=0; i<4; i++)
  58. {
  59. int zx = now.x + dx[i];
  60. int zy = now.y + dy[i];
  61. if(check(zx, zy) && !vis[zx][zy])
  62. {
  63. vis[zx][zy] = 1;
  64. person next;
  65. next.x = zx;
  66. next.y = zy;
  67. next.path = now.path + dpathP[i];
  68. myPerson.push(next);
  69. }
  70. }
  71. }
  72. return false;
  73. }
  74. bool bfs()
  75. {
  76. int vis[25][25];
  77. memset(vis, 0, sizeof(vis));
  78. status first;
  79. first.bx = bx;
  80. first.by = by;
  81. first.px = sx;
  82. first.py = sy;
  83. first.path = "";
  84. vis[bx][by] = 1;
  85. queue<status> myPath;
  86. myPath.push(first);
  87. while(!myPath.empty())
  88. {
  89. status now = myPath.front();
  90. myPath.pop();
  91. for(int i=0; i<4; i++)
  92. {
  93. int bbx = now.bx + dx[i];
  94. int bby = now.by + dy[i];
  95. int tx = now.bx - dx[i];
  96. int ty = now.by - dy[i];
  97. string path = "";
  98. if(check(bbx, bby) && check(tx, ty) && !vis[bbx][bby])
  99. {
  100. if(bfs2(now.px, now.py, tx, ty, now.bx, now.by, path))
  101. {
  102. vis[bbx][bby] = 1;
  103. status next;
  104. next.bx = bbx;
  105. next.by = bby;
  106. next.px = now.bx;
  107. next.py = now.by;
  108. next.path = now.path + path + dpathB[i];
  109. if(mp[bbx][bby]=='T')
  110. {
  111. ans = next.path;
  112. return true;
  113. }
  114. myPath.push(next);
  115. }
  116. }
  117. }
  118. }
  119. return false;
  120. }
  121. int main()
  122. {
  123. int cc = 0;
  124. while(cin >> n >> m)
  125. {
  126. ans = '\0';
  127. cc++;
  128. getchar();
  129. memset(mp, 0, sizeof(mp));
  130. if(n==0 && m==0) break;
  131. for(int i=0; i<n; ++i)
  132. {
  133. scanf("%s", mp[i]);
  134. getchar();
  135. for(int j=0; j<m; j++)
  136. {
  137. if(mp[i][j]=='S')
  138. {
  139. sx = i;
  140. sy = j;
  141. }
  142. if(mp[i][j]=='B')
  143. {
  144. bx = i;
  145. by = j;
  146. }
  147. }
  148. }
  149. cout << "Maze #" << cc << endl;
  150. if(bfs())
  151. cout << ans << endl;
  152. else
  153. cout << "Impossible." << endl;
  154. cout << endl;
  155. }
  156. return 0;
  157. }

E Dearboy's Puzzle (POJ 2308)

题目大意:

题目给出一个地图,里面有ABCD和*五种字符,*表示为空,ABCD表示有四种卡片,然后对这四种卡片进行连连看消除。最后问给出的地图能否通过连连看消除所有的ABCD。

解题思路:

首先统计ABCD四种牌的张数,如果存在某种牌有奇数个,那么无解。接着对地图外层进行DFS查找,指定一张牌来想办法把这张牌消除。再通过BFS以指定的这张牌为起点,查询能够和他配对的牌。找到后,再枚举这张牌与哪一张牌相消或者当前这张牌不消。在消除的过程中,需要单独判断AB这种情况。
          BA

AC代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<queue>
  7. using namespace std;
  8. const int dx[]= {1,-1,0,0};
  9. const int dy[]= {0,0,1,-1};
  10. int mp[12][12],n,m,num[5],sum;
  11. bool flag,vis[11][11];
  12. char str[12];
  13. struct Node
  14. {
  15. int x,y,turn,d;
  16. Node() {}
  17. Node(int a,int b,int c,int dd)
  18. {
  19. x = a;
  20. y = b;
  21. turn = c;
  22. d = dd;
  23. }
  24. };
  25. void Init()
  26. {
  27. memset(mp,0,sizeof mp);
  28. memset(num,0,sizeof num);
  29. sum = flag = 0;
  30. }
  31. bool inarea(int x,int y)
  32. {
  33. if(x<1||y<1||x>n||y>m) return false;
  34. else return true;
  35. }
  36. void bfs(int x,int y,int w,Node *can,int &len)
  37. {
  38. memset(vis,0,sizeof vis);
  39. queue<Node> Q;
  40. Q.push(Node(x,y,0,-1));
  41. vis[x][y] = 1;
  42. while(!Q.empty())
  43. {
  44. Node u = Q.front();
  45. Q.pop();
  46. if(mp[u.x][u.y] == w)
  47. {
  48. can[++len].x = u.x;
  49. can[len].y = u.y;
  50. continue;
  51. }
  52. for(int i = 0; i < 4; i++)
  53. {
  54. int tx = u.x+dx[i],ty = u.y+dy[i],tturn = 0;
  55. if(!inarea(tx,ty) || (mp[tx][ty] != -1&&mp[tx][ty] != w) || vis[tx][ty]) continue;
  56. if(u.d == i||u.d == -1)
  57. tturn = u.turn;
  58. else tturn = u.turn+1;
  59. if(tturn > 2) continue;
  60. vis[tx][ty] = 1;
  61. Q.push(Node(tx,ty,tturn,i));
  62. }
  63. }
  64. }
  65. bool check()
  66. {
  67. for(int i = 1; i < n; i++)
  68. for(int j = 1; j < m; j++)
  69. {
  70. if(mp[i][j] != -1&&mp[i][j+1] != -1&&mp[i][j] != mp[i][j+1])
  71. {
  72. if(mp[i][j] == mp[i+1][j+1]&&mp[i][j+1] == mp[i+1][j]&&num[mp[i][j]] == 2&&num[mp[i][j+1]] == 2)
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. void dfs(int cur)
  79. {
  80. if(cur == 0)
  81. {
  82. flag = 1;
  83. return;
  84. }
  85. if(flag) return;
  86. if(!check()) return;
  87. for(int i = 1; i <= n; i++)
  88. for(int j = 1; j <= n; j++)
  89. {
  90. if(flag) return;
  91. if(mp[i][j] != -1)
  92. {
  93. int w = mp[i][j],len = 0;
  94. Node can[110];
  95. mp[i][j] = -1;
  96. bfs(i,j,w,can,len);
  97. for(int k = 1; k <= len; k++)
  98. {
  99. mp[can[k].x][can[k].y] = -1;
  100. num[w] -= 2;
  101. dfs(cur-2);
  102. num[w] += 2;
  103. mp[can[k].x][can[k].y] = w;
  104. }
  105. mp[i][j] = w;
  106. }
  107. }
  108. }
  109. int main()
  110. {
  111. while(scanf("%d%d",&n,&m) != EOF&&n+m)
  112. {
  113. Init();
  114. for(int i = 1; i <= n; i++)
  115. {
  116. scanf("%s",str+1);
  117. for(int j = 1; j <= m; j++)
  118. {
  119. if(str[j] == '*') mp[i][j] = -1;
  120. else mp[i][j] = str[j]-'A'+1,num[str[j]-'A'+1]++;
  121. }
  122. }
  123. sum = num[1]+num[2]+num[3]+num[4];
  124. if(num[1]%2||num[2]%2||num[3]%2||num[4]%2)
  125. {
  126. printf("no\n");
  127. continue;
  128. }
  129. dfs(sum);
  130. if(flag) printf("yes\n");
  131. else printf("no\n");
  132. }
  133. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注