HDOJ-----1242BFS

来源:互联网 发布:windows下安装hadoop 编辑:程序博客网 时间:2024/06/06 09:17

Rescue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26310    Accepted Submission(s): 9300


Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
 

Input
First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.

Process to the end of the file.
 

Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
 

Sample Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
 

Sample Output
13

有n名天使的朋友r来营救天使a,样例只给出n==1,之前一直想错,一直从r开始。

从a开始搜索r才是对的,普通的bfs,就多了一个对x使用优先队列

最后,,,,我特么居然又挂在输入上了,题目也不提个醒,多组数据我一直按照单组输入,亏死------


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;char map[210][210];int vis[210][210];int mov[4][2] = {0, 1, 0, -1, -1, 0, 1, 0};int m, n, X, Y;struct node{    int x, y, time;    friend bool operator < (node a, node b){        return a.time > b.time;    }//按时间排优先级,这个排序和sort完全相反,大于号就是从小到大取,也挺好记}r, t;bool judge(node v){    if(v.x >= n || v.y >= m || v.x < 0 || v.y < 0){        return false;    }    if(vis[v.x][v.y] || map[v.x][v.y] == '#'){        return false;    }    return true;}void bfs(){    memset(vis, 0, sizeof(vis));    priority_queue<node > q;    vis[t.x][t.y] = 1;    q.push(t);    while(!q.empty()){        t = q.top();        q.pop();        for(int i = 0; i < 4; i++){            r.x = t.x + mov[i][0];            r.y = t.y + mov[i][1];            r.time = t.time+1;            if(judge(r)){                if(map[r.x][r.y] == 'r'){                    printf("%d\n", r.time);                    return ;                }                vis[r.x][r.y] = 1;                if(map[r.x][r.y] == 'x'){                    r.time++;                }                q.push(r);            }        }    }    printf("Poor ANGEL has to stay in the prison all his life.\n");}int main(){    while(~scanf("%d%d", &n, &m)){        int o = 0;        for(int i = 0; i < n; i++){            scanf(" %s", map[i]);            if(!o){                for(int j = 0; j < m; j++){                    if(map[i][j] == 'a'){                        t.x = i;                        t.y = j;                        t.time = 0;                        o++;                        break;                    }                }            }        }        bfs();    }    return 0;}


0 0