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.....#..#.##...##...#..............
Sample Output
13
题意概况:给你一个n*m的图,a代表天使的位置,r代表天使的朋友的位置,x代表士兵,遇到士兵需要花费1个时间去打败他,天使的朋友要去救天使,问最快需要多少时间找到天使?
解题思路:
BFS:广搜+优先队列,做题之前把优先队列的内容看一下。
DFS:这个严格来说还是有问题的,HDU能交过,ZOJ1649超时,因为天使有多个朋友,所以应该让天使去找朋友,找到一个朋友记录需要的时间,在找到下一个朋友的时候比较需要时间选择短的,看最后所用的最短时间。因为有士兵的存在,在遇到士兵的用时加1。其他就是DFS模板。
代码:
BFS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct edge
{
int x,y,t;
friend bool operator < (edge a,edge b)
{
return a.t>b.t;
}
};
char str[250][250];
int book[250][250];
int m,n,min;
int nex[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int BFS(edge start)
{
int i;
priority_queue<edge>p;
edge now,next;
p.push(start);
while(p.empty()!=1)
{
now=p.top();
p.pop();
//printf("%d\n",now.t);
for(i=0;i<4;i++)
{
next.x=now.x+nex[i][0];
next.y=now.y+nex[i][1];
if(str[next.x][next.y]=='r')
{
next.t=now.t+1;
return next.t;
}
if(next.x>=m||next.x<0||next.y>=n||next.y<0)
continue;
if(str[next.x][next.y]!='#'&&book[next.x][next.y]==0)
{
book[next.x][next.y]=1;
if(str[next.x][next.y]=='.')
{
next.t=now.t+1;
p.push(next);
}
if(str[next.x][next.y]=='x')
{
next.t=now.t+2;
p.push(next);
}
}
}
}
return -1;
}
int main()
{
int i,j,f;
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(str,0,sizeof(str));
for(i=0;i<m;i++)
{
scanf("\n");
scanf("%s",str[i]);
}
f=0;
edge w;
memset(book,0,sizeof(book));
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(str[i][j]=='a')
{
w.t=0;
w.x=i;
w.y=j;
book[i][j]=1;
f=BFS(w);
break;
}
}
}
if(f==-1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",f);
}
return 0;
}
DFS
#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 300
int m,n;
char map[N][N];
int book[N][N];
int max;
int next[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
void DFS(int x,int y,int t)
{
int i,j,k;
if(map[x][y]=='r')
{
if(t<max)
max=t;
return ;
}
if(map[x][y]=='x')
{
t++;
}
for(i=0;i<4;i++)
{
int tx=x+next[i][0];
int ty=y+next[i][1];
if(tx>m||tx<1||ty>n||ty<1)
continue;
// printf("%d %d \n",tx,ty);
if(map[tx][ty]!='#'&&book[tx][ty]==0)
{
book[tx][ty]=1;
DFS(tx,ty,t+1);
book[tx][ty]=0;
}
}
return ;
}
int main()
{
int i,j,u,v;
while(scanf("%d%d",&m,&n)!=EOF)
{
max=99999999;
for(i=1;i<=m;i++)
{
scanf("\n");
for(j=1;j<=n;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='a')
{
u=i;v=j;
}
}
}
/*for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("%c",map[i][j]);
printf("\n");
}*/
// printf("%d %d \n",u,v);
memset(book,0,sizeof(book));
book[u][v]=1;
DFS(u,v,0);
if(max==99999999)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",max);
}
return 0;
}
BFS: