Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12560 Accepted Submission(s): 3960
Special Judge
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.
labyrinth. The input is terminated by the end of file. More details in the Sample Input.
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.
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.
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
/* hdoj 1026 BFS 利用递归打印路径 */ #include<iostream> #include<stdio.h> #include<queue> using namespace std; #define N 101 struct node{ int x,y,step,pre_dir; }; node ans[N][N]; char map[N][N]; int direc[4][2]={1,0,-1,0,0,-1,0,1}; int n,m,flag; bool check(int x,int y) { if(x>=0&&y>=0&&x<n&&y<m&&map[x][y]!='X') return true; return false; } void printPath(int pre_dir,int x,int y,int step) { int pre_x=x-direc[pre_dir][0]; int pre_y=y-direc[pre_dir][1]; if(x==0&&y==0) return ; if(map[x][y]=='.') { printPath(ans[pre_x][pre_y].pre_dir,pre_x,pre_y,step-1); printf("%ds:(%d,%d)->(%d,%d)\n",step,pre_x,pre_y,x,y); } else { printPath(ans[pre_x][pre_y].pre_dir,pre_x,pre_y,step-(map[x][y]-'0')-1); printf("%ds:(%d,%d)->(%d,%d)\n",step-(map[x][y]-'0'),pre_x,pre_y,x,y); for(int i=map[x][y]-'0'-1;i>=0;i--) printf("%ds:FIGHT AT (%d,%d)\n",step-i,x,y); } } void BFS() { node start,end; int i; queue<node> que; start.x=0; start.y=0; start.step=0; start.pre_dir=-1; que.push(start); while(!que.empty()) { start=que.front(); que.pop(); if(start.x==n-1&&start.y==m-1) flag=1; for(i=0;i<4;i++) { end.x=start.x+direc[i][0]; end.y=start.y+direc[i][1]; end.step=start.step; end.pre_dir=start.pre_dir; if(check(end.x,end.y)) { if(map[end.x][end.y]=='.') { end.step+=1; if(end.step<ans[end.x][end.y].step) { ans[end.x][end.y].step=end.step; ans[end.x][end.y].pre_dir=i; que.push(end); } } else if(map[end.x][end.y]>='1'&&map[end.x][end.y]<='9') { end.step+=map[end.x][end.y]-'0'+1; if(end.step<ans[end.x][end.y].step) { ans[end.x][end.y].step=end.step; ans[end.x][end.y].pre_dir=i; que.push(end); } } } } } } int main() { int i,j; //freopen("test.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) scanf("%s",map[i]); for(i=0;i<n;i++) for(j=0;j<m;j++) ans[i][j].step=10000000; flag=0; BFS(); if(!flag) { 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",ans[n-1][m-1].step); printPath(ans[n-1][m-1].pre_dir,n-1,m-1,ans[n-1][m-1].step); printf("FINISH\n"); } } return 0; }