您的位置:首页 > 其它

HDU 1026 Ignatius and the Princess I

2012-11-24 21:57 357 查看

Ignatius and the Princess I

[b]Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7820 Accepted Submission(s): 2335Special Judge[/b][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 labyrinthis 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 wholelabyrinth. 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 minimumseconds), 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.XXXXX15 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)FINISHIt 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)FINISHGod please help our poor hero.FINISH[align=left]Author[/align]Ignatius.L费了半天劲,用dfs 写出来了熟悉了一下dfs,但是这题目dfs会超时的应该用bfs,明天打bfs,dfs还是和大家分享一下吧
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
using namespace std;
int a[150][150];
int status[150][150];
int statck[1000000][2];
int res[1000000][2];
int tag,top,s,min1,n,m,sum;
int vex[]={-1,1,0,0};
int vey[]={0,0,-1,1};
int main()
{
void dfs(int x,int y);
int i,j,t,flag,x1,y1,x2,y2;
char c;
while(scanf("%d %d%*c",&n,&m)!=EOF)
{
for(i=0;i<=n-1;i++)
{
for(j=0;j<=m-1;j++)
{
cin>>c;
if(c=='X')
{
a[i][j]=0;
}else if(c=='.')
{
a[i][j]=-1;
}else
{
a[i][j]=c-'0';
}
}
}
if(a[0][0]==0)
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
continue;
}
min1=0x7ffffff;
memset(status,0,sizeof(status));
top=0; tag=0; s=0;
statck[top][0]=0; statck[top++][1]=0;
status[0][0]=1;
if(a[0][0]!=-1)
{
s+=a[0][0];
}
dfs(0,0);
if(!tag)
{
printf("God please help our poor hero.\n");
printf("FINISH\n");
}else
{
flag=1;
printf("It takes %d seconds to reach the target position, let me show you the way.\n",min1);
for(i=0;i<=sum-2;i++)
{
x1=res[i][0]; y1=res[i][1];
if(i==0&&a[x1][y1]!=-1)
{
for(j=1;j<=a[x1][y1];j++)
{
printf("%ds:FIGHT AT (%d,%d)\n",flag,x1,y1);
flag++;
}
}
x2=res[i+1][0]; y2=res[i+1][1];
printf("%ds:(%d,%d)->(%d,%d)\n",flag,x1,y1,x2,y2);
flag++;
if(a[x2][y2]!=-1)
{
for(j=1;j<=a[x2][y2];j++)
{
printf("%ds:FIGHT AT (%d,%d)\n",flag,x2,y2);
flag++;
}
}
}
printf("FINISH\n");
}
}
return 0;
}
void dfs(int x,int y)
{
int i,j,xend,yend;
for(i=0;i<=3;i++)
{
xend=x+vex[i]; yend=y+vey[i];
if(xend>=0&&xend<=n-1&¥d>=0&¥d<=m-1&&!status[xend][yend]&&a[xend][yend]!=0)
{
s+=1;
status[xend][yend]=1;
statck[top][0]=xend; statck[top++][1]=yend;
if(a[xend][yend]!=-1)
{
s+=a[xend][yend];
}
if(xend!=n-1||yend!=m-1)
{
dfs(xend,yend);
}else
{
if(s<min1)
{
tag=1;
min1=s;
sum=top;
for(j=0;j<=sum-1;j++)
{
res[j][0]=statck[j][0];
res[j][1]=statck[j][1];
}
}
}
s-=1;
status[xend][yend]=0;
top-=1;
if(a[xend][yend]!=-1)
{
s-=a[xend][yend];
}
}
}
}
 今天用BFS写了一下这道题目,在用bfs 写的时候想了一下,其实这题再用bfs的时候和用bfs求最短路的思想是一个样的,出队列的标记为可用,当到达改点的时间更少而更新的时候检查一下这个点是否可用,也就是是否在队列中。如果在队列中就不用放在队列中,不在队列中就要放进这个队列,标记为不可用。这和bfs求最短路的思想是完全一样的。这是整个程序的核心。
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>using namespace std;int vex[]={-1,1,0,0};int vey[]={0,0,-1,1};int queue[1000000][2];int status[150][150],a[150][150],res[150][150];int statck[1000000][2],base,top;int pt[150][150];int INF=0x7ffffff;int n,m;int main(){void bfs();int i,j,s,t,x,y,x1,y1,x2,y2,flag,k;char c;while(scanf("%d %d%*c",&n,&m)!=EOF){for(i=0;i<=n-1;i++){for(j=0;j<=m-1;j++){scanf("%c",&c);if(c=='X'){a[i][j]=0;}else if(c=='.'){a[i][j]=-1;}else if(c>='1'&&c<='9'){a[i][j]=c-'0';}}getchar();}for(i=0;i<=n-1;i++){for(j=0;j<=m-1;j++){res[i][j]=INF;}}memset(status,0,sizeof(status));bfs();if(res[n-1][m-1]==INF){printf("God please help our poor hero.\n");printf("FINISH\n");}else{printf("It takes %d seconds to reach the target position, let me show you the way.\n",res[n-1][m-1]);x=n-1; y=m-1;top=0;while(1){statck[top][0]=x; statck[top++][1]=y;if(x==0&&y==0){break;}k=pt[x][y];x1=vex[k]*-1; y1=vey[k]*-1;x+=x1; y+=y1;}flag=1;for(i=top-1;i>=1;i--){x1=statck[i][0]; y1=statck[i][1];if(a[x1][y1]!=-1&&i==top-1){for(j=1;j<=a[x1][y1];j++){printf("%ds:FIGHT AT (%d,%d)\n",flag,x1,y1);flag++;}}x2=statck[i-1][0]; y2=statck[i-1][1];printf("%ds:(%d,%d)->(%d,%d)\n",flag,x1,y1,x2,y2);flag++;if(a[x2][y2]!=-1){for(j=1;j<=a[x2][y2];j++){printf("%ds:FIGHT AT (%d,%d)\n",flag,x2,y2);flag++;}}}printf("FINISH\n");}}return 0;}void bfs(){int i,j,t;int x,y,xend,yend;top=0;base=0;if(a[0][0]!=0){queue[top][0]=0; queue[top++][1]=0;if(a[0][0]!=-1){res[0][0]=a[0][0];}else{res[0][0]=0;}status[0][0]=1;}while(base<top){x=queue[base][0]; y=queue[base++][1];status[x][y]=0;for(i=0;i<=3;i++){xend=x+vex[i]; yend=y+vey[i];if(xend>=0&&xend<=n-1&¥d>=0&¥d<=m-1&&a[xend][yend]!=0){if(a[xend][yend]==-1){t=res[x][y]+1;}else{t=res[x][y]+1+a[xend][yend];}if(res[xend][yend]>t){res[xend][yend]=t;pt[xend][yend]=i;if(status[xend][yend]==0){queue[top][0]=xend; queue[top++][1]=yend;status[xend][yend]=1;}}}}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: