ACM-BFS之Rescue——hdu1242

Rescue
题目: http://acm.hdu.edu.cn/showproblem.php?pid=1242
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


用到优先队列的广搜,题意就是从r出发到a结束:

'.'是路,'#'是墙,'x'是卫兵,遇到.花费1s,遇到x花费2s。

求最短步数,若无法到达,输出-1。

BFS题目,这里用到了优先队列,vis数组存的是到达某一点的最短步数,而不是是否访问过这一点。

现在都有点不懂:题目中 "r" stands for each of Angel's friend. Angel's friends want to save Angel. Their task is: approach Angel.

我一直以为一张地图里的r有多个。。

事实证明好像r是只有一个。。。o(╯□╰)o啊


#include 
#include 
#include 
using namespace std;
int n,m,f_x,f_y,dis[4][2]={0,1,0,-1,1,0,-1,0};
char mapp[201][201];
int vis[201][201];
struct  Coordinate
{
    int x,y,step;
    friend bool operator<(Coordinate c1,Coordinate c2)
    {
        return c2.step=n || y>=m)  return 0;
    if(mapp[x][y]=='#')    return 0;
    return 1;
}

// bfs用优先队列来做
int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    priority_queue   q;
    Coordinate pre,lst;
    int i;

    pre.x=x;
    pre.y=y;
    pre.step=0;

    q.push(pre);

    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        if(pre.x==f_x && pre.y==f_y)    return pre.step;

        for(i=0;i<4;++i)
        {
            lst.x=pre.x+dis[i][0];
            lst.y=pre.y+dis[i][1];

            if(judge(lst.x,lst.y))
            {
                // 判断当前点是否有卫兵,有则增加步数
                if(mapp[lst.x][lst.y]=='x') lst.step=pre.step+2;
                else    lst.step=pre.step+1;
                
                // vis存到达该点最短步数
                if(vis[lst.x][lst.y]>=lst.step || vis[lst.x][lst.y]==0)
                {
                    vis[lst.x][lst.y]=lst.step;
                    q.push(lst);
                }
            }
        }
    }
    return -1;
}

int main()
{
    int i,j,s_x,s_y;
    int temp;
    while(cin>>n>>m)
    {
        for(i=0;i>mapp[i][j];
                if(mapp[i][j]=='r') {s_x=i;s_y=j;}
                if(mapp[i][j]=='a') {f_x=i;f_y=j;}
            }

        temp=bfs(s_x,s_y);

        if(temp==-1)    cout<<"Poor ANGEL has to stay in the prison all his life."<


你可能感兴趣的:(ACM-搜索,漫漫刷题路)