2023华为OD机试 (B卷)|200分 疫情扩散时间计算(Java JavaScript C++ Python

2023华为OD机试 (B卷)|200分 疫情扩散时间计算(Java JavaScript C++ Python)
题目描述

  • 在一个地图中(地图有N*N个区域组成),有部分区域被感染病菌。
  • 感染区每天都会把周围(上下左右)的4个区域感染。
  • 请根据给定的地图计算,多少天后,全部区域都会被感染。 如果初始地图上所有区域全部都被感染,或者没有被感染,返回-1

输入描述

一行N*N个数字(只包含0,1,不会有其他数字)表示一个地图,数字间用“”分割,0表示未感染区域,1表示已经感染区域
N个数字表示地图中一行,输入数据共表示N行N列的区域地图。 例如输入1,0,1,0,0,0,1,0,1,表示地图

101
000
101

输出描述

一个整数,表示经过多少天后,全部区域都被感染1 <=N <= 200

用例

输入1,0,1,0,0,0,1,0,1
输出2
说明一天以后,地图中仅剩余中心点未被感染,2天后,全部被感染
输入1,1,1,1,1,1,1,1,1
输出-1
说明全部被感染

Java

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        List<Integer> map = new ArrayList<>();
        int pos = 0;
        String token;
        while ((pos = input.indexOf(",")) != -1) { // 将输入字符串转换为一维数组
            token = input.substring(0, pos);
            map.add(Integer.parseInt(token));
            input = input.substring(pos + 1);
        }
        map.add(Integer.parseInt(input));
        System.out.println(getResult(map)); // 输出感染天数
    }

    public static int getResult(List<Integer> map) {
        int n = (int) Math.sqrt(map.size());

        int[][] matrix = new int[n][n]; // 将一维数组转换为二维矩阵

        Queue<int[]> q = new LinkedList<>(); // 用队列存储感染区域

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = map.get(i * n + j); // 将一维数组转换为二维矩阵
                if (matrix[i][j] == 1) q.offer(new int[]{i, j}); // 将感染区域加入队列
            }
        }

        if (q.isEmpty() || q.size() == map.size()) { // 判断特殊情况
            return -1;
        }

        int healthy = map.size() - q.size(); // 记录未感染区域数量

        int[][] offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 记录四个方向的偏移量

        int day = 0; // 记录感染天数
        while (!q.isEmpty() && healthy > 0) { // 当队列不为空且还有未感染区域时,进行循环
            int[] tmp = q.poll(); // 取出队首元素
            int x = tmp[0], y = tmp[1]; // 获取队首元素的坐标
            day = matrix[x][y] + 1; // 记录感染天数

            for (int[] offset : offsets) { // 遍历四个方向
                int newX = x + offset[0]; // 新的横坐标
                int newY = y + offset[1]; // 新的纵坐标

                if (newX < 0 || newX >= n || newY < 0 || newY >= n) continue; // 判断边界

                if (matrix[newX][newY] == 0) { // 如果该区域未感染
                    healthy--; // 未感染区域数量减一
                    matrix[newX][newY] = day; // 标记该区域已感染
                    q.offer(new int[]{newX, newY}); // 将该区域加入队列
                }
            }
        }

        return day - 1; // 返回感染天数
    }
}


Javascript

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function getResult(map) {
  const n = Math.sqrt(map.length);

  const matrix = new Array(n).fill().map(() => new Array(n)); // 将一维数组转换为二维矩阵

  const q = []; // 用队列存储感染区域

  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n; j++) {
      matrix[i][j] = map[i * n + j]; // 将一维数组转换为二维矩阵
      if (matrix[i][j] === 1) q.push([i, j]); // 将感染区域加入队列
    }
  }

  if (q.length === 0 || q.length === map.length) { // 判断特殊情况
    return -1;
  }

  let healthy = map.length - q.length; // 记录未感染区域数量

  const offsets = [[-1, 0], [1, 0], [0, -1], [0, 1]]; // 记录四个方向的偏移量

  let day = 0; // 记录感染天数
  while (q.length > 0 && healthy > 0) { // 当队列不为空且还有未感染区域时,进行循环
    const [x, y] = q.shift(); // 取出队首元素
    day = matrix[x][y] + 1; // 记录感染天数

    for (const offset of offsets) { // 遍历四个方向
      const [dx, dy] = offset;
      const newX = x + dx; // 新的横坐标
      const newY = y + dy; // 新的纵坐标

      if (newX < 0 || newX >= n || newY < 0 || newY >= n) continue; // 判断边界

      if (matrix[newX][newY] === 0) { // 如果该区域未感染
        healthy--; // 未感染区域数量减一
        matrix[newX][newY] = day; // 标记该区域已感染
        q.push([newX, newY]); // 将该区域加入队列
      }
    }
  }

  return day - 1; // 返回感染天数
}

rl.on('line', (input) => {
  const map = input.split(',').map(Number);
  console.log(getResult(map));
});

C++
#include
#include
#include
#include
using namespace std;

int getResult(vector& map) {
int n = sqrt(map.size());

vector<vector<int>> matrix(n, vector<int>(n)); // 将一维数组转换为二维矩阵

queue<pair<int, int>> q; // 用队列存储感染区域

for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        matrix[i][j] = map[i * n + j]; // 将一维数组转换为二维矩阵
        if (matrix[i][j] == 1) q.push({i, j}); // 将感染区域加入队列
    }
}

