机器人走迷宫

标题:机器人走迷宫 | 时间限制:1秒 | 内存限制:65536K | 语言限制:不限
1、 房间由X*Y的方格组成,例如下图为6*4的大小。每一个方格以坐标(x,y)描述。
2、 机器人固定从方格(0,0)出发,只能向东或者向北前进。出口固定为房间的最东北角,如下图的方格(5,3)。用例保证机器人可以从入口走到出口。


import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int xNum = scanner.nextInt();
        int yNum = scanner.nextInt();
        int[][] map = new int[xNum][yNum];
        int n = scanner.nextInt();
        while (n > 0) {
            int wallX = scanner.nextInt();
            int wallY = scanner.nextInt();
            map[wallX][wallY] = 1;
            n--;
        }
        path(map, 0, 0, xNum - 1, yNum - 1);
        int trap = 0;
        int unReachable = 0;
        for (int i = 0; i < xNum; i++) {
            for (int j = 0; j < yNum; j++) {
                if (map[i][j] == 2) {
                    trap++;
                }
                if (map[i][j] == 0) {
                    unReachable++;
                }
            }
        }
        System.out.println(trap + " " + unReachable);
    }

    public static void path(int[][] room, int nextStepX, int nextStepY, int x, int y) {
        if (room[nextStepX][nextStepY] == 1) {
            return;
        }
        if (room[nextStepX][nextStepY] != 0) {
            return;
        }
        if (nextStepX == x && nextStepY == y) {
            room[nextStepX][nextStepY] = -1;
            return;
        }
        if (nextStepX < x) {
            path(room, nextStepX + 1, nextStepY, x, y);
        }
        if (nextStepY < y) {
            path(room, nextStepX, nextStepY + 1, x, y);
        }

        if (nextStepX == x || nextStepY == y) {
            if (nextStepX == x && nextStepY < y && room[nextStepX][nextStepY + 1] != -1) {
                room[nextStepX][nextStepY] = 2;
            } else if (nextStepY == y && nextStepX < x && room[nextStepX + 1][nextStepY] != -1) {
                room[nextStepX][nextStepY] = 2;
            } else {
                room[nextStepX][nextStepY] = -1;
            }
        } else if (room[nextStepX + 1][nextStepY] != -1 && room[nextStepX][nextStepY + 1] != -1) {
            room[nextStepX][nextStepY] = 2;
        } else {
            room[nextStepX][nextStepY] = -1;
        }
    }
}



全部评论
function maze () { let cell_n = '6 4'.split(" "); //格数 let wall = ['0 2', '1 2', '2 2', '4 1', '5 1']; let trap = 2; // 陷阱 let no_arrive = 3; // 不可达的 let trap_a = []; let arrive_a = []; //不可达的坐标 for (let i = cell_n[0] - 1; i > cell_n[0] - 1 - trap; i--) { trap_a.push(i + " " + '0') } for (let i = 0; i < no_arrive; i++) { arrive_a.push(i + " " + (cell_n[1] - 1)) } let forbid = [...wall, ...trap_a, ...arrive_a]; // 开始走格数 let init = "0 0"; let line = []; for (let i = 0; i < cell_n[0]; i++) { for (let j = 0; j < cell_n[1]; j++) { if (!forbid.includes(i + ' ' + j) && (i >= init.split(' ')[0] && j >= init.split(' ')[1])) { console.log(i + ' ' + j) init = i + ' ' + j; line.push(i + ' ' + j) } } } console.log(line) } maze()
点赞 回复 分享
发布于 2024-03-21 14:41 浙江

相关推荐

用微笑面对困难:你出于礼貌叫了人一声大姐,大姐很欣慰,她真把你当老弟
点赞 评论 收藏
分享
评论
2
7
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务