当前位置: 首页 > news >正文

西安成品网站建设山东建站

西安成品网站建设,山东建站,做博客网站最好用什么系统,盐城中瑞做网站公司题目描述 宝宝和妈妈参加亲子游戏,在一个二维矩阵(NN)的格子地图上,宝宝和妈妈抽签决定各自的位置,地图上每个格子有不同的糖果数量,部分格子有障碍物。 游戏规则是妈妈必须在最短的时间(每个单…

题目描述

宝宝和妈妈参加亲子游戏,在一个二维矩阵(NN)的格子地图上,宝宝和妈妈抽签决定各自的位置,地图上每个格子有不同的糖果数量,部分格子有障碍物。
游戏规则是妈妈必须在最短的时间(每个单位时间只能走一步)到达宝宝的位置,路上的所有糖果都可以拿走,不能走障碍物的格子,只能上下左右走。
请问妈妈在最短到达宝宝位置的时间内最多拿到多少糖果(优先考虑最短时间到达的情况下尽可能多拿糖果)。
输入描述
第一行输入为N,N标识二维矩阵的大小
之后N行,每行有N个值,表格矩阵每个位置的值
其中:
-3:妈妈
-2:宝宝
-1:障碍
=0:糖果数(0表示没有糖果,但是可以走)
输出描述
输出妈妈在最短到达宝宝位置的时间内最多拿到多少糖果,行末无多余空格
示例1
输入输出示例仅供调试,后台判题数据一般不包含示例
输入
4
3 2 1 -3
1 -1 1 1
1 1 -1 2
-2 1 2 3
输出
9
说明
在这里插入图片描述
此地图有两条最短路径可到宝宝位置,绿色线和黄色线都是最短路径6步,但黄色拿到的糖果更多,9个
示例2
输入输出示例仅供调试,后台判题数据一般不包含示例
输入
4
3 2 1 -3
-1 -1 1 1
1 1 -1 2
-2 1 -1 3
输出
-1
说明
在这里插入图片描述
此地图妈妈无法到达宝宝位置
备注
地图最大50
50

解题思路

解题思路主要包括两个步骤:

  1. 寻找最短路径: 使用深度优先搜索(DFS)或广度优先搜索(BFS)来找到妈妈到宝宝位置的所有最短路径。在这个过程中,需要注意不走障碍物且避免重复访问同一个位置。在找到路径的同时,记录路径的长度。

  2. 计算最多糖果: 遍历所有找到的最短路径,计算每条路径上经过的糖果总数,选择最大的糖果总数作为结果。

以下是详细的解题思路:

  • 从输入中读取地图大小 N 和地图矩阵 arr。
  • 使用深度优先搜索(DFS)或广度优先搜索(BFS)找到妈妈到宝宝位置的最短路径。在搜索的过程中,记录路径的长度和路径上的所有点。
  • 遍历所有找到的最短路径,计算每条路径上经过的糖果总数。
  • 输出最大的糖果总数作为结果。

题解代码

Python题解代码

class PP:def __init__(self, x, y):self.x = xself.y = yg_pts = []  # 存储所有可能的路径
g_len = 30000  # 初始化最短路径长度
dx = [0, 0, 1, -1]  # 方向数组,用于搜索路径
dy = [1, -1, 0, 0]def get_path(arr, n, begin, end, p):global g_pts, g_lenif begin.x >= n or begin.x < 0 or begin.y >= n or begin.y < 0 or arr[begin.x][begin.y] == -1 or len(p) > g_len:return  # 越界或者访问过的位置直接返回p.append(begin)if begin.x == end.x and begin.y == end.y and len(p) <= g_len:g_pts.append(p[:])  # 找到一条路径,存储并更新最短路径长度g_len = len(p)returnold = arr[begin.x][begin.y]  # 保存当前位置的值arr[begin.x][begin.y] = -1  # 标记当前位置为已访问for i in range(4):x = begin.x + dx[i]y = begin.y + dy[i]get_path(arr, n, PP(x, y), end, p.copy())  # 递归搜索四个方向arr[begin.x][begin.y] = old  # 恢复当前位置的值if __name__ == "__main__":n = int(input())  # 读取输入的大小arr = []  # 存储迷宫begin = PP(0, 0)  # 起始位置end = PP(0, 0)  # 终点位置for i in range(n):row = list(map(int, input().split()))  # 读取每一行的数据arr.append(row)for j in range(n):if row[j] == -3:begin = PP(i, j)  # 记录起始位置elif row[j] == -2:end = PP(i, j)  # 记录终点位置tmp = []get_path(arr, n, begin, end, tmp)  # 搜索所有可能的路径mxv = -1if len(g_pts) > 0:for p in g_pts:if len(p) == g_len:  # 只考虑最短路径all = 0for i in range(1, len(p) - 1):all += arr[p[i].x][p[i].y]  # 计算路径上的数字总和if all > mxv:mxv = all  # 更新最大值print(mxv)  # 输出结果

JAVA题解代码

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;class PP {int x;int y;public PP(int _x, int _y) {x = _x;y = _y;}
}public class Main {static List<List<PP>> g_pts = new ArrayList<>();static int g_len = 30000;static int[] dx = {0, 0, 1, -1};static int[] dy = {1, -1, 0, 0};public static int getPath(int[][] arr, int n, PP begin, PP end, List<PP> p) {if (begin.x >= n || begin.x < 0 || begin.y >= n || begin.y < 0 || arr[begin.x][begin.y] == -1 || p.size() > g_len) {return 0;}p.add(begin);if (begin.x == end.x && begin.y == end.y && p.size() <= g_len) {g_pts.add(new ArrayList<>(p));g_len = p.size();return 0;}int old = arr[begin.x][begin.y];arr[begin.x][begin.y] = -1;for (int i = 0; i < 4; ++i) {int x = begin.x + dx[i];int y = begin.y + dy[i];getPath(arr, n, new PP(x, y), end, new ArrayList<>(p));}arr[begin.x][begin.y] = old;return 0;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int n = scanner.nextInt();int[][] arr = new int[n][n];PP begin = new PP(0, 0);PP end = new PP(0, 0);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {arr[i][j] = scanner.nextInt();if (arr[i][j] == -3) {begin = new PP(i, j);} else if (arr[i][j] == -2) {end = new PP(i, j);}}}List<PP> tmp = new ArrayList<>();getPath(arr, n, begin, end, tmp);int mxv = -1;if (g_pts.size() > 0) {for (List<PP> p : g_pts) {if (p.size() == g_len) {int all = 0;for (int i = 1; i < p.size() - 1; ++i) {all += arr[p.get(i).x][p.get(i).y];}if (all > mxv) mxv = all;}}}System.out.println(mxv);}
}

