概述
同诡异的楼梯,也是优先队列,注意这里从天使姐姐开始搜起
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
struct Node
{
int x, y;
int step;
bool operator <(const Node t)const{
return step > t.step;
}
};
const int Go[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
char map[210][210];
int n, m;
Node start;
int ans;
int check(int x, int y)
{
return (x>=0 && x<n && y>=0 &&y < m);
}
int visited[210][210];
void bfs()
{
priority_queue <Node>Que;
//while (!Que.empty())Que.pop();
Que.push(start);
memset(visited, 0, sizeof(visited));
visited[start.x][start.y] = 1;
//Node pre, next;
while (!Que.empty()){
Node pre = Que.top();
Que.pop();
for (int i = 0; i < 4; i ++){
int xx = pre.x+Go[i][0];
int yy = pre.y+Go[i][1];//cout<<"asdasd"<<endl;
if (check(xx, yy)&&map[xx][yy] != '#'&&!visited[xx][yy]){
visited[xx][yy] = 1;
if (map[xx][yy]=='r'){
ans = pre.step+1;
return ;
}
if (map[xx][yy]=='x'){
Node next;
next.x = xx, next.y = yy;
next.step = pre.step+2;
Que.push(next);
}
else {
Node next;
next.x = xx, next.y = yy;
next.step = pre.step+1;
Que.push(next);
}
//visited[next.x][next.y] = 1;
}
}
}
}
int main()
{
while (scanf("%d %d", &n, &m)!=EOF ){
for (int i = 0; i < n; i ++){
getchar();
scanf("%s", map[i]);
//cin>>map[i];
for (int j = 0; j < m; j ++){
//cin >> map[i][j];
if (map[i][j] == 'a'){
start.x = i;
start.y = j;
}
}
}
start.step = 0;
ans = -1;
bfs();
if (ans != -1){
printf("%d/n", ans);
}
else printf("Poor ANGEL has to stay in the prison all his life./n");
}
return 0;
}
最后
以上就是怕黑哈密瓜为你收集整理的hdu1242 rescue的全部内容,希望文章能够帮你解决hdu1242 rescue所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复