Description
Hong was kidnapped by a bad woman. She was put in a *. She transmitted distress signals to S
students to save her as soon as possible.
The * can be considered as a N\times MN×M matrix. Each grid can be road, barrier or wall. In each unit time, a student can move to an adjacent road(two grid are adjacent if and only if they have public side) or change an adjacent barrier to road. Wall can not be passed.
These S
students are originally at S
different positions and depart at t = 0 . Each of them wants to save Hong as soon as possible. Hong wants to know when she will be saved.
Input
The first line contains two integers N
and M
.
Each of the next N
lines contains M
chars, 'R' is a grid of road, 'B' is a grid of barrier, 'W' is a grid of wall, 'H' is the position of Hong, 'S' is the position of a student.
Output
Output a single integer, shows the minimal time Hong can be saved.
We guarantee that there must be a way to save Hong.
Sample Input
3 3
SBW
WRB
RBH
Sample output
6
Limit
1 second for each test case. The memory limit is 256MB.
For 20% test cases, N,M \leq 200N,M≤200.
For 50% test cases, there is no barrier.
For 100% test cases, N,M \leq 2000N,M≤2000.
Solution
import java.io.*;
import java.util.*;
class Node implements Comparable<Node>{
int x, y, dist = 0;
Node(int x, int y, int dist){this.x = x; this.y = y; this.dist = dist;}
@Override
public int compareTo(Node node){
return dist - node.dist;
}
}
public class Main{
static int[] dx = {-1, 1,0, 0 };
static int[] dy = {0, 0, 1, -1};
static int N = 0;
static int M = 0;
static char[][] state = null;
public static void main(String[] args)throws Exception{
InputStream sys = System.in;
// InputStream fil = new FileInputStream(new File("C:\\Users\\ASUS\\desktop\\data4.txt"));
long b = System.currentTimeMillis();
BufferedReader in = new BufferedReader(new InputStreamReader(sys));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
st = new StringTokenizer(in.readLine()); //buffer: 31KiB
N = Short.valueOf(st.nextToken());
M = Short.valueOf(st.nextToken());
state = new char[N][M];//2000*2000*8 bits 30MiB W:0 R:1 S:2 B:3
int H_x = 0, H_y = 0;
char[] line;
for(short n = 0; n < N; ++n) {
line = in.readLine().toCharArray();
for(short m = 0; m < M; ++m) {
if (line[m] == 'H') {
H_x = n;
H_y = m;
state[n][m] = 'R';
}else{
state[n][m] = line[m];
}
}
}
PriorityQueue<Node> queue = new PriorityQueue<Node>();
Node H = new Node(H_x, H_y, 0);
queue.add(H);
Node p = null;
boolean Found = false;
while(!queue.isEmpty() && !Found) {
p = queue.poll();
for(int i = 0; i < 4; ++i) {
int x = p.x + dx[i];
int y = p.y + dy[i];
if (x < 0 || x >= N || y < 0 || y >= M) continue;
if (state[x][y] == 'W') continue;
if (state[x][y] == 'S') {
out.println(p.dist + 1);
Found = true;
break;
}else if (state[x][y] == 'B') {
queue.add(new Node(x, y, p.dist + 2));
}else {
queue.add(new Node(x, y, p.dist + 1));
}
state[x][y] = 'W';
}
}
//+" ("+D.x+","+D.y+")"
out.flush();
// System.out.println((System.currentTimeMillis() - b )*1.0/1000.0+"s");
}
}