hdu1254 推箱子

来源:互联网 发布:.cx域名投资 编辑:程序博客网 时间:2024/05/18 08:39

看到童年时的游戏了,嗯,推箱子。

双重搜索,第一层bfs搜索箱子可以到达的地方求最短路,第二层搜索人是否可以到达能推动箱子的地方dfs、bfs随意用个呗,

毕竟箱子如果有一面靠墙,那相反的方向那么也就不能走了,人总不能站在墙上推箱子的撒~

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;int m,n,map[10][10];int dir[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};struct node{    int bx,by,step,px,py;};//bfs判断人是否可以到达相应的地点推动箱子int judge(int ex,int ey,int bx,int by,int px,int py){    int visit[10][10];    queue<node>q;    node now,next;    memset(visit,0,sizeof(visit));    now.px = px;    now.py = py;    q.push(now);    while(!q.empty())    {        now = q.front();        if(now.px==ex&&now.py==ey)        {            return 1;        }        q.pop();        for(int i = 0;i<4;i++)        {            next.px = now.px+dir[i][0];            next.py = now.py+dir[i][1];            if(0<=next.px&&next.px<m&&0<=next.py&&next.py<n&&!visit[next.px][next.py]&&map[next.px][next.py]!=1)            {                if(next.px==bx&&next.py==by)                {                                                       if(ex!=bx||ey!=by)           //如文后倒数两个例子,当人是不可以直接穿过箱子到达箱子另一面推动箱子                    {                        visit[next.px][next.py] = 1;                        continue;                    }                }                visit[next.px][next.py] = 1;                q.push(next);            }        }    }    return 0;}int bfs(int bx,int by,int px,int py)         //对箱子进行搜索{    int visit[10][10][4];             //三维数组记录已访问过度数组,因为箱子从不同方向到达某一点的状态是不同的所以用三维数组来记录    queue<node>q;    node now,next;    memset(visit,0,sizeof(visit));    now.bx = bx;    now.by = by;    now.px = px;    now.py = py;    now.step = 0;    q.push(now);    while(!q.empty())    {        now = q.front();        if(map[now.bx][now.by]==3)        {            return now.step;        }        q.pop();        next.px = now.bx;        next.py = now.by;        next.step = now.step+1;        for(int i = 0;i<4;i++)        {            next.bx = now.bx+dir[i][0];            next.by = now.by+dir[i][1];            if(0<=next.bx&&next.bx<m&&0<=next.by&&next.by<n&&!visit[next.bx][next.by][i]&&map[next.bx][next.by]!=1)            {                int x,y;                x = now.bx-dir[i][0];                y = now.by-dir[i][1];                if(0<=x&&0<=y&&judge(x,y,now.bx,now.by,now.px,now.py))                {                    visit[next.bx][next.by][i] = 1;                    q.push(next);                }            }        }    }    return -1;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int bx,by,px,py;        scanf("%d%d",&m,&n);        for(int i = 0;i<m;i++)        {            for(int j = 0;j<n;j++)            {                scanf("%d",&map[i][j]);                if(map[i][j]==2)              //记录箱子的起始位置                {                    bx = i;                    by = j;                }                else if(map[i][j]==4)        //记录搬运工的起始位置                {                    px = i;                    py = j;                }            }        }        int ans;        ans = bfs(bx,by,px,py);        printf("%d\n",ans);    }    return 0;}/*测试数据1003 34 0 30 2 11 1 13 34 2 00 0 30 0 03 34 0 10 2 30 1 16 60 0 0 0 0 01 1 0 1 1 00 0 2 0 0 01 1 0 1 1 10 0 0 4 0 00 0 0 3 0 06 30 0 00 0 00 0 01 2 10 4 00 0 3*/


0 0