bfs围追堵截迷宫,带有墙的那种

信息学奥赛一本通T1250-The Castle - C语言网 (dotcpp.com)

  1. 四个数组,存放上下左右能走的方向。

  1. 手动赋值上下左右判断数组

列出墙的所有情况。

import java.util.*;

public class Main {
    static class jieDian {
        int x;
        int y;

        public jieDian(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }

        public jieDian() {

        }
    }

    static int[] shang = new int[17];
    static int[] xia = new int[17];
    static int[] zuo = new int[17];
    static int[] you = new int[17];
    static int n;
    static int m;
    static int[][] arr;
    static int[][] chek;
    static int cont = 0;
    static int max = 0;

    public static void main(String[] args) {
        inio();// 初始化判断方向的数字组,将墙的方向全部列出来,找出可以走的方向,赋值为1

        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        m = sc.nextInt();
        arr = new int[n][m];
        chek = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        // bfs
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (chek[i][j] != 1) {
                    bfs(i, j);
                    cont++;
//                    return;
                }
            }
        }
        System.out.println(cont);
        System.out.println(max+1);
    }

    private static void bfs(int a1, int a2) {
        Queue<jieDian> dui = new LinkedList<Main.jieDian>();
        dui.offer(new jieDian(a1, a2));
        int sum = 0;
        chek[a1][a2] = 1;//进来第一次,脚踩的地方,下次就不能再次到达了!!!
        while (!dui.isEmpty()) {
            int size = dui.size();
            for (int i = 0; i < size; i++) {
                Main.jieDian p = dui.poll();
                int x = p.x;
                int y = p.y;
//                if (arr[x][y] == 0) {
//                    System.out.println(shang[arr[x][y]] != 0 && x - 1 >= 0 && chek[x - 1][y] != 1);
//                    System.out.println(you[arr[x][y]] != 0 && y + 1 < m && chek[x][y + 1] != 1);
//                    System.out.println(xia[arr[x][y]] != 0 && x + 1 < n && chek[x + 1][y] != 1);
//                    System.out.println(zuo[arr[x][y]] != 0 && y - 1 >= 0 && chek[x][y - 1] != 1);
//
//                }
                // 上
                if (shang[arr[x][y]] != 0 && x - 1 >= 0 && chek[x - 1][y] != 1) {
                    sum++;
                    chek[x - 1][y] = 1;
                    dui.offer(new jieDian(x - 1, y));
//                    System.out.println("上" + 1);
                }
                // 右
                if (you[arr[x][y]] != 0 && y + 1 < m && chek[x][y + 1] != 1) {
                    sum++;
                    chek[x][y + 1] = 1;
                    dui.offer(new jieDian(x, y + 1));
//                    System.out.println("右" + 1);
                } 
                // 下
                if (xia[arr[x][y]] != 0 && x + 1 < n && chek[x + 1][y] != 1) {
                    sum++;
                    chek[x + 1][y] = 1;
                    dui.offer(new jieDian(x + 1, y));
//                    System.out.println("下" + 1);
                } 
                // 左
                if (zuo[arr[x][y]] != 0 && y - 1 >= 0 && chek[x][y - 1] != 1) {
                    sum++;
                    chek[x][y - 1] = 1;
                    dui.offer(new jieDian(x, y - 1));
//                    System.out.println("左" + 1);
                } else {
                    f4 = true;
                }
            }
        }

        max = Math.max(max, sum);

    }

    private static void inio() {
        int[][] ditu = new int[][] { { 13, 9, 12, 4, 8, 1, 5, 0 }, { 7, 3, 6, 2, 4, 1, 5, 0 },
                { 14, 6, 12, 2, 4, 8, 10, 0 }, { 11, 9, 3, 2, 8, 1, 10, 0 } };
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 8; j++) {
                if (i == 0) {
                    shang[ditu[i][j]] = 1;
                } else if (i == 1) {
                    xia[ditu[i][j]] = 1;
                } else if (i == 2) {
                    zuo[ditu[i][j]] = 1;
                } else {
                    you[ditu[i][j]] = 1;
                }
            }
        }
        // ok
//        System.out.println(Arrays.toString(shang));
//        System.out.println(Arrays.toString(xia));
//        System.out.println(Arrays.toString(zuo));
//        System.out.println(Arrays.toString(you));
    }
}

注意当前点,一定是走过了的,我已经在这个地方栽了两次了呜呜呜

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值