Problem Description
Angel was caught by theMOLIGPY! 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 theprison.<br><br>Angel's friends want to save Angel. Their task is:approach Angel. We assume that "approach Angel" is to get tothe position where Angel stays. When there's a guard inthe grid, we must kill him (or her?) to move intothe grid. We assume that we moving up, down, right, lefttakes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.<br><br>You have to calculate the minimal time to approach Angel. (Wecan move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, ofcourse.)<br>
Input
First line contains two integers stand for N and M.<br><br>Then N lines follows, every line has Mcharacters. "." stands for road, "a" stands for Angel, and "r" stands for each ofAngel's friend. <br><br>Processto the end of the file.<br>
Output
For each test case, yourprogram 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 prisonall his life." <br>
Sample Input
7 8
# . # # # # # .
# . a # . . r .
# . . # x . . .
. . # . . # . #
# . . . # # . .
. # . . . . . .
. . . . . . . .
Sample Output13
解题思路:
考虑到1-朋友不止一个,小胖子(angel)只有一个,
2- 守卫也是一条道路,但通过花费的时间要多,
一开始试图用广搜却不知道那个多一个时间点的守卫怎么处理,看到广搜大部分都和优先队列或者什么剪枝联系在一起,就弱弱的换回了深搜,又没有考虑到小胖子人缘挺好,是friendes,非常迅速的卒了,
bug..s :可以从我的注释里面看到我的各种蠢,就不一一赘述了(捂脸。
AC.1
//朋友不只一个,so..倒着搜
//输入字符串不用&
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<math.h>
using namespace std;
int n,m,starx,stary,len,minl;
//dx[]={-1,1,0,0},dy[]={0,0,-1,1};换一种枚举方法就用不到它了
bool mark[201][201];
char a[201][201];
void search(int x,int y,int len)
{
//cout<<"x="<<x<<" y="<<y<<" len="<<len<<endl;
if(x<0||x>=n||y<0||y>=m||mark[x][y]==1)
return ;//越界或已经走过
if(len>=minl||a[x][y]=='#')
return;//比原来的最小路径长或怼墙了
if(a[x][y]=='r')//达到一个终点,比较时长
{
if(len<minl)minl=len;
return;
}
if(a[x][y]=='x')//杀掉一个人需要时间加一
{
++len;
}
mark[x][y]=1;//标记
search(x+1,y,len+1);//枚举四个方向
search(x-1,y,len+1);
search(x,y+1,len+1);
search(x,y-1,len+1);
mark[x][y]=0;//回溯
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(mark,0,sizeof(mark));//清零标记数组
for(int i=0;i<n;i++)
{
scanf("%s",a[i]);
for(int j=0;j<m;j++)
{
if(a[i][j]=='a')//'a'代表天使,搜索起点,花费一秒
{starx=i;stary=j;}//此处不要将mark[x][y]标记
}
}
len=0;
minl=80000;//最多80000步吗?不明白大佬的INT_MAX有什么好处?
search(starx,stary,len);
if(minl<80000){cout<<minl<<endl;}
else cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
}
return 0;
}
2660

被折叠的 条评论
为什么被折叠?



