【题解】【洛谷P1126】 机器人搬重物

代码 代码 1486 人阅读 | 0 人回复

<
P1126 机械人搬重物

传收门

那讲题本来出啥好道的,但细节其实比力多,被坑了很多多少次。

  • 起首输进的是格子图,需求转化成面图,具体操作是
    142605qlpqfdf9ffq65urf.jpg

  • 最坑的一个面正在于,平常写宽搜的时分,碰到出鸿沟大概不克不及会见的面时,皆是间接进进下一层轮回(continue),但正在那讲题中,因为能够走1~3步,那末当途径上呈现停滞时,则不克不及停止下一轮轮回,需求break。
代码:

  1. #include <bits/stdc++.h>
  2. #define MAX 55
  3. using namespace std;
  4. int mod(int x){
  5.         return (x+4)%4;
  6. }
  7. struct pt{
  8.         int x, y, dir, step;
  9.         pt(){}
  10.         pt(int a, int b, int c, int d):x(a), y(b), dir(c), step(d){}
  11. };
  12. const int movx[] = {0,1,0,-1}, movy[] = {1,0,-1,0};
  13. int a[MAX][MAX];
  14. bool vis[MAX][MAX][5];
  15. int n, m;
  16. pt st, ed;
  17. void bfs(){
  18.         queue<pt> q;
  19.         bool flag = false;
  20.         st.step = 0;
  21.         q.push(st);
  22.         vis[st.x][st.y][st.dir] = true;
  23.         while(!q.empty()){
  24.                 pt t = q.front();
  25.                 q.pop();
  26.                 if(t.x == ed.x && t.y == ed.y){
  27.                         cout << t.step << endl;
  28.                         flag = true;
  29.                         break;
  30.                 }
  31.                 for(int i = 1; i <= 3; i++){
  32.                         int u, v;
  33.                         u = t.x + i*movx[t.dir];
  34.                         v = t.y + i*movy[t.dir];
  35.                         if(u<=0 || u>=n || v<=0 || v>=m || a[u][v] == 1){
  36.                                 break;
  37.                         }
  38.                         if(vis[u][v][t.dir]){
  39.                                 continue;
  40.                         }
  41.                         vis[u][v][t.dir] = true;
  42.                         q.push(pt(u, v, t.dir, t.step+1));
  43.                 }
  44.                 if(!vis[t.x][t.y][mod(t.dir+1)]){
  45.                         vis[t.x][t.y][mod(t.dir+1)] = true;
  46.                         q.push(pt(t.x, t.y, mod(t.dir+1), t.step+1));
  47.                 }
  48.                 if(!vis[t.x][t.y][mod(t.dir-1)]){
  49.                         vis[t.x][t.y][mod(t.dir-1)] = true;
  50.                         q.push(pt(t.x, t.y, mod(t.dir-1), t.step+1));
  51.                 }
  52.         }
  53.         if(!flag){
  54.                 cout << -1 << endl;
  55.         }
  56. }
  57. int main()
  58. {
  59.         cin >> n >> m;
  60.         for(int i = 1; i <= n; i++){
  61.                 for(int j = 1; j <= m; j++){
  62.                         scanf("%d", &a[i][j]);
  63.                         if(a[i][j] == 1){
  64.                                 a[i-1][j-1] = a[i-1][j] = a[i][j-1] = 1;
  65.                         }
  66.                 }
  67.         }
  68.         cin >> st.x >> st.y >> ed.x >> ed.y;
  69.         char c;
  70.         cin >> c;
  71.         switch(c){
  72.                 case &#39;E&#39;:
  73.                         st.dir = 0; break;
  74.                 case &#39;S&#39;:
  75.                         st.dir = 1; break;
  76.                 case &#39;W&#39;:
  77.                         st.dir = 2; break;
  78.                 case &#39;N&#39;:
  79.                         st.dir = 3; break;
  80.         }
  81.         bfs();
  82.        
  83.         return 0;
  84. }
复造代码
 

免责声明:假如进犯了您的权益,请联络站少,我们会实时删除侵权内乱容,感谢协作!
1、本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,按照目前互联网开放的原则,我们将在不通知作者的情况下,转载文章;如果原文明确注明“禁止转载”,我们一定不会转载。如果我们转载的文章不符合作者的版权声明或者作者不想让我们转载您的文章的话,请您发送邮箱:Cdnjson@163.com提供相关证明,我们将积极配合您!
2、本网站转载文章仅为传播更多信息之目的,凡在本网站出现的信息,均仅供参考。本网站将尽力确保所提供信息的准确性及可靠性,但不保证信息的正确性和完整性,且不对因信息的不正确或遗漏导致的任何损失或损害承担责任。
3、任何透过本网站网页而链接及得到的资讯、产品及服务,本网站概不负责,亦不负任何法律责任。
4、本网站所刊发、转载的文章,其版权均归原作者所有,如其他媒体、网站或个人从本网下载使用,请在转载有关文章时务必尊重该文章的著作权,保留本网注明的“稿件来源”,并自负版权等法律责任。
回复 关闭延时

使用道具 举报

 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则