POJ 3669 Meteor Shower

编程语言: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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值