hdu 1254(两个BFS) 推箱子

http://acm.hdu.edu.cn/showproblem.php?pid=1254

首先,要判断人是不是可以从4到达箱子的位置2,而且不止判断一次,因为推动箱子一步后,人的位置也会改变,所以每次移动箱子前都要判断

所以这里要用两个搜索,当每朝着一个方向移动一步箱子的时候,就需要判断 从 此刻的 人位置能不能到达箱子反方向的那个位置(人只能推箱子,

不能拉什么的)  注意人在移动的时候箱子对于人来说也算障碍物,这里需要开一个hash1的四维数组记录走过的位置,不然会死循环超时间

 这个记录的数组不要叫做hash啊,因为像hash,max,min什么的都是有自定义的特殊含义的名称,然后我们不用他的自定义的含义的话OJ编译器

会识别不出而报错,我就是刚开始没注意   结果CE了一面

code

  1 #include<cstdio>
  2 #include<queue>
  3 #include<cstring>
  4 using namespace std;
  5 struct node {
  6     int wx,wy;
  7 };
  8 struct point{
  9     int x,y;//记录箱子的位置
 10     int step;
 11     int renx,reny; //记录人得位置
 12 };
 13 int n,m,rx,ry,xx,xy;
 14 int hash1[10][10][10][10];
 15 int map[10][10],visit[10][10];
 16 int dx[]={1,-1,0,0};
 17 int dy[]={0,0,1,-1};
 18 int bfs1(int q,int p,int x,int y)//搜索人是否能到箱子那里
 19 {
 20     int i;
 21     queue<node>Q;
 22     node now,next;
 23     now.wx=q;now.wy=p;
 24     Q.push(now);
 25     while (!Q.empty())
 26     {
 27         now=Q.front();
 28         Q.pop();
 29         if (now.wx==x&&now.wy==y)
 30            return 1;
 31         for (i=0;i<4;i++)
 32         {
 33             next.wx=now.wx+dx[i];
 34             next.wy=now.wy+dy[i];
 35             if (next.wx<1||next.wx>n||next.wy<1||next.wy>m)continue;
 36             if (map[next.wx][next.wy]==1)continue;
 37             if (visit[next.wx][next.wy]==1)continue;
 38             visit[next.wx][next.wy]=1;
 39             Q.push(next);
 40         }
 41     }
 42     return -1;
 43 }
 44 int bfs2()
 45 {
 46     int i,q,p;
 47     queue<point>Q;
 48     point now,next;
 49     now.x=xx;now.y=xy;
 50     now.renx=rx;now.reny=ry;
 51     now.step=0;
 52     Q.push(now);
 53     while (!Q.empty())
 54     {
 55         now=Q.front();
 56         Q.pop();
 57         if (map[now.x][now.y]==3)
 58             return now.step;
 59         for (i=0;i<4;i++)
 60         {
 61             q=now.x-dx[i];
 62             p=now.y-dy[i];
 63             next.x=now.x+dx[i];
 64             next.y=now.y+dy[i];
 65             if (q<1||q>n||p<1||p>m)continue;
 66             if (map[q][p]==1)continue;
 67             if (hash1[next.x][next.y][q][p]!=0)continue;
 68             if (next.x<1||next.x>n||next.y<1||next.y>m)continue;
 69             if (map[next.x][next.y]==1)continue;
 70             memset(visit,0,sizeof(visit));
 71             visit[now.x][now.y]=1;visit[q][p]=1;
 72             if (bfs1(q,p,now.renx,now.reny)==-1)continue;
 73             next.renx=q;next.reny=p;
 74             next.renx=now.x;next.reny=now.y;
 75             hash1[next.x][next.y][q][p]=1;
 76             next.step=now.step+1;
 77             Q.push(next);
 78         }
 79     }
 80     return -1;
 81 }
 82 int main()
 83 {
 84     int t,i,j;
 85     while (~scanf("%d",&t))
 86     {
 87         while (t--)
 88         {
 89             memset(hash1,0,sizeof(hash1));
 90             scanf("%d %d",&n,&m);
 91             for (i=1;i<=n;i++)
 92             {
 93                 for (j=1;j<=m;j++)
 94                 {
 95                     scanf("%d",&map[i][j]);
 96                     if (map[i][j]==4)
 97                         rx=i,ry=j;
 98                     if (map[i][j]==2)
 99                         xx=i,xy=j;
100                 }
101             }
102             printf("%d
",bfs2());
103         }
104     }
105     return 0;
106 }
原文地址:https://www.cnblogs.com/JJCHEHEDA/p/4712701.html