编程语言:Java
题目链接:http://poj.org/problem?id=3669
题解:每个点的值为最早被流星破坏的时间,在bfs中,只需将自己能够前去的点加入队列即可。
结果:AC
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
public class Main {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static Scanner sc = new Scanner(System.in);
static int m;
static int MAX_N = 302;
static int[][] a = new int[302][302];
static int res = -1;
static int[] dx = {1, -1, 0, 0, 0};
static int[] dy = {0, 0, 1, -1, 0};
public static void main(String[] args) throws IOException {
m = sc.nextInt();
sc.nextLine();
for (int i = 0; i < MAX_N; i++) {
Arrays.fill(a[i], Integer.MAX_VALUE);
}
for (int i = 0; i < m; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
int t = sc.nextInt();
for (int j = 0; j < 5; j++) {
int ti = x + dx[j];
int tj = y + dy[j];
if (ti > -1 && ti < MAX_N & tj > -1 && tj < MAX_N)
a[ti][tj] = Math.min(a[ti][tj], t);
}
}
bfs(0, 0);
out.println(res);
out.flush();
}
private static void bfs(int x, int y) {
LinkedBlockingQueue<point> que = new LinkedBlockingQueue<point>();
boolean[][] f = new boolean[MAX_N][MAX_N];
que.add(new point(x, y, 0));
while (!que.isEmpty()) {
point p = que.poll();
if (a[p.x][p.y] == Integer.MAX_VALUE) {
res = p.step;
break;
} else {
for (int i = 0; i < 4; i++) {
int tx = p.x + dx[i];
int ty = p.y + dy[i];
if (tx > -1 && tx < MAX_N && ty > -1 && ty < MAX_N && a[tx][ty] > p.step + 1 && !f[tx][ty]) {
f[tx][ty] = true;
que.add(new point(tx, ty, p.step + 1));
}
}
}
}
}
}
class point {
int x;
int y;
int step;
public point(int x, int y, int step) {
this.x = x;
this.y = y;
this.step = step;
}
}