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.
题意是要求从起点到终点所花费的最短时间,第一反应是用 bfs和队列实现。但是这样会超时,所以使用bfs和优先队列,优先队列是在普通队列上加入了优先级,优先弹出那些优先级高的元素,本题中是优先弹出花费时间小的节点。
实现:
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Stack;
public class P1026 {
int n, m, mins;
char[][] maze;
int[][] vis;
int[] dr = { -1, 0, 1, 0 };
int[] dc = { 0, -1, 0, 1 };
Point[][] roads;
int success;
public static void main(String[] args) {
new P1026().run();
}
public void run() {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextInt()) {
n = scanner.nextInt();
m = scanner.nextInt();
maze = new char[n][m];
vis = new int[n][m];
roads = new Point[n][m];
String line;
scanner.nextLine();
for (int i = 0; i < n; i++) {
line = scanner.nextLine();
for (int j = 0; j < m; j++) {
maze[i][j] = line.charAt(j);
vis[i][j] = 0;
}
}
success = 0;
bfs();
if (success == 1) {
System.out.printf("It takes %d seconds to reach the target position, let me show you the way.\n",mins);
print();
}else{
System.out.printf("God please help our poor hero.\nFINISH\n");
}
}
scanner.close();
}
public void bfs() {
Point point = new Point();
PriorityQueue queue = new PriorityQueue<>();
queue.offer(point);
vis[0][0] = 1;
while (!queue.isEmpty()) {
point = queue.poll();
if (point.x == n - 1 && point.y == m - 1) {
mins = point.cnt;
success = 1;
return ;
}
for (int d = 0; d < 4; d++) {
int rtemp = point.x + dr[d];
int ctemp = point.y + dc[d];
if (rtemp >= 0 && rtemp < n && ctemp >= 0 && ctemp < m
&&vis[rtemp][ctemp] != 1 && maze[rtemp][ctemp] != 'X') {
vis[rtemp][ctemp] = 1;
roads[rtemp][ctemp] = new Point(point.x, point.y, point.cnt);
if (maze[rtemp][ctemp] == '.') {
queue.offer(new Point(rtemp, ctemp, point.cnt + 1));
} else {
queue.offer(new Point(rtemp, ctemp, point.cnt
+ (maze[rtemp][ctemp] - '0' + 1)));
}
}
}
}
}
public void print() {
Stack stack = new Stack<>();
Point temp = roads[n - 1][m - 1];
stack.push(new Point(n - 1, m - 1, mins));
while (temp.x != 0 || temp.y != 0) {
stack.push(temp);
temp = roads[temp.x][temp.y];
}
int t = 1;
while (!stack.empty()) {
temp = stack.peek();
stack.pop();
if (maze[temp.x][temp.y] == '.')
System.out.printf("%ds:(%d,%d)->(%d,%d)\n", t++,
roads[temp.x][temp.y].x ,
roads[temp.x][temp.y].y , temp.x , temp.y );
else {
System.out.printf("%ds:(%d,%d)->(%d,%d)\n", t++,
roads[temp.x][temp.y].x ,
roads[temp.x][temp.y].y , temp.x , temp.y );
int k = maze[temp.x][temp.y] - '0';
while (k-- != 0)
System.out.printf("%ds:FIGHT AT (%d,%d)\n", t++,
temp.x , temp.y );
}
}
System.out.printf("FINISH\n");
}
class Point implements Comparable {
int x, y, cnt;
public Point() {
x = 0;
y = 0;
cnt = 0;
}
public Point(int x, int y, int cnt) {
this.x = x;
this.y = y;
this.cnt = cnt;
}
@Override
public int compareTo(Point o) {
// TODO Auto-generated method stub
return this.cnt > o.cnt ? 1 : -1;
}
}
}
注:提交到OJ上,请将主类名改为Main。