HDU 1242 Rescue

#include<stdio.h>
#include<string.h>
#include<queue>

using namespace std;

const int maxn = 1e3 + 5;
const int INF = 0x3f3f3f3f;

char mp[maxn][maxn];
int vis[maxn][maxn];

int dx[10] = {0,0,-1,1,1,1,-1,-1};
int dy[10] = {1,-1,0,0,-1,1,-1,1};
int n,m;

struct node{
	int x,y;
	int step;
	node(int _x, int _y, int _step = 0): x(_x), y(_y),step(_step){
	}
	bool operator < (const node & i)const{
		return step > i.step;
	}
};

int main (){

	while (scanf("%d%d", &n, &m) != EOF){		
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				mp[i][j] = INF;
			}
		}
		
		for (int i = 0; i < n; i++){
				scanf("%s",mp[i]);
		}
		
		int sx,sy;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < m; j++){
				if (mp[i][j] == 'a'){
					sx = i;
					sy = j;
				}
			}
		}
		priority_queue<node> que;
		que.push(node(sx,sy));
		memset(vis,0,sizeof(vis));
		vis[sx][sy] = 1;
		
		int ans = 0;
		
		while (!que.empty() ){
			node tem = que.top() ;
			que.pop() ;
			
			if (mp[tem.x ][tem.y ] == 'r'){
				ans = tem.step ;
				break;
			}
			
			for (int i = 0; i < 4; i++){
				int x = tem.x + dx[i];
				int y = tem.y + dy[i];
				
				if (x < 0 || y < 0 || x >= n || y >= m) continue;
				else if (vis[x][y])	continue;
				else if (mp[x][y] == '.' || mp[x][y] == 'r') { // 开始忘了在这判断r,结果总是出错
					que.push(node(x,y,tem.step + 1)); 
					vis[x][y] = 1;
					continue;
				}
				else if (mp[x][y] == '#')	continue; //#不允许通过,题上好像没说
				else  {
					que.push(node(x,y,tem.step + 2)); 
					vis[x][y] = 1;
				}
			}
		}
		if (!que.empty() ){
			printf("%d\n",ans);
		}
		else printf("Poor ANGEL has to stay in the prison all his life.\n");
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值