蓝桥杯之穿越雷区

蓝桥杯之穿越雷区_第1张图片
蓝桥杯之穿越雷区_第2张图片
广度优先算法:

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class LanQiao29 {
     
    static boolean[][] isVisted;
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        char[][] board = new char[n][n];
        isVisted = new boolean[n][n];
        int row = 0;
        int column = 0;
        for (int i = 0; i < n; i++) {
     
            for (int j = 0; j < n; j++) {
     
                board[i][j] = sc.next().charAt(0);
                if (board[i][j] == 'A') {
     //记录'A'所在的行和列
                    row = i;
                    column = j;
                }
            }
        }
        
        bfs(board, row, column, n);
        /*
        int[][] next = {
     {-1, 0}, {0, -1}, {1, 0}, {0, 1}};//next数组用来表示方向,即往哪里移动
        bfs1(board, next, row, column, n);
        */
    }
    public static void bfs(char[][] board, int row, int column, int n) {
     
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{
     row, column, 0});//行、列、步数
        int temp = 0;
        while (!queue.isEmpty()) {
     
            int[] arr = queue.poll();
            int i = arr[0];
            int j = arr[1];
            int curStep = arr[2];
            isVisted[i][j] = true;
            if ((i - 1) >= 0 && !isVisted[i - 1][j] && board[i][j] != board[i - 1][j]) {
     
                queue.add(new int[]{
     i - 1, j, curStep + 1});
            }
            if ((j - 1) >= 0 && !isVisted[i][j - 1] && board[i][j] != board[i][j - 1]) {
     
                queue.add(new int[]{
     i, j - 1, curStep + 1});
            }
            if ((i + 1) < n && !isVisted[i + 1][j] && board[i][j] != board[i + 1][j]) {
     
                queue.add(new int[]{
     i + 1, j, curStep + 1});
            }
            if ((j + 1) < n && !isVisted[i][j + 1] && board[i][j] != board[i][j + 1]) {
     
                queue.add(new int[]{
     i, j + 1, curStep + 1});
            }
            if (board[i][j] == 'B') {
     
                System.out.println(curStep);
                return;
            }
        }
        System.out.println(-1);
    }
    //other version
    public static void bfs1(char[][] board, int[][] next, int row, int column, int n) {
     
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{
     row, column, 0});//行、列、步数
        while (!queue.isEmpty()) {
     
            int[] arr = queue.poll();
            int i = arr[0];
            int j = arr[1];
            int curStep = arr[2];
            isVisted[i][j] = true;
            for (int k = 0; k < 4; k++) {
     
                int nextX = i + next[k][0];
                int nextY = j + next[k][1];
                if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= n) {
     
                    continue;
                }
                if (!isVisted[nextX][nextY] && board[i][j] != board[nextX][nextY]) {
     
                    queue.add(new int[]{
     nextX, nextY, curStep + 1});
                }
            }
            if (board[i][j] == 'B') {
     
                System.out.println(curStep);
                return;
            }
        }
        System.out.println(-1);
    }
}

深度优先算法:

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class LanQiao29 {
     
    static int res = Integer.MAX_VALUE;
    static int step = 0;
    static boolean[][] isVisted;
    public static void main(String[] args) {
     
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        char[][] board = new char[n][n];
        isVisted = new boolean[n][n];
        int row = 0;
        int column = 0;
        for (int i = 0; i < n; i++) {
     
            for (int j = 0; j < n; j++) {
     
                board[i][j] = sc.next().charAt(0);
                if (board[i][j] == 'A') {
     //记录'A'所在的行和列
                    row = i;
                    column = j;
                }
            }
        }
        
        dfs(board, row, column, n);
        if (res == Integer.MAX_VALUE) {
     
            System.out.println(-1);
        } else {
     
            System.out.println(res);
        }
        /*
        int[][] next = {
     {-1, 0}, {0, -1}, {1, 0}, {0, 1}};//next数组用来表示方向,即往哪里移动
        dfs1(board, next, row, column, n);
        System.out.println(res);
        */
    }

    public static void dfs(char[][] board, int i, int j, int n) {
     
        if (board[i][j] == 'B') {
     
            res = Integer.min(res, step);
            return;
        }
        isVisted[i][j] = true;
        step++;
        if ((i - 1) >= 0 && !isVisted[i - 1][j] && board[i][j] != board[i - 1][j]) {
     
            dfs(board, i - 1, j, n);
        }
        if ((j - 1) >= 0 && !isVisted[i][j - 1] && board[i][j] != board[i][j - 1]) {
     
            dfs(board, i, j - 1, n);
        }
        if ((i + 1) < n && !isVisted[i + 1][j] && board[i][j] != board[i + 1][j]) {
     
            dfs(board, i + 1, j, n);
        }
        if ((j + 1) < n && !isVisted[i][j + 1] && board[i][j] != board[i][j + 1]) {
     
            dfs(board, i, j + 1, n);
        }
        isVisted[i][j] = false;
        step--;
    }
    //other version
    public static void dfs1(char[][] board, int[][] next, int i, int j, int n) {
     
        isVisted[i][j] = true;
        if (board[i][j] == 'B') {
     
            res = Integer.min(res, step);
            return;
        }
        for (int k = 0; k < 4; k++) {
     
            int nextX = i + next[k][0];
            int nextY = j + next[k][1];
            if (nextX < 0 || nextX >= n || nextY < 0 || nextY >= n) {
     
                continue;
            }
            if (!isVisted[nextX][nextY] && board[i][j] != board[nextX][nextY]) {
     
                //isVisted[nextX][nextY] = true;//可以有可以没有
                step++;
                dfs1(board, next, nextX, nextY, n);
                isVisted[nextX][nextY] = false;
                step--;
            }
        }
    }   
}

你可能感兴趣的:(算法)