题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1026
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
while (scn.hasNext()) {
int height = Integer.parseInt(scn.next());
int width = Integer.parseInt(scn.next());
Search search = new Search(height, width, 0, 0);
for (int i = 0; i < height; ++i) {
search.setMatrixLine(i, scn.next().toCharArray());
}
search.beginSearch();
}
scn.close();
}
}
class Search {
private final int[][] direction = { { -1, 0 }, { 1, 0 }, { 0, -1 },
{ 0, 1 } };
private int height;
private int width;
private char[][] matrix;
private int startX;
private int startY;
public Search(int height, int width, int startX, int startY) {
this.height = height;
this.width = width;
matrix = new char[height][width];
this.startX = startX;
this.startY = startY;
}
public void beginSearch() {
Queue<Node> queue = new PriorityQueue<Node>();
queue.offer(new Node(startX, startY, 0, 0, -1, -1, null));
matrix[startX][startY] = 'X';
while (!queue.isEmpty()) {
Node node = queue.poll();
for (int k = 0; k < 4; ++k) {
int nextX = node.getX() + direction[k][0];
int nextY = node.getY() + direction[k][1];
int nextTime = -1;
int fightTime = 0;
if (0 > nextX || nextX >= height || 0 > nextY || nextY >= width
|| 'X' == matrix[nextX][nextY]) {
continue;
}
if ('.' == matrix[nextX][nextY]) {
nextTime = node.getTime() + 1;
} else if ('1' <= matrix[nextX][nextY]
&& '9' >= matrix[nextX][nextY]) {
nextTime = node.getTime() + 1 + matrix[nextX][nextY] - '1'
+ 1;
fightTime = matrix[nextX][nextY] - '1' + 1;
}
if (nextX == height - 1 && nextY == width - 1) {
System.out.println("It takes " + nextTime
+ " seconds to reach the target position, let me show you the way.");
Node out = new Node(nextX, nextY, nextTime, fightTime,
node.getX(), node.getY(), node);
Stack<Node> stack = new Stack<Node>();
while (null != out.getPreNode()) {
if (out.getFightTime() > 0) {
for (int i = 1; i <= out.getFightTime(); ++i) {
stack.push(new Node(out.getX(), out.getY(), out
.getTime(), -1, out.getPreX(), out
.getPreY(), null));
}
}
stack.push(out);
out = out.getPreNode();
}
int count = 0;
while (!stack.empty()) {
out = stack.pop();
if (out.getFightTime() != -1) {
System.out.println((++count) + "s:("
+ out.getPreX() + "," + out.getPreY()
+ ")->(" + out.getX() + "," + out.getY()
+ ")");
} else {
System.out.println((++count) + "s:FIGHT AT ("
+ out.getX() + "," + out.getY() + ")");
}
}
System.out.println("FINISH");
return;
}
queue.offer(new Node(nextX, nextY, nextTime, fightTime, node
.getX(), node.getY(), node));
matrix[nextX][nextY] = 'X';
}
}
System.out.println("God please help our poor hero.");
System.out.println("FINISH");
}
public void setMatrixLine(int line, char[] ch) {
matrix[line] = ch;
}
}
class Node implements Comparable<Node> {
private int x;
private int y;
private int time;
private int fightTime;
private int preX;
private int preY;
private Node preNode;
public Node(int x, int y, int time, int fightTime, int preX, int preY,
Node preNode) {
this.x = x;
this.y = y;
this.time = time;
this.fightTime = fightTime;
this.preX = preX;
this.preY = preY;
this.preNode = preNode;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getTime() {
return time;
}
public int getFightTime() {
return fightTime;
}
public int getPreX() {
return preX;
}
public int getPreY() {
return preY;
}
public Node getPreNode() {
return preNode;
}
@Override
public int compareTo(Node node) {
if (this.time != node.time) {
return this.time - node.time;
} else if (this.x != node.x) {
return this.x - node.x;
} else {
return this.y - node.y;
}
}
}