hdu 1429 priority_queue+bfs

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

int
n, m, t;
char
map[20][20];
int
dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
bool
hash[20][20][1100];
int
keybin[10] = {1,2,4,8,16,32,64,128,256,512};

struct
Node
{

    short
x, y, step, keystate;
    bool
key[10];
};


bool
operator < (Node a, Node b)
{

    return
a.step > b.step;
}


Node first, next;
void
init()
{

    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] == '@')
            {

                first.x = i;
                first.y = j;
            }
        }
    }

    memset(first.key, 0, sizeof(first.key));
    memset(hash, 0, sizeof(hash));
    first.step = 0;
    first.keystate = 0;
}


int
bfs()
{

    priority_queue <Node> Q;
    Q.push(first);

    while
(!Q.empty())
    {

        first = Q.top();
        Q.pop();

        if
(map[first.x][first.y] == '^')
        {

            return
first.step;
        }

        if
(first.step >= t)//*if not then get a TLE
        {
            return
-1;
        }

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

            next = first;
            next.x = first.x + dir[i][0];
            next.y = first.y + dir[i][1];
            next.step = first.step + 1;
            if
(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m
                &&
map[next.x][next.y] != '*')
            {

                if
(map[next.x][next.y] >= 'A' && map[next.x][next.y] <= 'J')
                {

                    if
(first.key[map[next.x][next.y]-'A'] &&
                        !
hash[next.x][next.y][next.keystate])
                    {

                        hash[next.x][next.y][next.keystate] = true;
                        Q.push(next);
                    }
                }

                else if
(map[next.x][next.y] >= 'a' && map[next.x][next.y] <= 'j')
                {

                    int
state = next.keystate, k;
                    k = map[next.x][next.y]-'a';
                    if
(!next.key[k])
                    {

                        state += keybin[k];
                    }

                    if
(!hash[next.x][next.y][state])
                    {

                        hash[next.x][next.y][state] = true;
                        next.key[k] = true;
                        next.keystate = state;
                        Q.push(next);
                    }
                   
                }

                else

                {

                    if
(!hash[next.x][next.y][next.keystate])
                    {

                        hash[next.x][next.y][next.keystate] = true;
                        Q.push(next);
                    }
                }
            }
        }
    }

    return
-1;
}

int
main()
{

    while
(scanf("%d %d %d", &n, &m, &t) != EOF)
    {

        init();
        int
result = bfs();
        if
(result == -1 || result >= t)
        {

            puts("-1");
        }

        else

        {

            printf("%d\n", result);
        }
    }

    return
0;
}

posted on 2009-08-03 15:02  ZAFU_VA  阅读(341)  评论(0)    收藏  举报

导航