Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6305 Accepted Submission(s): 1882
Special Judge
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.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX.
5 6
.XX.1.
..X.2.
2...X.
...XX.
XXXXX1
5 6
.XX...
..XX1.
2...X.
...XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH
题意:给个二维数组,'.'可以走,'X'不可走,'1-9'代表在此消耗的时间
输出记录从(0,0)到(n-1,m-1)的耗时最小值
#include<stdio.h>
#include<queue>
using namespace std;
int m,n;
char a[105][105];
int d[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
struct haha
{
int x1;
int y1;
int ti;
friend bool operator < (haha a, haha b)
{
return a.ti > b.ti; //重载小于号使得小的先出队列
}
}q,temp;
int flag[105][105];
struct xixi
{
int x2;
int y2;
}pre[105][105];
void BFS()
{
int i,x,y,time,x0,y0,xi,yi,j,tm;
q.x1=n-1;q.y1=m-1;q.ti=0;
if(a[n-1][m-1]>=48&&a[n-1][m-1]<=57)
q.ti=a[n-1][m-1]-'0';
priority_queue<haha>duilie;//这个要放进来 每次都要进行从新分配一个队列 不然的话 就会造成上一次的数据无法清空
duilie.push(q);
while(!duilie.empty())
{
temp=duilie.top();
duilie.pop();
if(temp.x1==0&&temp.y1==0)
{
printf ("It takes %d seconds to reach the target position, let me show you the way.\n",temp.ti);
xi=0;yi=0;tm=1;
while(pre[xi][yi].x2!=-1)
{
x0=pre[xi][yi].x2;
y0=pre[xi][yi].y2;
printf("%ds:(%d,%d)->(%d,%d)\n",tm++,xi,yi,x0,y0);
if(a[x0][y0]>=48&&a[x0][y0]<=57)//这里不小心 把xo写成了x WA了 10多次 哎 这就是不细心的下场啊 耽误我3个多小时啊
{
for(j=0;j<a[x0][y0]-'0';j++)
printf ("%ds:FIGHT AT (%d,%d)\n",tm++,x0,y0);
}
xi=x0;
yi=y0;
}
return ;
}
for(i=0;i<4;i++)
{
x=temp.x1+d[i][0];
y=temp.y1+d[i][1];
if(x<0||y<0||x>=n||y>=m||a[x][y]=='X'||flag[x][y]!=0) continue;
if(a[x][y]>=48&&a[x][y]<=57)
{ time=temp.ti+a[x][y]-'0'; time=time+1;}
else
time=temp.ti+1;
q.ti=time;q.x1=x;q.y1=y;
flag[x][y]=1;
pre[x][y].x2=temp.x1; pre[x][y].y2=temp.y1;
duilie.push(q);
}
}
puts("God please help our poor hero.");
}
int main()
{
int i;
while (scanf ("%d %d", &n, &m)!=EOF)
{
for(i=0;i<n;i++)
scanf("%s",a[i]);
memset(flag,0,sizeof(flag));
flag[n-1][m-1]=1;
pre[n-1][m-1].x2=-1;
BFS();
puts("FINISH");
}
return 0;
}
/*参考了 大牛的代码 一开始按照自己的方法弄了个runtime error 郁闷死了 这个方法简便 很好的一种思想 记每一步都要标记上一步的xy
并且 倒着去搜索 很方便按题意的要求 输出数据 帅呆了 */