hdu 1254 推箱子

#include <iostream>
#include <queue>
using namespace
std;

int
map[7][7];
bool
hash[7][7];
int
dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int
fuck[7][7][7][7];//记录该状态下的最短步数
int n, m;

struct
Point
{

    char
x, y;
};


struct
Node
{

    Point you, box;
    int
step;
};


Point end, you_first, box_first;
Node first, next;

void
init()
{

    scanf("%d %d", &n, &m);

    int
i, j;
    for
(i = 0; i < n; ++i)
    {

        for
(j = 0; j < m; ++j)
        {

            scanf("%d", &map[i][j]);
            if
(map[i][j] == 3)
            {

                end.x = i;
                end.y = j;
                map[i][j] = 0;
            }

            else if
(map[i][j] == 2)
            {

                box_first.x = i;
                box_first.y = j;
                map[i][j] = 0;
            }

            else if
(map[i][j] == 4)
            {

                you_first.x = i;
                you_first.y = j;
                map[i][j] = 0;
            }
        }
    }
}


bool
order_bfs(Point start, Point last)
{

    queue <Point> q;
    int
i;
    memset(hash, 0, sizeof(hash));
    Point youP, nyouP;
    youP = start;
    hash[start.x][start.y] = 1;
    q.push(youP);
    while
(!q.empty())
    {

        youP = q.front();
        q.pop();
        if
(youP.x == last.x && youP.y == last.y)
        {

            return
true;
        }

        for
(i = 0; i < 4; ++i)
        {

            nyouP.x = youP.x + dir[i][0];
            nyouP.y = youP.y + dir[i][1];

            if
(nyouP.x >= 0 && nyouP.x < n && nyouP.y >= 0 && nyouP.y < m &&
                map[nyouP.x][nyouP.y] == 0 && !hash[nyouP.x][nyouP.y])
            {

                if
(nyouP.x == first.box.x && nyouP.y == first.box.y)//不能走在原箱子处
                {
                    continue
;
                }

                else

                {

                    hash[nyouP.x][nyouP.y] = 1;
                    q.push(nyouP);
                }
            }
        }
    }

    return
false;
}


int
bfs()
{

    queue <Node> Q;
    memset(fuck, 4, sizeof(fuck));
    int
i;
    first.box = box_first;
    first.you = you_first;
    first.step = 0;
    fuck[first.box.x][first.box.y][first.you.x][first.you.y] = 0;
    Q.push(first);
    while
(!Q.empty())
    {

        first = Q.front();
        Q.pop();
        if
(first.box.x == end.x && first.box.y == end.y)
        {

            return
first.step;
        }

        for
(i = 0; i < 4; ++i)
        {

            next.box.x = first.box.x + dir[i][0];
            next.box.y = first.box.y + dir[i][1];
            next.you.x = first.box.x - dir[i][0];
            next.you.y = first.box.y - dir[i][1];

            if
(next.box.x >= 0 && next.box.x < n && next.box.y >= 0 && next.box.y < m
                &&
next.you.x >= 0 && next.you.x < n && next.you.y >= 0 && next.you.y < m
                &&
map[next.box.x][next.box.y] == 0 && map[next.you.x][next.you.y] == 0)
            {

                if
(order_bfs(first.you, next.you))//判断是否可达
                {
                    next.step = first.step + 1;
                    next.you = first.box;
                    if
(next.step < fuck[next.box.x][next.box.y][next.you.x][next.you.y])
                    {

                        fuck[next.box.x][next.box.y][next.you.x][next.you.y] = next.step;
                        Q.push(next);
                    }
                }
            }
        }
    }

    return
-1;
}



int
main()
{

    int
T;
    scanf("%d", &T);
    while
(T--)
    {

        init();
        printf("%d\n", bfs());
    }

    return
0;
}

posted on 2009-08-03 10:01  ZAFU_VA  阅读(416)  评论(0)    收藏  举报

导航