标签:
本题需要注意的是,天使的朋友可能不只一个,所以,应该从天使的位置开始搜去找其朋友就ok了。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <map>
#include <set>
using namespace std;
#define Maxn 500
int hang,lie;
int end_x,end_y;
int begin_x,begin_y;
int maxn;
int q = 0;
bool flag = false;
char MAP[Maxn][Maxn];
int dir[4][2] = {
{1,0},
{-1,0},
{0,1},
{0,-1}
};
void print()
{
for(int i = 0; i < hang; i++)
{
for(int j = 0; j < lie; j++)
{
printf("%c",MAP[i][j]);
}
printf("\n");
}
}
void dfs(int x,int y,int Time)
{
if (MAP[x][y] == ‘r‘)
{
flag = true;
if (Time < maxn)
{
maxn = Time;
}
}
MAP[x][y] = ‘#‘;
for(int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx >= 0 && xx < hang && yy >= 0 && yy < lie && MAP[xx][yy] != ‘#‘)
{
if (MAP[xx][yy] != ‘#‘)
{
if (MAP[xx][yy] == ‘x‘)
{
dfs(xx,yy,Time+2);
MAP[xx][yy] = ‘x‘;
}
else if(MAP[xx][yy] == ‘r‘)
{
dfs(xx,yy,Time+1);
MAP[xx][yy] = ‘r‘;
}
else
{
dfs(xx,yy,Time+1);
MAP[xx][yy] = ‘.‘;
}
}
}
}
}
int main()
{
while(cin >> hang >> lie)
{
maxn = 214748364;
flag=false;
for (int i = 0; i < hang; i++)
{
scanf("%s",MAP[i]);
}
for(int i = 0; i < hang; i++)
{
for(int j = 0; j < lie; j++)
{
// printf("%c",MAP[i][j]);
if (MAP[i][j] == ‘a‘)
{
begin_x = i;
begin_y = j;
MAP[i][j] = ‘#‘;
}
}
}
dfs(begin_x,begin_y,0);
if (flag)
{
printf("%d\n",maxn);
}
else
{
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
}
}
标签:
原文地址:http://www.cnblogs.com/yakoazz/p/5727733.html