Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8 #.#####. #.a#..r. #..#x... ..#..#.# #...##.. .#...... ........
Sample Output
13
分析:这道题一开始我就直接奔着bfs求解最短路径去了,结果竟然是timeoutlimited,后来仔细读题才发现,原来有一个隐藏的要求我给忽略了,题目要求的是最小时间,我就直接奔着最短路径去了,殊不知忽略了权值的影响,遇到守卫时间是要加一的,也就是说,在考虑路径的同时也要考虑守卫带来的影响。如果路径短但是因为守卫多时间反而为增大,优先选择守卫少的的路径也是一个方面,应该综合考虑路径和守卫的双方面的影响。为此,我使用了优先队列,在综合考虑路径和守卫的同时从队列里面优先取出时间最小的节点,上代码:
import java.util.*; public class Main { static Scanner in = new Scanner(System.in); static int m,n,t,s1,e1,s2,e2; static char[][] ma = new char[205][205]; static boolean[][] vis = new boolean[205][205]; static int[][] dir = { {0,1},{1,0},{0,-1},{-1,0}}; //自定义比较器,按照步数(时间)从小到大排序 static Comparator<Node> com = new Comparator<Node>() { @Override public int compare(Node o1, Node o2) { return o1.step-o2.step; } }; static int bfs() { PriorityQueue<Node> q = new PriorityQueue<Node>(com); Node st = new Node(s1,e1,0); q.add(st); vis[s1][e1]=true; int tx,ty; while(!q.isEmpty()){ Node te=q.poll(); if(te.x==s2&&te.y==e2)//到达终点,返回 return te.step; for (int i = 0; i < 4; i++) { tx=te.x+dir[i][0]; ty=te.y+dir[i][1]; if(tx<0||ty<0||tx>=n||ty>=m) continue; if(!vis[tx][ty]&&ma[tx][ty]!='#'){ vis[tx][ty]=true; Node nw; if(ma[tx][ty]=='x')//如果是守卫,步数加一 nw=new Node(tx, ty, te.step+1+1); else nw=new Node(tx, ty, te.step+1); q.add(nw); } } } return 0; } public static void main(String[] args) { while(in.hasNext()) { n = in.nextInt(); m = in.nextInt(); if(m==0&&n==0) break; String s; for (int i = 0; i < n; i++) { s=in.next(); for (int j = 0; j < s.length(); j++) { ma[i][j]=s.charAt(j); vis[i][j]=false; if(ma[i][j]=='a'){ s1=i;e1=j; } if(ma[i][j]=='r'){ s2=i;e2=j; } } } int res; res = bfs(); if(res!=0) System.out.println(res); else System.out.println("Poor ANGEL has to stay in the prison all his life."); } } } class Node{ int x; int y; int step; Node (){} Node(int x,int y,int step){ this.x=x; this.y=y; this.step=step; } }