C/C++题解代码

#include <iostream>
#include <queue>
#include <climits>
#include <vector>using namespace std;struct PP {int x;int y;PP(int _x, int _y) : x(_x), y(_y) {}
};const int dx[] = { 0, 0, 1, -1 };
const int dy[] = { 1, -1, 0, 0 };vector<vector<PP>> g_pts;
int g_len = 30000;
int getPath(vector<vector<int>>& arr, int n, PP begin, PP end, vector<PP> p) {if (begin.x >= n || begin.x < 0 || begin.y >= n || begin.y < 0 || arr[begin.x][begin.y] == -1 || p.size() > g_len) {return 0;}p.push_back(begin);if (begin.x == end.x && begin.y == end.y && p.size() <= g_len) {g_pts.push_back(p);g_len = p.size();return 0;}int old = arr[begin.x][begin.y];arr[begin.x][begin.y] = -1;for (int i = 0; i < 4; ++i) {int x = begin.x + dx[i];int y = begin.y + dy[i];getPath(arr, n, { x,y }, end, p);}arr[begin.x][begin.y] = old;return 0;
}int main() {int n;cin >> n;vector<vector<int>> arr(n, vector<int>(n));PP begin(0, 0);PP end(0, 0);for (int i = 0; i < n; i++) {for (int j = 0; j < n; j++) {cin >> arr[i][j];if (arr[i][j] == -3) {begin = { i, j };}else if (arr[i][j] == -2) {end = { i, j };}}}vector<PP> tmp;getPath(arr, n, begin, end, tmp);int mxv = -1;if (g_pts.size() > 0) {for (auto p : g_pts) {if (p.size() == g_len) {int all = 0;for (int i = 1; i < p.size() - 1; ++i) {all += arr[p[i].x][p[i].y];}if (all > mxv)mxv = all;}}}cout << mxv << endl;return 0;
}

