http://acm.hdu.edu.cn/showproblem.php?pid=1026
// 一个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 que = new PriorityQueue(100,new Comparator(){
@Override
public int compare(Position o1, Position o2) {
if(o1.stepNum >= o2.stepNum){
return 1;
}
return -1;
}
});
Stack stack = new Stack();
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='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();
}
}