Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14677 Accepted Submission(s): 4653
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.
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
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 struct node 8 { 9 int x,y,step; 10 friend bool operator < (node n1,node n2) //优先队列,队列从小到大排序 11 { 12 return n1.step>n2.step; 13 } 14 } s1,s2,ss[110][110]; //ss数组记录走的所有路径 15 char map[105][105]; 16 int visit[105][105],n,m,t,p; 17 int f[4][2]= {0,1,0,-1,1,0,-1,0}; 18 19 void BFS() 20 { 21 priority_queue <node> q; //优先队列的定义 22 while(!q.empty()) 23 q.pop(); 24 s1.x=0; 25 s1.y=0; 26 s1.step=0; 27 visit[0][0]=1; 28 ss[s1.x][s1.y].x=0; //起点为左上角 29 ss[s1.x][s1.y].y=0; 30 q.push(s1); 31 while(!q.empty()) 32 { 33 s1=q.top(); 34 q.pop(); 35 if(s1.x==n-1&&s1.y==m-1) //终点为右下角 36 { 37 p=s1.step; 38 return; 39 } 40 for(int i=0; i<4; i++) 41 { 42 s2=s1; 43 s2.x=s1.x+f[i][0]; 44 s2.y=s1.y+f[i][1]; 45 if(s2.x>=0&&s2.x<n&&s2.y>=0&&s2.y<m&&map[s2.x][s2.y]!='X'&&!visit[s2.x][s2.y]) 46 { 47 visit[s2.x][s2.y]=1; 48 s2.step=s1.step+1; 49 if(map[s2.x][s2.y]>='1'&&map[s2.x][s2.y]<='9') //遇到怪物的时候需要耗得时间 50 s2.step=s2.step+map[s2.x][s2.y]-'0'; 51 ss[s2.x][s2.y].x=s1.x; //记录走到这一步的上一步坐标 52 ss[s2.x][s2.y].y=s1.y; 53 q.push(s2); 54 } 55 } 56 } 57 p=-1; //到不了的标记 58 return; 59 } 60 61 void print(int x,int y) //深搜输出,从终点开始往前搜 62 { 63 if(x==0&&y==0) return; //找到起点,返回上一次 64 print(ss[x][y].x,ss[x][y].y); 65 printf("%ds:(%d,%d)->(%d,%d) ",t++,ss[x][y].x,ss[x][y].y,x,y); 66 if(map[x][y]>='1'&&map[x][y]<='9') //如果遇到怪兽,多呆几秒的输出 67 { 68 int w=map[x][y]-'0'; 69 for(int i=w; i>0; i--) 70 printf("%ds:FIGHT AT (%d,%d) ",t++,x,y); 71 } 72 } 73 int main() 74 { 75 int i,j; 76 while(~scanf("%d%d",&n,&m)) 77 { 78 for(i=0; i<n; i++) 79 scanf("%s",&map[i]); 80 memset(visit,0,sizeof(visit)); 81 BFS(); 82 t=1; 83 if(p==-1) //到不了的特定输出 84 printf("God please help our poor hero. "); 85 else 86 { 87 printf("It takes %d seconds to reach the target position, let me show you the way. ",p); //输出一共需要多少秒 88 print(n-1,m-1); 89 } 90 printf("FINISH "); //别忘了需要输出的最后一句话 91 } 92 return 0; 93 }