hdu 1242 Rescue

来源:互联网 发布:龙游天下丁五味知真相 编辑:程序博客网 时间:2024/06/06 05:11
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;

int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
    int x, y, time;
        friend  bool operator < (node a, node b)
    {
        return a.time > b.time;
    }
};
int bfs()
{
    node now, next;
    priority_queue<node>q;
    while(!q.empty())
        q.pop();
    now.x = sx;
    now.y = sy;
    now.time = 0;
    q.push(now);
    judge[now.x][now.y] = 1;
    while(!q.empty())
    {
        now = q.top();
        q.pop();
        if(map[now.x][now.y] == 'r') return now.time;
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + d[i][0];
            next.y = now.y + d[i][1];
            if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
            {
                if(map[next.x][next.y] == 'x') next.time = now.time + 2;
                else
                    next.time = now.time + 1;
                judge[next.x][next.y] = 1;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        if(n==0&&m==0) break;
        for(int i=0; i<n; i++)
            scanf("%s",map[i]);
        
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                if(map[i][j] == 'a')
                {
                    sx = i;
                    sy = j;
                }
            }
        }

        memset(judge, 0, sizeof(judge));
        int f = bfs();
        if(f == -1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",  f);
    }
    return 0;

}

区别

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;

int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
    int x, y, time;
        friend  bool operator < (node a, node b)
    {
        return a.time > b.time;
    }
};
int bfs()
{
    node now, next;
    priority_queue<node>q;
    while(!q.empty())
        q.pop();
    now.x = sx;
    now.y = sy;
    now.time = 0;
    q.push(now);
    judge[now.x][now.y] = 1;
    while(!q.empty())
    {
        now = q.top();
        q.pop();
        if(map[now.x][now.y] == 'r') return now.time;
        for(int i = 0; i < 4; i++)
        {
            next.x = now.x + d[i][0];
            next.y = now.y + d[i][1];
            if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
            {
                if(map[next.x][next.y] == 'x') next.time = now.time + 2;
                else
                    next.time = now.time + 1;
                judge[next.x][next.y] = 1;
                q.push(next);
            }
        }
    }
    return -1;
}

int main()
{
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        if(n==0&&m==0) break;
        getchar();
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &map[i][j]);
                if(map[i][j] == 'a')
                {
                    sx = i;
                    sy = j;
                }
            }
            getchar();
        }

        memset(judge, 0, sizeof(judge));
        int f = bfs();
        if(f == -1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",  f);
    }
    return 0;
}


0 0
原创粉丝点击