Ignatius and the Princess I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10051 Accepted Submission(s): 3023
Special Judge
Problem DescriptionThe 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.
InputThe 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.
OutputFor 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.
Sample Input
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.
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)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
題意:一個N*M的圖,一秒內可上下左右選個方向走一步,一進去,若有數字,就要花數字大小的時間停留在那個格子,輸出從起點(0, 0)到終點(N-1, M-1)每一秒的路徑。
思路: 優先隊列+BFS(廣度優先搜尋)
import java.io.*;import java.util.*;public class Main {int n,m;char ch[][];boolean boo[][];int fx[]={1,-1,0,0};int fy[]={0,0,-1,1};Queue<Node> que=new PriorityQueue<Node>();//優先隊列public static void main(String[] args) {new Main().work();}void work(){Scanner sc=new Scanner(new BufferedInputStream(System.in));while(sc.hasNext()){que.clear();n=sc.nextInt();m=sc.nextInt();ch=new char[n][m];boo=new boolean[n][m];for(int i=0;i<n;i++){String s=sc.next();ch[i]=s.toCharArray();Arrays.fill(boo[i],false);}Node node=new Node();node.x=0;node.y=0;node.t=0;String s1="("+node.x+","+node.y+")";node.s=s1+" ";boo[node.x][node.y]=true;;que.add(node);BFS();}}void BFS(){while(!que.isEmpty()){Node node=que.poll();if((node.x==n-1)&&(node.y==m-1)){out(node.s,node.t);return;}for(int i=0;i<4;i++){int px=node.x+fx[i];int py=node.y+fy[i];if(check(px,py)&&!boo[px][py]){boo[px][py]=true;Node td=node.getNode();td.x=px;td.y=py;String s1="("+td.x+","+td.y+")";td.s+=s1+" ";if(ch[px][py]=='.'){td.t=td.t+1;}else{td.t=td.t+(ch[px][py]-'0');td.t=td.t+1;for(int j=0;j<ch[px][py]-'0';j++){td.s+=s1+" ";}}que.add(td);}}}System.out.println("God please help our poor hero.");System.out.println("FINISH");}//輸出void out(String s,int time){System.out.println("It takes "+time+" seconds to reach the target position, let me show you the way.");String str[]=s.split(" ");for(int i=1;i<str.length;i++){if(str[i].length()!=0){System.out.print(i+"s:");if(str[i-1].equals(str[i])){System.out.println("FIGHT AT "+str[i]);}else{System.out.println(str[i-1]+"->"+str[i]);}}}System.out.println("FINISH");}boolean check(int px,int py){if(px<0||px>n-1||py<0||py>m-1||ch[px][py]=='X')return false;return true;}class Node implements Comparable<Node>{int x;int y;int t;String s;public int getT() {return t;}public void setT(int t) {this.t = t;}Node getNode(){Node node=new Node();node.t=t;node.s=s;return node;}public int compareTo(Node o) {//按升序排序return this.t>o.t?1:-1;}}}