聚餐地计算(华为od机考题)

一、题目

1.原题

小华和小为是很要好的朋友,他们约定周末一起吃饭。

通过手机交流,

他们在地图上选择了多个聚餐地点

(由于自然地形等原因,部分聚餐地点不可达),

求小华和小为都能到达的聚餐地点有多少个?

2.题目理解

考点:[广搜, 矩阵, 并查集]

二、思路与代码过程

1.思路

输入:

地图map(包含餐厅1,可移动空间0,障碍物-1);

小华和小为出发位置。

计算过程:

使用队列初始存储出发位置,对方向数组进行遍历,(BFS),得到可以抵达的餐厅的位置数组表,将小华和小为的进行相同值筛选后得到都可以抵达的餐厅位置数组,对数组长度进行输出即可。

2.代码过程

①main函数

复制代码
public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入地图长宽:");
        int m = sc.nextInt();
        int n = sc.nextInt();
        System.out.println("请输入地图(0:正常通行;-1:障碍;1:餐厅):");
        int[][] map = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = sc.nextInt();
            }
        }
        System.out.println("请输入小华位置:");
        int[] posH = new int[2];
        posH[0] = sc.nextInt();
        posH[1] = sc.nextInt();
        System.out.println("请输入小为位置:");
        int[] posW = new int[2];
        posW[0] = sc.nextInt();
        posW[1] = sc.nextInt();
        ArrayList<int[]>HCr = CountArrival(posH,map);
        ArrayList<int[]>WCr = CountArrival(posW,map);
        ArrayList<int[]>HWCR = CalBothCan(HCr,WCr);
        System.out.println("小华和小为都能到达的聚餐地点有:"+HWCR.size()+"个。");
    }

②CalBothCan

复制代码
private static ArrayList<int[]> CalBothCan(ArrayList<int[]> hcr, ArrayList<int[]> wcr) {
        ArrayList<int[]> BR = new ArrayList<>();
        while (BR.size() < Math.min(hcr.size(), wcr.size())) {
            for (int[] i : hcr) {
                for (int[] j : wcr) {
                    if (Arrays.equals(i, j)) {
                        BR.add(i);
                    }
                }
            }
        }
        return BR;
    }

③方向数组

复制代码
static int[][] DIRECTION ={{0,1},{1,0},{0,-1},{-1,0}};//方向数组

④CountArrival

复制代码
private static ArrayList<int[]> CountArrival(int[] pos, int[][] map) {
        boolean[][] visited = new boolean[map.length][map[0].length];//抵达标记防止重复
        Queue<int[]> Q = new LinkedList<>();
        Q.add(pos);
        visited[pos[0]][pos[1]] = true;
        ArrayList<int[]> CR = new ArrayList<>();

        while(!Q.isEmpty()) {
            int[] cur = Q.poll();
            visited[cur[0]][cur[1]] = true;
            for (int[] direction : DIRECTION) {
                int x = cur[0] + direction[0];
                int y = cur[1] + direction[1];
                if (x >= 0 && y >= 0 && x < map.length && y < map[0].length&&!visited[x][y]) {//在边界内
                    if (map[x][y] == 0) {
                        Q.add(new int[]{x,y});
                        visited[x][y] = true;
                    }
                    if (map[x][y] == 1) {
                        Q.add(new int[]{x,y});
                        CR.add(new int[]{x,y});
                        visited[x][y] = true;
                    }
                    if (map[x][y] == -1) {
                        continue;
                    }
                }
            }
        }
        return CR;
    }

三、运行结果

1.运行截图

2.完整代码

复制代码
import java.util.*;

