hdu 1242 Rescue
来源:互联网 发布:龙游天下丁五味知真相 编辑:程序博客网 时间:2024/06/06 05:11
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
for(int i=0; i<n; i++)
scanf("%s",map[i]);
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
区别
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
int n, m, sx, sy, judge[210][210], d[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[210][210];
struct node
{
int x, y, time;
friend bool operator < (node a, node b)
{
return a.time > b.time;
}
};
int bfs()
{
node now, next;
priority_queue<node>q;
while(!q.empty())
q.pop();
now.x = sx;
now.y = sy;
now.time = 0;
q.push(now);
judge[now.x][now.y] = 1;
while(!q.empty())
{
now = q.top();
q.pop();
if(map[now.x][now.y] == 'r') return now.time;
for(int i = 0; i < 4; i++)
{
next.x = now.x + d[i][0];
next.y = now.y + d[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !judge[next.x][next.y] && map[next.x][next.y]!='#')
{
if(map[next.x][next.y] == 'x') next.time = now.time + 2;
else
next.time = now.time + 1;
judge[next.x][next.y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
if(n==0&&m==0) break;
getchar();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
scanf("%c", &map[i][j]);
if(map[i][j] == 'a')
{
sx = i;
sy = j;
}
}
getchar();
}
memset(judge, 0, sizeof(judge));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n", f);
}
return 0;
}
- hdu 1242 Rescue
- HDU-1242-Rescue
- HDU 1242 Rescue
- HDU-1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue<java>
- hdu 1242 rescue
- HDU-1242:Rescue
- HDU 1242 Rescue
- HDU 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- hdu 1242 Rescue
- Rescue hdu 1242
- hdu-1242-Rescue
- Hdu 1242 Rescue
- JSON
- 强大的 Fresco(加载网络和本地图片)简单使用
- Nodejs测试:从0到90(理论篇)
- IOS线程的四种方法
- hdoj2504又见GCD
- hdu 1242 Rescue
- MFC处理回车窗口消失
- [汇编]8086寻址方式
- Servlet 3.0 新特性概览
- HDOJ 1997 汉诺塔VII 【递归】
- 《Java中的泛型如何使用》
- 设备像素(物理像素), css像素, 设备独立像素, 设备像素比
- Redis集群搭建详细过程整理备忘
- 被 9 整除