您的位置:首页 > 其它

杭电-1026Ignatius and the Princess I(BFS+记录路径)

2015-10-30 20:52 447 查看

Ignatius and the Princess I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15010 Accepted Submission(s): 4766

Special Judge

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) 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.

[align=left]Output[/align]
For each test case, you should output "God please help our poor hero." if Ignatius can't reach the target position, or you should output "It takes n seconds to reach the target position, let me show you the way."(n is the minimum
seconds), and tell our hero the whole path. Output a line contains "FINISH" after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.

[align=left]Sample Input[/align]

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.


[align=left]Sample Output[/align]

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


这个题看着挺难的,其实也不简单!

主要意思是,从左上角走到右下角,若能走到,记录路径并输出!

这里记录路径我用的是结构体数组+栈

一定注意下,用队列前要清空队列,否则超内存

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
int m,n,vis[102][102],mov[4][2]={0,1,0,-1,1,0,-1,0};
char map[102][102];
struct node
{
friend bool operator<(node a,node b)
{
return a.step>b.step;
}
int x,y,step;
}ta,tb;
struct nodex
{
int x;
int y;
}mm;
nodex best[102][102];
priority_queue<node>q;
stack<nodex> w;
bool can(node x)
{
if(x.x<0||x.x>m-1||x.y<0||x.y>n-1||vis[x.x][x.y]||map[x.x][x.y]=='X')
return false;
return true;
}
int bfs()
{
int i,cot=0;
ta.x=0;
ta.y=0;
ta.step=0;
while(!q.empty())
q.pop();//否则超内存
vis[ta.x][ta.y] = 1;
q.push(ta);
while(!q.empty())
{
ta=q.top();
q.pop();
if(ta.x==m-1&&ta.y==n-1)
return ta.step;
for(i=0;i<4;i++)
{
tb.x=ta.x+mov[i][0];
tb.y=ta.y+mov[i][1];
if(can(tb))
{
mm.x=ta.x;
mm.y=ta.y;
if(map[tb.x][tb.y]>='1'&&map[tb.x][tb.y]<='9')
tb.step=ta.step+map[tb.x][tb.y]-'0'+1;
else
tb.step=ta.step+1;

vis[tb.x][tb.y]=1;
q.push(tb);
best[tb.x][tb.y]=mm;
}
}
}
return 0;
}
int main()
{
int i;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
scanf("%s",map[i]);
memset(vis,0,sizeof(vis));
int t=bfs();
if(!t)
printf("God please help our poor hero.\nFINISH\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",t);
nodex qq;
best[0][0].x=-1;
best[0][0].y=-1;
qq.x=m-1;
qq.y=n-1;
while(best[qq.x][qq.y].x!=-1&&best[qq.x][qq.y].y!=-1)
{
w.push(qq);
qq=best[qq.x][qq.y];
}
int time=1;
while(!w.empty())
{
printf("%ds:(%d,%d)->(%d,%d)\n",time++,qq.x,qq.y,w.top().x,w.top().y);
qq=w.top();
if(map[qq.x][qq.y]!='.')
{
for(i=1;i<map[qq.x][qq.y]-'0'+1;i++)
printf("%ds:FIGHT AT (%d,%d)\n",time++,qq.x,qq.y);
}
w.pop();
}
printf("FINISH\n");
}
}
return 0;
}


下面是我用DFS写的,不过,超时了!

#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;
char map[110][110];
int m,n,nex[10100],vis[110][110],mov[4][2]={0,1,0,-1,1,0,-1,0},best,cot;
int bes[10100];
bool can(int x,int y)
{
if(x<0||x>m-1||y<0||y>n-1||map[x][y]=='X'||vis[x][y])
return false;
return true;
}
void dfs(int x,int y,int time)
{
int i,j;
if(x==m-1&&y==n-1)
{
if(time<best)
{
best=time;
for(i=0;i<cot;i++)
bes[i]=nex[i];
}
return ;
}
for(i=0;i<4;i++)
{
int xx=x+mov[i][0],yy=y+mov[i][1];
if(can(xx,yy))
{
vis[xx][yy]=1;
if(map[xx][yy]>='1'&&map[xx][yy]<='9')
{
for(j=0;j<=map[xx][yy]-'0';j++)
nex[cot++]=xx*n+yy;
dfs(xx,yy,time+map[xx][yy]-'0'+1);
cot-=(map[xx][yy]-'0'+1);
}
else
{
nex[cot]=xx*n+yy;
cot++;
dfs(xx,yy,time+1);
cot--;
}
vis[xx][yy]=0;
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
for(i=0;i<m;i++)
scanf("%s",map[i]);
best=INF;
memset(vis,0,sizeof(vis));
memset(nex,0,sizeof(nex));
memset(bes,0,sizeof(bes));
vis[0][0]=1;
nex[0]=0;
cot=1;
dfs(0,0,0);
if(best==INF)
printf("God please help our poor hero.\nFINISH\n");
else
{
printf("It takes %d seconds to reach the target position, let me show you the way.\n",best);
for(i=1;i<=best;i++)
{
int x1=bes[i-1]/n;
int y1=bes[i-1]%n;
int x2=bes[i]/n;
int y2=bes[i]%n;
if(bes[i]!=bes[i-1])
{
printf("%ds:(%d,%d)->(%d,%d)\n",i,x1,y1,x2,y2);
}
else
{
printf("%ds:FIGHT AT (%d,%d)\n",i,x2,y2);
}
}
printf("FINISH\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: