概述
这题时间怎么判断最小????
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 210;
int n, m, sx, sy, vis[maxn][maxn], dir[4][2] = {{-1, 0}, {0, -1}, {0, 1}, {1, 0}};
char map[maxn][maxn];
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);
vis[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 + dir[i][0];
next.y = now.y + dir[i][1];
if(next.x >= 0 && next.x < n && next.y >= 0 && next.y < m && !vis[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;
vis[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(vis, 0, sizeof(vis));
int f = bfs();
if(f == -1)
printf("Poor ANGEL has to stay in the prison all his life.n");
else
printf("%dn", f);
}
return 0;
}
最后
以上就是开心菠萝为你收集整理的hdu1242的全部内容,希望文章能够帮你解决hdu1242所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复