if (q.empty() || q.size() == map.size()) { // 判断特殊情况
    return -1;
}

int healthy = map.size() - q.size(); // 记录未感染区域数量

vector<vector<int>> offsets = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 记录四个方向的偏移量

int day = 0; // 记录感染天数
while (!q.empty() && healthy > 0) { // 当队列不为空且还有未感染区域时,进行循环
    pair<int, int> tmp = q.front(); // 取出队首元素
    q.pop(); // 弹出队首元素
    int x = tmp.first, y = tmp.second; // 获取队首元素的坐标
    day = matrix[x][y] + 1; // 记录感染天数

    for (vector<int>& offset : offsets) { // 遍历四个方向
        int newX = x + offset[0]; // 新的横坐标
        int newY = y + offset[1]; // 新的纵坐标

        if (newX < 0 || newX >= n || newY < 0 || newY >= n) continue; // 判断边界

        if (matrix[newX][newY] == 0) { // 如果该区域未感染
            healthy--; // 未感染区域数量减一
            matrix[newX][newY] = day; // 标记该区域已感染
            q.push({newX, newY}); // 将该区域加入队列
        }
    }
}

return day - 1; // 返回感染天数

}

int main() {
string input;
getline(cin, input);
vector map;
size_t pos = 0;
string token;
while ((pos = input.find(“,”)) != string::npos) { // 将输入字符串转换为一维数组
token = input.substr(0, pos);
map.push_back(stoi(token));
input.erase(0, pos + 1);
}
map.push_back(stoi(input));
cout << getResult(map) << endl; // 输出感染天数
return 0;
}

python

import math
from queue import Queue

def get_result(map):
    n = int(math.sqrt(len(map)))
    matrix = [[0 for j in range(n)] for i in range(n)]
    q = Queue()
    for i in range(n):
        for j in range(n):
            matrix[i][j] = map[i * n + j]
            if matrix[i][j] == 1:
                q.put((i, j))
    if q.empty() or q.qsize() == len(map):
        return -1
    healthy = len(map) - q.qsize()
    offsets = [[-1, 0], [1, 0], [0, -1], [0, 1]]
    day = 0
    while not q.empty() and healthy > 0:
        tmp = q.get()
        x, y = tmp[0], tmp[1]
        day = matrix[x][y] + 1
        for offset in offsets:
            new_x = x + offset[0]
            new_y = y + offset[1]
            if new_x < 0 or new_x >= n or new_y < 0 or new_y >= n:
                continue
            if matrix[new_x][new_y] == 0:
                healthy -= 1
                matrix[new_x][new_y] = day
                q.put((new_x, new_y))
    return day - 1

input_str = input()
input_list = list(map(int, input_str.split(",")))
print(get_result(input_list))

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 很高兴能够参加2023华为OD机试(B)。以下是我的回答: 首先,对于我作为一名技术爱好者和计算机科学专业的学生来说,华为OD机试是我展示自己技能和提升职业能力的绝佳机会。我热爱挑战自己,希望能够通过这次机试来展示我的技术水平和解决问题的能力。 另外,作为一家世界知名的科技公司,华为以其创新能力和领导地位而闻名。参加华为OD机试,我希望能够学习到尖端的技术和最佳的工程实践。通过与华为的工程师们交流和学习,我相信我将能够进一步提升自己,掌握更多的技术和知识。 此外,参加华为OD机试也是为了追求个人职业发展的机会。华为作为一家全球性的科技公司,有广阔的市场和众多的职业发展机会。通过参加华为OD机试,我希望能够获得华为的认可,并有机会加入华为,与卓越的团队一起工作,共同推动公司的发展和创新。 最后,我相信参加华为OD机试将是一次富有挑战性和有意义的经历。我会认真准备,研究和了解华为的相关业务和技术要求。我相信通过自己的努力和准备,我将能够展现出自己的实力和能力,为华为做出贡献。 总而言之,参加2023华为OD机试(B)对我而言是一个重要的机会。我将全力以赴,准备充,希望在机试中有出色的表现。我相信通过这次机试,我将能够展现自己的能力,进一步提升自己,并为华为的发展贡献力量。 ### 回答2: 2023华为OD机试(B)是华为公司在2023年举行的一次线上机试。作为华为公司的OD机试,它将通过互联网的方式进行远程评估,为应聘者提供了参与华为招聘的机会。 华为OD机试(B)相对于其他别可能会有一些特殊设计或者难度更高的问题。应聘者需要准备好面对各种类型的算法和编程问题,以及一些与华为相关的项目经验。 首先,准备方面,应聘者需掌握基本的数据结构和算法,熟悉常见的算法题型,如搜索、排序、动态规划等。此外,对于华为相关的项目和技术,应聘者需要了解并掌握相关的核心知识和实践经验。 其次,在机试过程中,要注意时间的合理安排和控制。机试通常时间较紧,应聘者需要快速理解问题,思考解决方案,并高效地实现代码。同时,要注重代码的可读性和优化,保证代码的正确性和效率。 最后,在OD机试结束后,应聘者还可以了解并掌握自己在机试中的不足之处,进一步提升自己的技术能力。同时,可以多关注和了解一些与华为相关的技术和行业动态,为今后的发展做好准备。 总之,参加2023华为OD机试(B)是一次宝贵的机会,应聘者需要提前做好充的准备,掌握技术知识和项目经验,合理安排时间,在机试中发挥自己的优势,同时也要及时总结经验教训,为未来的发展做好准备。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值