公主被恶人抓走,被关押在牢房的某个地方。牢房用N*M (N, M <= 200)的矩阵来表示。矩阵中的每项可以代表道路(@)、墙壁(#)、和守卫(x)。 英勇的骑士(r)决定孤身一人去拯救公主(a)。我们假设拯救成功的表示是“骑士到达了公主所在的位置”。由于在通往公主所在位置的道路中可能遇到守卫,骑士一旦遇到守卫,必须杀死守卫才能继续前进。 现假设骑士可以向上、下、左、右四个方向移动,每移动一个位置需要1个单位时间,杀死一个守卫需要花费额外的1个单位时间。同时假设骑士足够强壮,有能力杀死所有的守卫。
给定牢房矩阵,公主、骑士和守卫在矩阵中的位置,请你计算拯救行动成功需要花费最短时间。
输入 第一行为一个整数S,表示输入的数据的组数(多组输入) 随后有S组数据,每组数据按如下格式输入 1、两个整数代表N和M, (N, M <= 200). 2、随后N行,每行有M个字符。"@"代表道路,"a"代表公主,"r"代表骑士,"x"代表守卫, "#"代表墙壁。 输出 如果拯救行动成功,输出一个整数,表示行动的最短时间。 如果不可能成功,输出"Impossible" 样例输入 2 7 8 #@#####@ #@a#@@r@ #@@#x@@@ @@#@@#@# #@@@##@@ @#@@@@@@ @@@@@@@@ 13 40 @x@@##x@#x@x#xxxx##@#x@x@@#x#@#x#@@x@#@x xx###x@x#@@##xx@@@#@x@@#x@xxx@@#x@#x@@x@ #@x#@x#x#@@##@@x#@xx#xxx@@x##@@@#@x@@x@x @##x@@@x#xx#@@#xxxx#@@x@x@#@x@@@x@#@#x@# @#xxxxx##@@x##x@xxx@@#x@x####@@@x#x##@#@ #xxx#@#x##xxxx@@#xx@@@x@xxx#@#xxx@x##### #x@xxxx#@x@@@@##@x#xx#xxx@#xx#@#####x#@x xx##@#@x##x##x#@x#@a#xx@##@#@##xx@#@@x@x x#x#@x@#x#@##@xrx@x#xxxx@##x##xx#@#x@xx@ #x@@#@###x##x@x#@@#@@x@x@@xx@@@@##@@x@@x x#xx@x###@xxx#@#x#@@###@#@##@x#@x@#@@#@@ #@#x@x#x#x###@x@@xxx####x@x##@x####xx#@x #x#@x#x######@@#x@#xxxx#xx@@@#xx#x#####@ 样例输出 13 7 使用到优先的队列,PriorityQueue类在Java1.5中引入并作为 Java Collections Framework 的一部分。PriorityQueue是基于优先堆的一个无界队列,这个优先队列中的元素可以默认自然排序或者通过提供的Comparator(比较器)在队列实例化的时排序。优先队列不允许空值,而且不支持non-comparable(不可比较)的对象,比如用户自定义的类。优先队列要求使用Java Comparable和Comparator接口给对象排序,并且在排序时会按照优先级处理其中的元素。
优先队列的头是基于自然排序或者Comparator排序的最小元素。如果有多个对象拥有同样的排序,那么就可能随机地取其中任意一个。当我们获取队列时,返回队列的头对象。
优先队列的大小是不受限制的,但在创建时可以指定初始大小。当我们向优先队列增加元素的时候,队列大小会自动增加。
PriorityQueue是非线程安全的,所以Java提供了PriorityBlockingQueue(实现BlockingQueue接口)用于Java多线程环境。
使用优先队列需要对自定义的类实现排序,即实现Comparable接口,重写compareTo方法,优先队列每次弹出的变量可能不是最头的节点,而是符合排序的节点。优先队列在方法上和一般的队列相同。
import java.util.*; //http://bailian.openjudge.cn/practice/4116/ class nodes implements Comparable<nodes>{ int x; int y; int step; public nodes(int x, int y, int step) { this.x = x; this.y = y; this.step = step; } @Override public int compareTo(nodes o) { // TODO Auto-generated method stub if( this.step > o.step ){ return 1; }else if( this.step == o.step){ return 0; }else{ return -1; } } } public class SavingPlan { private static Scanner input; static int N, M; static boolean[][] visited; static char[][] maps; static int[] dirx = { 0, 0, 1, -1 }; static int[] diry = { 1, -1, 0, 0 }; static int startx, starty; public static void main(String[] args) { input = new Scanner(System.in); int n = input.nextInt(); while (n-- != 0) { N = input.nextInt(); M = input.nextInt(); maps = new char[N][M]; visited = new boolean[N][M]; for (int i = 0; i < N; i++) { String s = input.next(); maps[i] = s.toCharArray(); Arrays.fill(visited[i], false); } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++){ if (maps[i][j] == 'r') { startx = i; starty = j; break; } } BFS(startx, starty); } } private static void BFS(int x, int y) { // TODO Auto-generated method stub Queue<nodes> q = new PriorityQueue<nodes>(); nodes first = new nodes(x, y, 0); q.add(first); visited[x][y] = true; boolean flag = false; while (!q.isEmpty()) { nodes n = q.poll(); for (int i = 0; i < 4; i++) { int row = n.x + dirx[i]; int col = n.y + diry[i]; if (row < 0 || row >= N || col < 0 || col >= M) continue; if (maps[row][col] == '#') continue; if (!visited[row][col]) { visited[row][col] = true; if (maps[row][col] == 'a') { System.out.println(n.step + 1); flag = true; } else if (maps[row][col] == '@') { q.add(new nodes(row, col, n.step + 1)); } else { q.add(new nodes(row, col, n.step + 2)); } } } } if (!flag) System.out.println("Impossible"); } }