1. 程式人生 > >Rescue 3解法:(1.DFS 2. BFS 3.BFS+優先佇列模板)

Rescue 3解法:(1.DFS 2. BFS 3.BFS+優先佇列模板)

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

題意:'.'為道路,‘#’障礙,‘a’為天使, 'r '為朋友 然後找從朋友到天使的最短路徑 思路: 朋友不只是一個,而天使卻是一個,所以我們可以反過來尋找,天使找朋友,那麼目標狀態就是找到朋友 可以說是一個棋盤型別的變形題目,DFS與BFS均可。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
 
int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
    int x, y, step;
    bool operator <(const node & t) const
    {
        return step>t.step;
    }
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
 
void bfs() {
    node now, tmp;
    int i,xx,yy;
    priority_queue<node> q;
    now.x = sx, now.y = sy, now.step = 0;
    map[sx][sy] = '#';
    q.push(now);
    while(!q.empty()) {
        now = q.top();
        q.pop();
//         cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
        for(i=0; i<4; i++) {
            xx = now.x +dx[i];
            yy = now.y +dy[i];
            if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
            if(map[xx][yy]=='r') {
                cout<<now.step+1<<endl;
                flag = true;
                return ;
            }
            if(map[xx][yy]=='x') {
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+2;
                q.push(tmp);
            } else {
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+1;
                q.push(tmp);
            }
            map[xx][yy] = '#';
        }
    }
}
 
int main() {
    int i, j;
    while(~scanf("%d%d",&n,&m)) {
        for(i=0; i<n; i++)
            for(j=0; j<m; j++) {
                cin>>map[i][j];
                if(map[i][j]=='a')
                    sx=i,sy=j;
            }
        flag = false;
        bfs();
        if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}
BFS:把當擴充套件到“x”點時,把他放到與他耗時一樣的那一層。這樣也能保證到擴充套件到目標狀態時,耗時是最小
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
 
int n, m;
char map[205][205];
int sx, sy;
bool flag;
struct node {
    int x, y, step;
    bool operator <(const node & t) const
    {
        return step>t.step;
    }
};
int dx[]= {-1,0,0,1};
int dy[]= {0,-1,1,0};
 
void bfs() {
    node now, tmp;
    int i,xx,yy;
    priority_queue<node> q;
    now.x = sx, now.y = sy, now.step = 0;
    map[sx][sy] = '#';
    q.push(now);
    while(!q.empty()) {
        now = q.top();
        q.pop();
//         cout<<now.x<<" "<<now.y<<" "<<now.step<<endl;
        for(i=0; i<4; i++) {
            xx = now.x +dx[i];
            yy = now.y +dy[i];
            if(xx<0||xx>=n||yy<0||yy>=m||map[xx][yy]=='#') continue;
            if(map[xx][yy]=='r') {
                cout<<now.step+1<<endl;
                flag = true;
                return ;
            }
            if(map[xx][yy]=='x') {
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+2;
                q.push(tmp);
            } else {
                tmp.x =xx, tmp.y = yy, tmp.step = now.step+1;
                q.push(tmp);
            }
            map[xx][yy] = '#';
        }
    }
}
 
int main() {
    int i, j;
    while(~scanf("%d%d",&n,&m)) {
        for(i=0; i<n; i++)
            for(j=0; j<m; j++) {
                cin>>map[i][j];
                if(map[i][j]=='a')
                    sx=i,sy=j;
            }
        flag = false;
        bfs();
        if(!flag) printf("Poor ANGEL has to stay in the prison all his life.\n");
    }
    return 0;
}

BFS+優先佇列,這是效率相對最高的演算法,因為利用小根堆,每次都是讓最小的出佇列判斷,所以只要找到r即為答案
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
char map[205][205];
int vis[205][205];
int m,n;
int d[4][2]= {0,-1,0,1,-1,0,1,0};
struct node
{
    int x,y;
    int time;
    friend bool operator<(const node &a,const node &b)//運算子過載對優先佇列裡的小於號進行的過載
    {
        return a.time>b.time;//時間小的先出隊
    }
};
int bfs(int x,int y)
{
    node st,nt;
    priority_queue<node> q;
    st.x=x;
    st.y=y;
    st.time=-1;
    q.push(st);
    vis[st.x][st.y]=1;
    while(!q.empty())
    {
        st=q.top();
        q.pop();
        if(map[st.x][st.y]=='r')
            return st.time;
        for(int i=0; i<4; i++)
        {
            nt.x=st.x+d[i][0];
            nt.y=st.y+d[i][1];
            if(!vis[nt.x][nt.y] && nt.x>=0 && nt.x<m && nt.y>=0 && nt.y<n && map[nt.x][nt.y]!='#')
            {
                vis[nt.x][nt.y]=1;
                if(map[nt.x][nt.y]=='.')
                    nt.time=st.time+1;
                else
                    nt.time=st.time+2;
                q.push(nt);
            }
        }
    }
    return -1;
}
int main()
{
    int x,y,count;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(vis,0,sizeof(vis));
        count=-1;
        for(int i=0; i<m; i++)
            scanf("%s",map[i]);
        for(int i=0; i<m; i++)
            for(int j=0; j<n; j++)
                if(map[i][j]=='a')
                {
                    x=i;
                    y=j;
                    break;
                }
        count=bfs(x,y);
        if(count==-1)
            printf("Poor ANGEL has to stay in the prison all his life.\n");
        else
            printf("%d\n",count);
    }
    return 0;
}

反思:多對題目進行思考,採用時間效率較高的演算法,不要因為題目的AC排名而顧慮太多,知識需要掌握,個人能力有所差距