1242 Rescue BFS

#include<iostream>
#include<string>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,di,dj,ok,ss;
char mapp[210][210];
int vis[210][210];
//应该不止一个朋友
struct node{
    int x,y;
    int step;
    friend bool operator < (node a,node b){

        return a.step > b.step;  //升序
    }
};
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
priority_queue<node> pq;
void  bfs(){
    node temp,next;
    int s,d;
    while(!pq.empty()){
        temp=pq.top();
       // cout<<temp.floor<<" "<<temp.x<<" "<<temp.y<<" "<<temp.step<<endl;
        pq.pop();
        if(temp.x==di&&temp.y==dj)
        {

        }
       for(int i=0;i<4;i++){
        s=temp.x+dir[i][0];
        d=temp.y+dir[i][1];
        if(s==di&&d==dj){
            ok=1;
            ss=temp.step+1;
            break;}
        if(s>=0&&s<n&&d>=0&&d<m&&vis[s][d]==0&&mapp[s][d]!=‘#‘){
            if(mapp[s][d]==‘.‘){
             next.step=temp.step+1;
            }else if(mapp[s][d]==‘x‘){
             next.step=temp.step+2;
            }
            next.x=s;
            next.y=d;
            pq.push(next);
            vis[s][d]=1;
        }
       }
      if(ok) break;
    }

}
int main(){
  int si,sj;
    while(cin>>n>>m){
        memset(vis,0,sizeof(vis));
        while(!pq.empty()) pq.pop();
        ok=0;
        ss=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++){
                cin>>mapp[i][j];
                if(mapp[i][j]==‘a‘){di=i;dj=j;}
                if(mapp[i][j]==‘r‘){si=i;sj=j;}
            }
        node tt;
        tt.x=si;tt.y=sj;tt.step=0;
        vis[si][sj]=1;
        pq.push(tt);

        bfs();

        if(ss) cout<<ss<<endl;
        else  cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
    }
    return 0;
}
时间: 2024-10-20 02:33:09

1242 Rescue BFS的相关文章

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue 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:

hdoj 1242 Rescue (BFS)

Rescue http://acm.hdu.edu.cn/showproblem.php?pid=1242 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18962    Accepted Submission(s): 6771 Problem Description Angel was caught by the MOLIGPY!

hdu 1242 Rescue(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

HDU 1242 Rescue(BFS)

题目链接 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: a

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

hdu 1242:Rescue(BFS广搜 + 优先队列)

Rescue Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14   Accepted Submission(s) : 7 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Angel was caught by the MOLIGPY

HDU 1242 Rescue营救 BFS算法

题目链接:HDU 1242 Rescue营救 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16524    Accepted Submission(s): 5997 Problem Description Angel was caught by the MOLIGPY! He was put in prison by

HDU 1242 -Rescue (双向BFS)&amp;&amp;( BFS+优先队列)

题目链接:Rescue 进度落下的太多了,哎╮(╯▽╰)╭,渣渣我总是埋怨进度比别人慢...为什么不试着改变一下捏.... 开始以为是水题,想敲一下练手的,后来发现并不是一个简单的搜索题,BFS做肯定出事...后来发现题目里面也有坑 题意是从r到a的最短距离,"."相当时间单位1,"x"相当时间单位2,求最短时间 HDU 搜索课件上说,这题和HDU1010相似,刚开始并没有觉得像剪枝,就改用  双向BFS   0ms  一Y,爽! 网上查了一下,神牛们竟然用BFS+

HDUJ 1242 Rescue 搜索

Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15582    Accepted Submission(s): 5656 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is