| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Poj 3669 新手求大神指教啊QAQ已经受不了了,折腾了一天啦QAQ,老是WA。。。import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main{
static int[][] plane;
static Queue<Point> queue = new LinkedList<Point>();
static int[] dx = { 1, 0, -1, 0 };
static int[] dy = { 0, 1, 0, -1 };
static boolean[][] visited;
static final int INF = 100000;
public static void main(String[] args) {
plane = new int[310][310];
visited = new boolean[310][310];
for (int i = 0; i < plane.length; i++) {
for (int j = 0; j < plane[0].length; j++) {
plane[i][j] = INF;
}
}
Scanner scan = new Scanner(System.in);
int line = scan.nextInt();
for (int i = 0; i < line; i++) {
int x = scan.nextInt();
int y = scan.nextInt();
int t = scan.nextInt();
plane[x][y] = t;
for (int j = 0; j < 4; j++) {
int xx = x + dx[j];
int yy = y + dy[j];
if (xx >= 0 && yy >= 0 && plane[xx][yy] > t) {
plane[xx][yy] = t;
}
}
}
scan.close();
queue.add(new Point(0, 0, 0));
System.out.println(bfs());
}
static int bfs() {
Point p = queue.poll();
visited[p.x][p.y] = true;
if (p.depth >= plane[p.x][p.y]) {
return -1;
}
while (p != null) {
int steps = p.depth;
if (plane[p.x][p.y] == INF) {
return steps;
}
for (int i = 0; i < 4; i++) {
int xx = p.x + dx[i];
int yy = p.y + dy[i];
if (xx >= 0 && yy >= 0 && steps + 1 < plane[xx][yy]
&& !visited[xx][yy]) {
visited[xx][yy] = true;
queue.add(new Point(xx, yy, steps + 1));
}
}
p = queue.poll();
}
return -1;
}
}
class Point {
int x;
int y;
int depth;
Point(int x, int y, int depth) {
this.x = x;
this.y = y;
this.depth = depth;
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator