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排名而顾虑太多,知识需要掌握,个人能力有所差距
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值