蓝桥杯_穿越雷区 java

题目描述

X 星的坦克战车很奇怪,它必须交替地穿越正能量辐射区和负能量辐射区才能保持正常运转,否则将报废。

某坦克需要从 A 区到 B 区去( A,B 区本身是安全区,没有正能量或负能量特征),怎样走才能路径最短?

已知的地图是一个方阵,上面用字母标出了 A,B 区,其它区都标了正号或负号分别表示正负能量辐射区。

例如:

A + - + -

- + - - +

- + + + -

+ - + - +

B + - + -

坦克车只能水平或垂直方向上移动到相邻的区。

输入描述

第一行是一个整数 n,表示方阵的大小, 4≤n<100。

接下来是 n 行,每行有 n 个数据,可能是 A,B,+,- 中的某一个,中间用空格分开。A,B 都只出现一次。

输出描述

输出一个整数,表示坦克从 A 区到 B 区的最少移动步数。

如果没有方案,则输出 -1。

输入输出样例

示例
输入
5
A + - + -
- + - - +
- + + + -
+ - + - +
B + - + -
输出
10

运行限制

  • 最大运行时间:1s

  • 最大运行内存: 512M

常规bfs题目

代码:

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

public class 穿越雷区 {

    static int n, startX, startY, step = 0;//方阵大小,开始位置,步数
    static char[][] map;//地图
    static boolean[][] vis;//记录访问数组
    static int[][] dir = { { -1, 0 }, { 0, 1 }, { 1, 0 }, { 0, -1 } };//方向数组 上,右,下,左

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        n = scanner.nextInt();
        map = new char[n][n];
        vis = new boolean[n][n];
        //初始化数组
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = scanner.next().charAt(0);
                //记录开始位置
                if (map[i][j] == 'A') {
                    startX = i;
                    startY = j;
                }
            }
        }
        //调用bfs
        bfs();
    }

    private static void bfs() {
        //初始化队列
        Queue<Node> queue = new LinkedList<>();
        queue.offer(new Node(startX, startY));
        while (!queue.isEmpty()) {
            int len = queue.size();
            while (len-- > 0) {
                //取队首
                Node tNode = queue.poll();
                //出口
                if (map[tNode.x][tNode.y] == 'B') {
                    System.out.println(step);
                    return;
                }
                //向邻居四个方向遍历
                for (int i = 0; i < dir.length; i++) {
                    int tx = tNode.x + dir[i][0];
                    int ty = tNode.y + dir[i][1];
                    //防止越界
                    if (tx >= 0 && tx < n && ty >= 0 && ty < n && vis[tx][ty] == false) {
                        //不能与上一次走的一致
                        if (map[tx][ty] != map[tNode.x][tNode.y]) {
                            //标记为已访问
                            vis[tx][ty] = true;
                            //入队列
                            queue.offer(new Node(tx, ty));
                        }
                    }
                }
            }
            //向邻居遍历完步数就加1
            step++;
        }
        //当退出循环时(即队列为空时)还没有找到就输出-1
        System.out.println(-1);
    }
}

class Node {
    int x;
    int y;

    public Node(int x, int y) {
        super();
        this.x = x;
        this.y = y;
    }
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的 Java 代码示例,用于生成一个包含随机雷的二维雷区,每个格子用数字表示周围雷的数量: ```java import java.util.Random; public class MineSweeper { private int[][] board; private int row; private int col; private int numMines; public MineSweeper(int row, int col, int numMines) { this.row = row; this.col = col; this.numMines = numMines; this.board = new int[row][col]; generateMines(); countMines(); } private void generateMines() { Random rand = new Random(); int mines = numMines; while (mines > 0) { int r = rand.nextInt(row); int c = rand.nextInt(col); if (board[r][c] != -1) { board[r][c] = -1; mines--; } } } private void countMines() { for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { if (board[r][c] == -1) { continue; } int count = 0; if (r > 0 && c > 0 && board[r-1][c-1] == -1) { count++; } if (r > 0 && board[r-1][c] == -1) { count++; } if (r > 0 && c < col-1 && board[r-1][c+1] == -1) { count++; } if (c > 0 && board[r][c-1] == -1) { count++; } if (c < col-1 && board[r][c+1] == -1) { count++; } if (r < row-1 && c > 0 && board[r+1][c-1] == -1) { count++; } if (r < row-1 && board[r+1][c] == -1) { count++; } if (r < row-1 && c < col-1 && board[r+1][c+1] == -1) { count++; } board[r][c] = count; } } } public void printBoard() { for (int r = 0; r < row; r++) { for (int c = 0; c < col; c++) { if (board[r][c] == -1) { System.out.print("* "); } else { System.out.print(board[r][c] + " "); } } System.out.println(); } } public static void main(String[] args) { MineSweeper game = new MineSweeper(5, 5, 5); game.printBoard(); } } ``` 示例输出: ``` 0 1 1 * 2 2 * 3 3 * * 4 * 4 3 2 * 3 * 2 1 1 2 1 0 ``` 其中,-1 表示一个雷,其余数字表示周围雷的数量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值