hdu 1180 (bfs)

#include <iostream>
#include <queue>
using namespace std;
class Node
{
    public :
        int x, y, time;
};

Node first, next;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
int n, m, a, b;
char map[21][21];
bool hash[21][21];
queue <Node> Q;

int bfs()
{
    int fx, fy;
    first.x = a;
    first.y = b;
    first.time = 0;
    Q.push(first);
    while (!Q.empty())
    {
        first = Q.front();
        Q.pop();

        if (map[first.x][first.y] == 'T')
        {
            return first.time;
        }

        int i;
        for (i = 0; i < 4; ++i)
        {
            fx = first.x + dir[i][0];
            fy = first.y + dir[i][1];

            if (fx >= 0 && fx < n && fy >= 0 && fy < m && map[fx][fy] != '*' && !hash[fx][fy])
            {
                if (map[fx][fy] == '.' || map[fx][fy] == 'T')
                {
                    next.x = fx;
                    next.y = fy;
                    next.time = first.time + 1;
                    hash[fx][fy] = 1;
                    Q.push(next);
                }
                else if (map[fx][fy] == '-')
                {
                    if (first.time % 2 == 0)
                    {
                        if (fx == first.x)
                        {
                            fy = 2*fy - first.y;
                            if (fy >= 0 && fy < m && map[fx][fy] != '*' && !hash[fx][fy])
                            {
                                next.x = fx;
                                next.y = fy;
                                next.time = first.time + 1;
                                hash[fx][fy] = 1;
                                Q.push(next);
                            }
                        }
                        else
                        {
                            next.x = first.x;
                            next.y = first.y;
                            next.time = first.time + 1;
                            Q.push(next);
                        }
                    }
                    else
                    {
                        if (fy == first.y)
                        {
                            fx = 2*fx - first.x;
                            if (fx >= 0 && fx < n && map[fx][fy] != '*' && !hash[fx][fy])
                            {
                                next.x = fx;
                                next.y = fy;
                                next.time = first.time + 1;
                                hash[fx][fy] = 1;
                                Q.push(next);
                            }
                        }
                        else
                        {
                            next.x = first.x;
                            next.y = first.y;
                            next.time = first.time + 1;
                            Q.push(next);
                        }
                    }
                }
                else if (map[fx][fy] == '|')
                {
                    if (first.time % 2 == 0)
                    {
                        if (fy == first.y)
                        {
                            fx = 2*fx - first.x;
                            if (fx >= 0 && fx < n && map[fx][fy] != '*' && !hash[fx][fy])
                            {
                                next.x = fx;
                                next.y = fy;
                                next.time = first.time + 1;
                                hash[fx][fy] = 1;
                                Q.push(next);
                            }
                        }
                        else
                        {
                            next.x = first.x;
                            next.y = first.y;
                            next.time = first.time + 1;
                            Q.push(next);
                        }
                    }
                    else
                    {
                        if (fx == first.x)
                        {
                            fy = 2*fy - first.y;
                            if (fy >= 0 && fy < m && map[fx][fy] != '*' && !hash[fx][fy])
                            {
                                next.x = fx;
                                next.y = fy;
                                next.time = first.time + 1;
                                hash[fx][fy] = 1;
                                Q.push(next);
                            }
                        }
                        else
                        {
                            next.x = first.x;
                            next.y = first.y;
                            next.time = first.time + 1;
                            Q.push(next);
                        }
                    }
                }
            }
        }
    }
    return -1;
}


int main()
{
    while (scanf("%d %d", &n, &m) != EOF)
    {
        int i, j;
        for (i = 0; i < n; ++i)
        {
            getchar();
            for (j = 0; j < m; ++j)
            {
                scanf("%c", &map[i][j]);
                if (map[i][j] == 'S')
                {
                    a = i;
                    b = j;
                }
            }
        }
        memset(hash, 0, sizeof(hash));
        hash[a][b] = 1;
        while (!Q.empty())
        {
            Q.pop();
        }
        printf("%d"n",bfs());
    }
    return 0;
}

posted on 2009-03-31 22:33  ZAFU_VA  阅读(814)  评论(0)    收藏  举报

导航