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: 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.)
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.
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... ..#..#.# #...##.. .#...... ........解题思路:遇到敌人便将其杀掉耗费的时间值加2,设置一个数组用来保存各个点的状态即可
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
int m,n;
char ss[200][200];
int flag[200][200];
int mm[200][200];
struct lmx{
int x;
int y;
int time;
};
bool isarea(int x,int y)
{
return (x>=0&&x<m&&y>=0&&y<n);
}
int c[4]={0,1,0,-1};
int d[4]={-1,0,1,0};
int X,Y,desx,desy;
queue<lmx>q;
int bfs()
{
int i;
lmx l,jk;
l.x=X;
l.y=Y;
l.time=0;
q.push(l);
while(!q.empty())
{
lmx sm=q.front();
q.pop();
for(i=0;i<4;i++)
{
int xx=sm.x+c[i];
int yy=sm.y+d[i];
if(isarea(xx,yy)&&flag[xx][yy]==0)
{
jk.x=xx;
jk.y=yy;
jk.time=sm.time+1;
if(ss[xx][yy]=='x') jk.time++;
if(jk.time<mm[xx][yy]) mm[xx][yy]=jk.time;
flag[xx][yy]=1;
q.push(jk);
}
}
}
return mm[desx][desy];
}
int main()
{
int i,j;
while(cin>>m>>n)
{
memset(flag,0,sizeof(flag));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mm[i][j]=1000;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>ss[i][j];
if(ss[i][j]=='#') flag[i][j]=1;
if(ss[i][j]=='r') {X=i;Y=j;}
if(ss[i][j]=='a') {desx=i;desy=j;}
}
}
int temp=bfs();
if(temp==1000) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<temp<<endl;
}
return 0;
}
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
int m,n;
char ss[200][200];
int flag[200][200];
int mm[200][200];
struct lmx{
int x;
int y;
int time;
};
bool isarea(int x,int y)
{
return (x>=0&&x<m&&y>=0&&y<n);
}
int c[4]={0,1,0,-1};
int d[4]={-1,0,1,0};
int X,Y,desx,desy;
queue<lmx>q;
int bfs()
{
int i;
lmx l,jk;
l.x=X;
l.y=Y;
l.time=0;
q.push(l);
while(!q.empty())
{
lmx sm=q.front();
q.pop();
for(i=0;i<4;i++)
{
int xx=sm.x+c[i];
int yy=sm.y+d[i];
if(isarea(xx,yy)&&flag[xx][yy]==0)
{
jk.x=xx;
jk.y=yy;
jk.time=sm.time+1;
if(ss[xx][yy]=='x') jk.time++;
if(jk.time<mm[xx][yy]) mm[xx][yy]=jk.time;
flag[xx][yy]=1;
q.push(jk);
}
}
}
return mm[desx][desy];
}
int main()
{
int i,j;
while(cin>>m>>n)
{
memset(flag,0,sizeof(flag));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
mm[i][j]=1000;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cin>>ss[i][j];
if(ss[i][j]=='#') flag[i][j]=1;
if(ss[i][j]=='r') {X=i;Y=j;}
if(ss[i][j]=='a') {desx=i;desy=j;}
}
}
int temp=bfs();
if(temp==1000) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<temp<<endl;
}
return 0;
}
为了明天所以选择坚定的执着今天。