SDAU 搜索专题 12 Rescue

来源:互联网 发布:视频修改软件手机软件 编辑:程序博客网 时间:2024/06/05 11:23

1:问题描述
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

2:大致题意

天使被魔鬼抓起来了,现在天使的朋友要去救天使,#是栏杆过不去。a是天使,r是天使的朋友,x是守卫,点点是路。走一个点点需要1min。如果有守卫就需要花1min解决掉守卫。要计算最短时间。

3:思路

这个是挺简单的一个BFS,也是4个方向,一个一个遍历,直到找到天使。

4:感想

这个算是BFS的入门题吧。。
好困,( ̄o ̄) . z Z。。。
再写几个睡觉觉。

5:ac代码

#include<iostream>#include<string.h>#include<set>#include<stdio.h>#include<vector>#include<algorithm>#include<numeric>#include<math.h>#include<string.h>#include<sstream>#include<stdio.h>#include<string>#include<cstdlib>#include<algorithm>#include<iostream>#include<queue>#include<iomanip>#include<cstdio>using namespace std;struct ans{    int x,y;    int t;    friend bool operator<(ans n1,ans n2)    {        return n1.t>n2.t;    }}n1,n2;char map[300][300];int a[300][300],n,m;int dix[10]={0,0,-1,1};int diy[10]={1,-1,0,0};int BFS(int ax,int ay, int rx,int ry){    int i;    priority_queue<ans> Q;    n1.x=rx;    n1.y=ry;    n1.t=0;    Q.push(n1);    while(!Q.empty())    {        n1=Q.top();        Q.pop();        if(n1.x==ax&&n1.y==ay) return n1.t;        for(i=0;i<4;i++)        {            n2.x=n1.x+dix[i];            n2.y=n1.y+diy[i];            if(map[n2.x][n2.y]=='x')            {                n2.t=n1.t+2;            }            else            {                n2.t=n1.t+1;            }            if(map[n2.x][n2.y]!='#'&&n2.x>0&&n2.x<=n&&n2.y>0&&n2.y<=m)            {                map[n2.x][n2.y]='#';                Q.push(n2);            }        }    }     return -1;}int main(){    char x;    //freopen("r.txt","r",stdin);    int i,k,z,j;    while(cin>>n>>m)    {        x=getchar();        int rx,ry,ax,ay;        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {                cin>>map[i][j];                if(map[i][j]=='a') {ax=i;ay=j;}                if(map[i][j]=='r') {rx=i;ry=j;}            }        }        z=BFS(ax,ay,rx,ry);        if(z==-1) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;        else cout<<z<<endl;    }}
0 0
原创粉丝点击