湖南科技大学OJ Ignatius and the Princess I 加强

问题 G: Ignatius and the Princess I 加强

时间限制: 2 Sec 内存限制: 128 MB
提交: 23 解决: 13
[ 提交][ 状态][ 讨论版]

题目描述

The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166's castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166's room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.

输入

The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=200,2<=M<=200) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.

输出

For each test case, you should output is the minimum seconds,More details in the Sample Output.

样例输入

5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1

样例输出

13
14

提示

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
char a[205][205];
struct haha
{
int x;
int y;
int ti;
bool operator <( const haha &a) const //a是随意定的名字 就用来当做一个结构体的节点
{ return ti>a.ti;
//注意对于结构体的优先队列要把自定义函数放进结构体内才行 >使得小的先出 <使得大的先出
}
}q,temp;
int step[4][2]={1,0,-1,0,0,1,0,-1},ans,flag,n,m;
void BFS()
{
int i,x,y;
priority_queue<haha>duilie; //这个要放进来 每次都要进行从新分配一个队列 不然的话 就会造成上一次的数据无法清空
q.x=0;q.y=0;q.ti=0;
duilie.push(q);
a[0][0]= '#' ;
while (!duilie.empty())
{
temp=duilie.top();
duilie.pop();
if (temp.x==n-1&&temp.y==m-1)
{
ans=temp.ti; return ;
}
for (i=0;i<4;i++)
{
x=temp.x+step[i][0]; y=temp.y+step[i][1];
if (x>=0&&x<n&&y>=0&&y<m)
{
if (a[x][y]== 'X' ) continue ;
if (a[x][y]== '.' )
{
q.x=x;
q.y=y;
q.ti=temp.ti+1;
duilie.push(q);
}
if (a[x][y]<= '9' &&a[x][y]>= '1' )
{
q.x=x;
q.y=y;
q.ti=temp.ti+a[x][y]- '0' +1;
duilie.push(q);
}
a[x][y]= '#' ;
}
}
}
}
int main()
{
int i;
while ( scanf ( "%d %d" ,&n,&m)!=EOF)
{
ans=999999999;flag=0;
for (i=0;i<n;i++)
scanf ( "%s" ,a[i]);
BFS();
printf ( "%d\n" ,ans);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值