题干:

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

题目大意:

天使被困在监狱,他的朋友们想见他,监狱的地形复杂,包括路(用点标示),墙(用#标示),天使的位置(用a标示),他的朋友(用r标示),监狱里还有守卫(用x标示),他的朋友只能向左右上下四个方向走,走以不花一单位时间,若碰上守卫,消灭守卫需要额外花费一单位时间。问最少多长时间天使能见到他的朋友。

解题报告:

       注意这题,可能有多个朋友,所以需要从Angel作为起点,朋友作为判断出口,而不是让朋友来找Angel,所以需要反着跑dfs。

错误代码:

#include<bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int ans;
int ex,ey;
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
char maze[205][205];
bool vis[205][205];
bool fit(int x,int y) {
	if(x > n || x < 1 || y > m || y < 1) return false;
	return true;
}
void dfs(int x,int y,int step) {
	if(step >= ans) return ;
	if(maze[x][y] == 'r'/*x == ex && y == ey*/) {
		ans = min(ans,step);return;
	}
	for(int k = 0; k<4; k++) {
		int tx = x + nx[k];
		int ty = y + ny[k];
		if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;
		if(!fit(tx,ty)) continue;
		if(maze[tx][ty] == 'x') step++;
		vis[tx][ty]=1;
		dfs(tx,ty,step+1);
		vis[tx][ty]=0;
		//step--;
	}
}

int main()
{
	int xx,yy;
	while(~scanf("%d%d",&n,&m)) {
		ans = INT_MAX;
		for(int i = 1; i<=n; i++) {
			scanf("%s",maze[i]+1);
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=m; j++) {
				if(maze[i][j] == 'a') xx=i,yy=j;
			}
		}
		
		dfs(xx,yy,0);
		if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",ans);
	}
	
	return 0;
}

AC代码:

#include<bits/stdc++.h>

using namespace std;
const int INF = 0x3f3f3f3f;
int n,m;
int ans;
int ex,ey;
int nx[4] = {0,1,0,-1};
int ny[4] = {1,0,-1,0};
char maze[205][205];
bool vis[205][205];
bool fit(int x,int y) {
	if(x > n || x < 1 || y > m || y < 1) return false;
	return true;
}
void dfs(int x,int y,int step) {
	if(step >= ans) return ;
	if(maze[x][y] == 'r'/*x == ex && y == ey*/) {
		ans = min(ans,step);return;
	}
	for(int k = 0; k<4; k++) {
		int tx = x + nx[k];
		int ty = y + ny[k];
		if(maze[tx][ty] == '#' || vis[tx][ty] == 1) continue;
		if(!fit(tx,ty)) continue;
		vis[tx][ty]=1;
		if(maze[tx][ty] == 'x') dfs(tx,ty,step+2);
		else dfs(tx,ty,step+1);
		
		vis[tx][ty]=0;
	}
}

int main()
{
	int xx,yy;
	while(~scanf("%d%d",&n,&m)) {
		ans = INT_MAX;
		for(int i = 1; i<=n; i++) {
			scanf("%s",maze[i]+1);
		}
		for(int i = 1; i<=n; i++) {
			for(int j = 1; j<=m; j++) {
				if(maze[i][j] == 'a') xx=i,yy=j;
			}
		}
		
		dfs(xx,yy,0);
		if(ans == INT_MAX) printf("Poor ANGEL has to stay in the prison all his life.\n");
		else printf("%d\n",ans);
	}
	
	return 0;
}

 对于这个错误代码,第一次修改的时候,加上了注释掉的那句step--,然后样例就wa,然后一想 还是不对,应该是如果step++过,那才step--,不然肯定答案就不正确了啊,于是就放弃了这种step++这种方式,改成了下面AC代码中的形式。

以后可以补充一下bfs的形式、、、