http://acm.hdu.edu.cn/showproblem.php?pid=1026
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.
// 一個WA 代碼:(不知道爲什麼)
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
class Position{
int x,y;
int stepNum = 0;
int s =0;
Position pre = null;// 記錄當前 點的父節點
int extra = 0;
public Position(int x,int y,int stepNum){
this.x = x; this.y = y; this.stepNum = stepNum;
}
public void addNum(int step){
this.stepNum += step;
}
public void setPre(Position pre){
this.pre = pre;
}
public void setExtra(int extra){
this.extra = extra;
}
public void setS(int s){
this.s = s;
}
}
public class HDU1026 {
int n,m;
char map[][];
boolean vis[][]; // 標記是否訪問
int dis[][] ={{1,0},{-1,0},{0,-1},{0,1}};
Position temp;
PriorityQueue<Position> que = new PriorityQueue<Position>(100,new Comparator<Position>(){
@Override
public int compare(Position o1, Position o2) {
if(o1.stepNum >= o2.stepNum){
return 1;
}
return -1;
}
});
Stack<Position> stack = new Stack<Position>();
public void solve(){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
boolean flage = false;
n = sc.nextInt(); m = sc.nextInt();
map = new char[n][m]; vis = new boolean[n][m];
// 輸入函數
for(int i=0; i<n; i++){
String str = sc.next();
map[i] = str.toCharArray();
}
que.clear(); stack.clear();
temp = new Position(0,0,0);// temp 現在表示起始位置
// if(map[0][0] >='1' && map[0][0]<='9'){
// temp.extra = map[0][0]-'0';
// //temp.stepNum = temp.extra;
// }
vis[0][0] = true;
que.add(temp);
while( !que.isEmpty()){
temp = que.poll();
if( temp.x == n-1 && temp.y == m-1){
flage = true;
int an = temp.stepNum;
System.out.println("It takes "+temp.stepNum+" seconds to reach the target position, let me show you the way.");
while(temp != null){
stack.add(temp);
temp = temp.pre;
}
int nums = 1;
for(int i=0; i< stack.peek().extra; i++){
System.out.println(nums+"s:FIGHT AT ("+stack.peek().x+","+stack.peek().y+")");
nums++;
}
while( !stack.empty()){
if(nums == an+1){
System.out.println("FINISH");
break;
}
System.out.print(nums+"s:("+stack.peek().x+","+stack.peek().y+")-->");
nums++;
stack.pop();
if(!stack.isEmpty()){
System.out.println("("+stack.peek().x+","+stack.peek().y+")");
for(int i=0; i< stack.peek().extra; i++){
System.out.println(nums+"s:FIGHT AT ("+stack.peek().x+","+stack.peek().y+")");
nums++;
}
}
}
break;
}
for(int i=0; i<4; i++){
int tx=0,ty = 0;
int t =0;
tx = temp.x + dis[i][0]; ty = temp.y + dis[i][1];
if( tx<0 || tx >=n || ty<0 || ty>=m || vis[tx][ty] || map[tx][ty] =='X' )
continue;
Position now = new Position(tx,ty,temp.stepNum + 1);
now.setS(temp.s+1);
if( map[tx][ty] >='1' && map[temp.x][temp.y] <= '9'){
t = map[tx][ty]-'0';
}
now.addNum(t);
now.setPre(temp);
now.setExtra(t);
vis[tx][ty] = true;
que.add(now);
}
}
if( !flage){
System.out.println("God please help our poor hero.");
System.out.println("FINISH");
}
}
}
public static void main(String[] args) {
new HDU1026().solve();
}
}