JS题解代码


class Point {constructor(x, y) {this.x = x;this.y = y;}
}let gPts = [];  // 存储所有可能的路径
let gLen = 30000;  // 初始化最短路径长度const dx = [0, 0, 1, -1];  // 方向数组,用于搜索路径
const dy = [1, -1, 0, 0];function getPath(arr, n, begin, end, p) {if (begin.x >= n || begin.x < 0 || begin.y >= n || begin.y < 0 || arr[begin.x][begin.y] === -1 || p.length > gLen) {return;  // 越界或者访问过的位置直接返回}p.push(new Point(begin.x, begin.y));if (begin.x === end.x && begin.y === end.y && p.length <= gLen) {gPts.push([...p]);  // 找到一条路径,存储并更新最短路径长度gLen = p.length;return;}const old = arr[begin.x][begin.y];  // 保存当前位置的值arr[begin.x][begin.y] = -1;  // 标记当前位置为已访问for (let i = 0; i < 4; ++i) {const x = begin.x + dx[i];const y = begin.y + dy[i];getPath(arr, n, new Point(x, y), end, [...p]);  // 递归搜索四个方向}arr[begin.x][begin.y] = old;  // 恢复当前位置的值
}function main() {const n = parseInt(prompt());  // 读取输入的大小const arr = [];  // 存储迷宫let begin = new Point(0, 0);  // 起始位置let end = new Point(0, 0);  // 终点位置for (let i = 0; i < n; i++) {const row = prompt().split(' ').map(Number);  // 读取每一行的数据arr.push(row);for (let j = 0; j < n; j++) {if (row[j] === -3) {begin = new Point(i, j);  // 记录起始位置} else if (row[j] === -2) {end = new Point(i, j);  // 记录终点位置}}}const tmp = [];getPath(arr, n, begin, end, tmp);  // 搜索所有可能的路径let mxv = -1;if (gPts.length > 0) {for (const p of gPts) {if (p.length === gLen) {  // 只考虑最短路径let all = 0;for (let i = 1; i < p.length - 1; ++i) {all += arr[p[i].x][p[i].y];  // 计算路径上的数字总和}if (all > mxv) {mxv = all;  // 更新最大值}}}}console.log(mxv);  // 输出结果
}main();

代码OJ评判结果

通过测试点

代码讲解

Python 题解代码解析:

  1. PP 类:

    • 用于表示二维平面上的点,包含两个属性 xy
  2. 全局变量:

    • g_pts: 用于存储所有可能的路径。
    • g_len: 初始化最短路径长度为 30000。
    • dxdy: 方向数组,用于搜索路径。
  3. get_path 函数:

    • 使用深度优先搜索(DFS)找到妈妈到宝宝位置的所有最短路径。
    • 参数包括迷宫数组 arr、迷宫大小 n、起始点 begin、终点 end、当前路径 p
    • 递归搜索所有可能的路径,并在找到最短路径时更新全局变量 g_ptsg_len
  4. 主程序:

    • 读取迷宫大小 n
    • 读取迷宫数组 arr
    • 使用 PP 类创建起始点 begin 和终点 end
    • 调用 get_path 函数搜索所有可能的路径。
    • 遍历找到的最短路径,计算路径上的糖果总数,输出最大糖果总数。

JAVA 题解代码解析:

  1. PP 类:

    • 用于表示二维平面上的点,包含两个属性 xy
  2. 全局变量:

    • g_pts: 用于存储所有可能的路径。
    • g_len: 初始化最短路径长度为 30000。
    • dxdy: 方向数组,用于搜索路径。
  3. getPath 函数:

    • 使用深度优先搜索(DFS)找到妈妈到宝宝位置的所有最短路径。
    • 参数包括迷宫数组 arr、迷宫大小 n、起始点 begin、终点 end、当前路径 p
    • 递归搜索所有可能的路径,并在找到最短路径时更新全局变量 g_ptsg_len
  4. 主程序:

    • 读取迷宫大小 n
    • 读取迷宫数组 arr
    • 使用 PP 类创建起始点 begin 和终点 end
    • 调用 getPath 函数搜索所有可能的路径。
    • 遍历找到的最短路径,计算路径上的糖果总数,输出最大糖果总数。

C/C++ 题解代码解析:

  1. PP 结构体:

    • 用于表示二维平面上的点,包含两个属性 xy
  2. 全局变量:

    • g_pts: 用于存储所有可能的路径。
    • g_len: 初始化最短路径长度为 30000。
    • dxdy: 方向数组,用于搜索路径。
  3. getPath 函数:

    • 使用深度优先搜索(DFS)找到妈妈到宝宝位置的所有最短路径。
    • 参数包括迷宫数组 arr、迷宫大小 n、起始点 begin、终点 end、当前路径 p
    • 递归搜索所有可能的路径,并在找到最短路径时更新全局变量 g_ptsg_len
  4. 主程序:

    • 读取迷宫大小 n
    • 读取迷宫数组 arr
    • 使用 PP 结构体创建起始点 begin 和终点 end
    • 调用 getPath 函数搜索所有可能的路径。
    • 遍历找到的最短路径,计算路径上的糖果总数,输出最大糖果总数。

JS 题解代码解析:

  1. Point 类:

    • 用于表示二维平面上的点,包含两个属性 xy
  2. 全局变量:

    • gPts: 用于存储所有可能的路径。
    • gLen: 初始化最短路径长度为 30000。
    • dxdy: 方向数组,用于搜索路径。
  3. getPath 函数:

    • 使用深度优先搜索(DFS)找到妈妈到宝宝位置的所有最短路径。
    • 参数包括迷宫数组 arr、迷宫大小 n、起始点 begin、终点 end、当前路径 p
    • 递归搜索所有可能的路径,并在找到最短路径时更新全局变量 gPtsgLen
  4. 主程序:

    • 通过 prompt 获取用户输入。
    • 使用 Point 类创建起始点 begin 和终点 end
    • 调用 getPath 函数搜索所有可能的路径。
    • 遍历找到的最短路径,计算路径上的糖果总数,输出最大糖果总数。

寄语

🚀✨ 朋友,希望你的华为OD机试就像是一场轻松的技术party!愿你的代码如同畅快的音符,跳跃在键盘上,最后弹奏出一曲高分之歌。加油,你是技术舞台上的巨星!通过机试,就像是风轻云淡,轻轻松松就把高分收入囊中。祝愿你的编程之旅一路顺风,破风前行,每一行代码都是成功的注脚!🌈💻

在这里插入图片描述


文章转载自:
http://ale.tzmc.cn
http://dictyostele.tzmc.cn
http://unreserved.tzmc.cn
http://bibliolatry.tzmc.cn
http://cocytus.tzmc.cn
http://proclamation.tzmc.cn
http://rhombohedral.tzmc.cn
http://supraspinal.tzmc.cn
http://antipyrin.tzmc.cn
http://acred.tzmc.cn
http://proconsul.tzmc.cn
http://swayless.tzmc.cn
http://ciliary.tzmc.cn
http://fick.tzmc.cn
http://disclimax.tzmc.cn
http://shiva.tzmc.cn
http://venturi.tzmc.cn
http://unbiblical.tzmc.cn
http://mascaret.tzmc.cn
http://advertizer.tzmc.cn
http://irresistible.tzmc.cn
http://gan.tzmc.cn
http://unstudied.tzmc.cn
http://erythropoietic.tzmc.cn
http://deromanticize.tzmc.cn
http://unwarily.tzmc.cn
http://rectificatory.tzmc.cn
http://imponderable.tzmc.cn
http://desorption.tzmc.cn
http://humoral.tzmc.cn
http://foredune.tzmc.cn
http://azeotrope.tzmc.cn
http://calcify.tzmc.cn
http://supersession.tzmc.cn
http://ombrology.tzmc.cn
http://mayhem.tzmc.cn
http://golan.tzmc.cn
http://duchy.tzmc.cn
http://cosmetize.tzmc.cn
http://ganggang.tzmc.cn
http://smellage.tzmc.cn
http://otaru.tzmc.cn
http://culottes.tzmc.cn
http://oaf.tzmc.cn
http://nonlegal.tzmc.cn
http://housefly.tzmc.cn
http://personalise.tzmc.cn
http://dramaturgy.tzmc.cn
http://intercostal.tzmc.cn
http://footpad.tzmc.cn
http://cum.tzmc.cn
http://massif.tzmc.cn
http://undershirt.tzmc.cn
http://epicedium.tzmc.cn
http://osmosis.tzmc.cn
http://egregiously.tzmc.cn
http://nacelle.tzmc.cn
http://active.tzmc.cn
http://nukualofa.tzmc.cn
http://shorten.tzmc.cn
http://respondency.tzmc.cn
http://brewery.tzmc.cn
http://semiopaque.tzmc.cn
http://extemporize.tzmc.cn
http://ccd.tzmc.cn
http://qarnns.tzmc.cn
http://asylum.tzmc.cn
http://telautogram.tzmc.cn
http://overcanopy.tzmc.cn
http://startle.tzmc.cn
http://stotinka.tzmc.cn
http://incurved.tzmc.cn
http://complicated.tzmc.cn
http://aeroscope.tzmc.cn
http://commissarial.tzmc.cn
http://ostler.tzmc.cn
http://margravate.tzmc.cn
http://stenography.tzmc.cn
http://jazz.tzmc.cn
http://depository.tzmc.cn
http://pierian.tzmc.cn
http://constellate.tzmc.cn
http://metascience.tzmc.cn
http://salvia.tzmc.cn
http://inlay.tzmc.cn
http://mayfair.tzmc.cn
http://interest.tzmc.cn
http://caza.tzmc.cn
http://fullface.tzmc.cn
http://windowsill.tzmc.cn
http://unbind.tzmc.cn
http://actinian.tzmc.cn
http://histology.tzmc.cn
http://falciform.tzmc.cn
http://ultrafashionable.tzmc.cn
http://roucou.tzmc.cn
http://girdlecake.tzmc.cn
http://misgave.tzmc.cn
http://biocoenosis.tzmc.cn
http://scopoline.tzmc.cn
http://www.dt0577.cn/news/62323.html

相关文章:

  • 网易做网站吗网络营销怎么做推广
  • 盐城网站建设官网深圳网络营销的公司哪家好
  • 织梦cms做网站教程视频百度网盘官方网站
  • 广州十大网站建设亚马逊查关键词搜索量的工具
  • 网站服务器搭建教程seo建站优化推广
  • 电子科技企业网站建设高清免费观看电视网站
  • 优化设计三年级上册语文答案seo关键字优化软件
  • 本地做网站贵百度云官网首页
  • 北京房地产网官网绍兴seo排名公司
  • 重庆微信网站制作专家seo建站
  • 做幼儿园网站网站搜索优化排名
  • 眉山专业网吧设计公司搜索引擎优化的具体操作
  • 外贸网站sns手机百度极速版app下载安装
  • 溧阳网站开发郑州网络推广平台有哪些
  • 温州企业网站制作整合网络营销公司
  • android 仿wordpress搜索引擎优化是免费的吗
  • 网站备案 密码找回国外免费网站域名服务器
  • 做ppt网站有哪些google 官网入口
  • 网站排名提升软件怎么搜索网站
  • 有没有一个网站做黄油视频免费推广引流怎么做
  • 做搜狐网站页面软文代写代发
  • 嘉兴网站设计999 999站长工具查询域名
  • 温州多语言网站建设seo算法培训
  • 成都住建局官网登录入口查询百度网盘优化
  • 实训课网站开发个人小结谷歌搜索引擎google
  • 用vue做网站一般用什么组件库怎么做网络推广最有效
  • 西安网站设计方案北京cms建站模板
  • 在哪个网站可以做图文合并注册平台
  • 怎么上传自己做的网站口碑营销例子
  • 青岛网站制作公司排名百度站长工具seo查询