HDU 1242

http://acm.hdu.edu.cn/showproblem.php?pid=1242


  
#include < stdio.h >
#include
< string .h >
#include
< iostream >
#include
< queue >
using namespace std;
int dir[ 4 ][ 2 ] = { 0 , 1 , 0 , - 1 , 1 , 0 , - 1 , 0 };
int si,sj; // 公主的起始横纵坐标,
int n,m;
char map[ 201 ][ 201 ];
struct node
{
int x,y;
int time;
};
priority_queue
< node > q;
bool operator < (node a,node b)
{
return a.time > b.time;
}
int out_line( int a, int b)
{
if (a >= 0 && a < n && b >= 0 && b < n) return 1 ;
else return 0 ;
}
void getmap()
{
int i,j;
memset(map,
0 , sizeof (map));
for (i = 0 ;i < n;i ++ )
for (j = 0 ;j < m;j ++ )
{
scanf(
" %1s " , & map[i][j]);
if (map[i][j] == ' a ' ) si = i,sj = j;
}
}
int bfs( int si, int sj)
{
int i,mark[ 201 ][ 201 ];
node cur,next;
while ( ! q.empty()) // 清队列
q.pop();
memset(mark,
0 , sizeof (mark));
cur.x
= si, cur.y = sj, cur.time = 0 , mark[cur.x][cur.y] = 1 ;
q.push(cur);
while ( ! q.empty())
{
cur
= q.top();
q.pop();
for (i = 0 ;i < 4 ;i ++ )
{
next.x
= cur.x + dir[i][ 0 ];
next.y
= cur.y + dir[i][ 1 ];
if ( ! mark[next.x][next.y] && out_line(next.x,next.y) &&
map[next.x][next.y]
!= ' # ' )
{
mark[next.x][next.y]
= 1 ;
if (map[next.x][next.y] == ' x ' )next.time = cur.time + 2 ;
else next.time = cur.time + 1 ;
if (map[next.x][next.y] == ' r ' ) return next.time;
q.push(next);
}
}
}
return - 1 ;
}
int main()
{
int ans;
while ( ~ scanf( " %d%d " , & n, & m))
{
getmap();
ans
= bfs(si,sj);
if (ans ==- 1 )
printf(
" Poor ANGEL has to stay in the prison all his life.\n " );
else printf( " %d\n " ,ans);
}
return 0 ;
}

转载于:https://www.cnblogs.com/laipDIDI/articles/2048900.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值