ACM HDOJ 1026(Ignatius and the Princess I)

题目链接 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;
		}
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值