public class test39 {
    public static void main(String[] args) {
        ///*
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入地图长宽:");
        int m = sc.nextInt();
        int n = sc.nextInt();
        System.out.println("请输入地图(0:正常通行;-1:障碍;1:餐厅):");
        int[][] map = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = sc.nextInt();
            }
        }
        //System.out.println(Arrays.deepToString(map));
        System.out.println("请输入小华位置:");
        int[] posH = new int[2];
        posH[0] = sc.nextInt();
        posH[1] = sc.nextInt();
        System.out.println("请输入小为位置:");
        int[] posW = new int[2];
        posW[0] = sc.nextInt();
        posW[1] = sc.nextInt();
       //*/
        /*
        int[][] map = {{0,-1,-1,1},{0,1,-1,0},{-1,0,1,0},{0,-1,0,0}};
        int[] posH = {0,0};
        int[] posW = {3,3};
         */
        ArrayList<int[]>HCr = CountArrival(posH,map);
        ArrayList<int[]>WCr = CountArrival(posW,map);
        ArrayList<int[]>HWCR = CalBothCan(HCr,WCr);
        System.out.println("小华和小为都能到达的聚餐地点有:"+HWCR.size()+"个。");
    }

    private static ArrayList<int[]> CalBothCan(ArrayList<int[]> hcr, ArrayList<int[]> wcr) {
        ArrayList<int[]> BR = new ArrayList<>();
        while (BR.size() < Math.min(hcr.size(), wcr.size())) {
            for (int[] i : hcr) {
                for (int[] j : wcr) {
                    if (Arrays.equals(i, j)) {
                        BR.add(i);
                    }
                }
            }
        }
        return BR;
    }

    static int[][] DIRECTION ={{0,1},{1,0},{0,-1},{-1,0}};//方向数组

    private static ArrayList<int[]> CountArrival(int[] pos, int[][] map) {
        boolean[][] visited = new boolean[map.length][map[0].length];//抵达标记防止重复
        Queue<int[]> Q = new LinkedList<>();
        Q.add(pos);
        visited[pos[0]][pos[1]] = true;
        ArrayList<int[]> CR = new ArrayList<>();

        while(!Q.isEmpty()) {
            int[] cur = Q.poll();
            visited[cur[0]][cur[1]] = true;
            for (int[] direction : DIRECTION) {
                int x = cur[0] + direction[0];
                int y = cur[1] + direction[1];
                if (x >= 0 && y >= 0 && x < map.length && y < map[0].length&&!visited[x][y]) {//在边界内
                    if (map[x][y] == 0) {
                        Q.add(new int[]{x,y});
                        visited[x][y] = true;
                    }
                    if (map[x][y] == 1) {
                        Q.add(new int[]{x,y});
                        CR.add(new int[]{x,y});
                        visited[x][y] = true;
                    }
                    if (map[x][y] == -1) {
                        continue;
                    }
                }
            }
        }
        /*
        System.out.println("\n可抵达餐厅有:");
        for (int[] element : CR) {
            System.out.print(Arrays.toString(element));
        }
        System.out.println();
         */
        return CR;
    }
}
相关推荐
不是吧这都有重名15 分钟前
[论文阅读]Deeply-Supervised Nets
论文阅读·人工智能·算法·大语言模型
homelook20 分钟前
matlab simulink双边反激式变压器锂离子电池均衡系统,双目标均衡策略,仿真模型,提高均衡速度38%
算法
什码情况1 小时前
星际篮球争霸赛/MVP争夺战 - 华为OD机试真题(A卷、Java题解)
java·数据结构·算法·华为od·面试·机试
天上路人1 小时前
采用AI神经网络降噪算法的通信语音降噪(ENC)模组性能测试和应用
人工智能·神经网络·算法
AA-代码批发V哥1 小时前
正则表达式: 从基础到进阶的语法指南
java·开发语言·javascript·python·正则表达式
字节高级特工1 小时前
【C++】”如虎添翼“:模板初阶
java·c语言·前端·javascript·c++·学习·算法
.Vcoistnt1 小时前
Codeforces Round 1024 (Div. 2)(A-D)
数据结构·c++·算法·贪心算法·动态规划·图论
晴天下小雨o1 小时前
排序算法总结
java·算法·排序算法
曼岛_1 小时前
[Java实战]Spring Boot 整合 Redis(十八)
java·spring boot·redis
向哆哆2 小时前
Netty在Java网络编程中的应用:实现高性能的异步通信
java·网络·php