hdu_1242_bfs_或者记忆化dfs

版权声明: https://blog.csdn.net/jianbagengmu/article/details/79399075

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

Author
CHEN, Xue

Source
ZOJ Monthly, October 2003

Recommend
Eddy | We have carefully selected several similar problems for you: 1240 1016 1010 1072 1241

mean:
第一行输入 n,m;
读入n行长度为m字符串
从r出发找a,多个r一个a,#是墙,x是士兵要花2单位时间通过,.是路通过花费1单位时间
问 :r拯救a的最短路线
ans:
记忆话dfs a找寻r
优先队列版bfs ,优先时间;

#include<bits/stdc++.h>
using namespace std;
const int N=444;
int n,m,vis[N][N],mm;
char mp[N][N];
struct node
{
    int x,y,tim;
    bool operator <(const node &a)const
    {
        return tim>a.tim;
    }
};
int dis[4][2]= {0,1,0,-1,1,0,-1,0};
int bfs(int x,int y)
{
    memset(vis,0,sizeof(vis));
    vis[x][y]=1;
    node cur,nxt;
    cur.x=x,cur.y=y;
    cur.tim=0;
    priority_queue<node> q;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        if(mp[cur.x][cur.y]=='r')
        {
            return cur.tim;
        }
        for(int i=0; i<4; i++)
        {
            nxt.x=cur.x+dis[i][0];
            nxt.y=cur.y+dis[i][1];
            if(nxt.x<0||nxt.y<0||nxt.x>=n||nxt.y>=m
               ||mp[nxt.x][nxt.y]=='#'
               ||vis[nxt.x][nxt.y])
                continue;
            if(mp[nxt.x][nxt.y]=='x')
                nxt.tim=cur.tim+2;
            else nxt.tim=cur.tim+1;
            vis[nxt.x][nxt.y]=1;
            q.push(nxt);
        }
    }
    return -1;
}
/*int bfs(int x, int y)
{
    node point, newpoint;
    priority_queue<node>q; //定义优先队列
    memset(vis, 0, sizeof(vis));
    point.x = x;
    point.y = y;
    point.tim = 0;
    vis[point.x][point.y] = 1;
    q.push(point);
    while (!q.empty())
    {
        point = q.top();//top优先队列的顶部元素(下一个元素 相当于普通队列的front)
        q.pop();
        if (mp[point.x][point.y] == 'r')
        {
        //  flag = 1;
            return point.tim;
        }
        for (int i = 0; i<4; i++)
        {
            newpoint.x = point.x + dis[i][0];
            newpoint.y = point.y + dis[i][1];
            if (newpoint.x < 0 ||newpoint.y <0
                ||newpoint.x>=n||newpoint.y>=m
                ||vis[newpoint.x][newpoint.y]
                ||mp[newpoint.x][newpoint.y] == '#')//符合条件的
            continue;
                vis[newpoint.x][newpoint.y] = 1;
                if (mp[newpoint.x][newpoint.y] == 'x')
                    newpoint.tim = point.tim + 2;
                else
                    newpoint.tim = point.tim + 1;
                q.push(newpoint);

        }
    }
    return -1;
}*/


int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        getchar();
        for(i=0; i<n; i++)
            gets(mp[i]);
     //    for(i=0; i<n; i++) puts(mp[i]);
        mm=-1;
        for(i=0; i<n; i++)
            for(j=0; j<m; j++)
            {
                if(mp[i][j]=='a')
                {
                    mm=bfs(i,j);
                    break;
                }
            }
        if(mm==-1)
            puts("Poor ANGEL has to stay in the prison all his life.");
        else cout<<mm<<endl;
    }
}

#include<bits/stdc++.h>
using namespace std;
const int N=444;
char mp[N][N];
int vis[N][N], n,m,mm;
int dis[4][2]={0,1,0,-1,1,0,-1,0};
void dfs(int x,int y,int s)
{
    if(mp[x][y]=='r')
    {
        if(s<mm)
            mm=s;
        return ;
    }
    int i,j;
    for(i=0;i<4;i++)
    {
        int tx=x+dis[i][0];
        int ty=y+dis[i][1];
        if(tx<0||ty<0||tx>=n||ty>=m||mp[tx][ty]=='#'||vis[tx][ty]) continue;
        vis[tx][ty]=1;
        if(mp[tx][ty]=='x') dfs(tx,ty,s+2);
        else dfs(tx,ty,s+1);
        vis[tx][ty]=0;
    }
}
int main()
{
    int i,j;
    while(cin>>n>>m)
    {
        for(i=0;i<=n;i++)
            for(j=0;j<=m;j++)
                vis[i][j]=0;
        getchar();
        for(i=0;i<n;i++)
            gets(mp[i]);
        int sx=0,sy=0;
      //  puts("1111");
       for(i=0;i<n;i++)
        for(j=0;j<m;j++)
        {
            if(mp[i][j]=='a')
            {
                sx=i,sy=j,vis[i][j]=1;
                break;
            }
        }
        mm=999999;
        dfs(sx,sy,0);
        if(mm==999999)
            puts("Poor ANGEL has to stay in the prison all his life.");
        else
            cout<<mm<<endl;
    }
}

猜你喜欢

转载自blog.csdn.net/jianbagengmu/article/details/79399075