HDU 1026 (BFS + 優先隊列)

http://acm.hdu.edu.cn/showproblem.php?pid=1026

Problem Description
The 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.
 

Input
The 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.
 

Output
For 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. XXXXX1 5 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) 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

// 一個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();
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章