HDU 1072 BFS+优先队列 Java版

 Problem - 1072 (hdu.edu.cn)https://acm.hdu.edu.cn/showproblem.php?pid=1072

这个题目大致的含义是,用数字表示地图,0表示墙壁,不可走;1表示平地可以在上面行走;2表示起点;3表示终点;4也是可以走的位置,但是有点不同,这个题目的背景是炸弹在六秒内爆炸,如果在六秒内能到达终点(第六秒到达终点视为失败),如果在六秒内不能到达终点,但是可以到4的位置,到达4的位置以后,炸弹的倒计时重新变成六秒,这样也是可以的,所以4这个位置有重置炸弹时间的意思。

BFS的思路:使用到优先队列,当前这个点的步数(从起点算起)小的优先,然后到达4以后,需要把4重置为0,即4这个点原则上只能来一次,因为如果重复的来4,虽然炸弹也不会爆炸,但是对于解题没有意义。其他的基本上就是普通的BFS的思路了。

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;


public class Main {

    int T, m, n, start_x, start_y;
    int[][] map;
    PriorityQueue<Node> queue;
    int[] fx = {1, 0, 0, -1};//x是0的时候,左右移动
    int[] fy = {0, -1, 1, 0};
    Node node;

    public static void main(String[] args) {
        new Main().solve();
    }

    public void solve() {
        Scanner in = new Scanner(System.in);
        T = in.nextInt();
        while ((T--) != 0) {
            //n行
            n = in.nextInt();
            //m列
            m = in.nextInt();
            map = new int[n][m];
            queue = new PriorityQueue<>(cNode);
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    map[i][j] = in.nextInt();
                    if (map[i][j] == 2) {
                        start_x = i;
                        start_y = j;
                    }
                }
            }
            //开始走迷宫
            node = new Node(start_x, start_y, 0, 0);
            queue.add(node);
            if (BFS()) {
                System.out.println(node.step + 1);
            } else {
                System.out.println(-1);
            }

        }
    }

    private boolean BFS() {
        while (!queue.isEmpty()) {
            node = queue.poll();
            for (int i = 0; i < 4; i++) {
                int x_ = node.x + fx[i];
                int y_ = node.y + fy[i];
                int time_ = node.time + 1;
                int step_ = node.step + 1;
                Node t = new Node(x_, y_, step_, time_);
                if (check(t)) {
                    if (map[x_][y_] == 3) {
                        return true;
                    }
                    queue.add(t);
                }
            }
        }
        return false;
    }

    private boolean check(Node t) {
        if (t.x < 0 || t.y < 0 || t.x > n - 1 || t.y > m - 1 || map[t.x][t.y] == 0)
            return false;
        if (t.time == 6)
            return false;
        if (map[t.x][t.y] == 4) {
            t.time = 0;
            map[t.x][t.y] = 0;
        }
        return true;
    }

    //step较小的先返回
    static Comparator<Node> cNode = new Comparator<Node>() {
        public int compare(Node o1, Node o2) {
            return o1.step - o2.step;
        }

    };

}

class Node {
    //x,y记录位置
    //step记录步数
    //time记录时间,大于等于6秒的不行
    int x;
    int y;
    int step;
    int time;

    public Node(int x, int y, int step, int time) {
        this.x = x;
        this.y = y;
        this.step = step;
        this.time = time;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值