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

网站排名 各因素网站开发建设与维护

网站排名 各因素,网站开发建设与维护,牛商网网站建设,那种网站2021题目描述 迷宫地宫地形信息记录于一维字符串数组 maze 中,maze[i] 为仅由 "."、"X"、"S" 和 "E" 组成的字符串,其中: "X" 表示墙,不可直接通行,但可选择将墙推倒…

题目描述

迷宫地宫地形信息记录于一维字符串数组 maze 中,maze[i] 为仅由 "."、"X"、"S" 和 "E" 组成的字符串,其中:

  • "X" 表示墙,不可直接通行,但可选择将墙推倒后通过;
  • "." 表示空地,可以通行;
  • "S" 表示迷宫入口;
  • "E" 表示迷宫出口。

请你求出入口到出口所需的最少推墙次数。

注意: 迷宫中仅有一个入口和一个出口。

示例 1:

输入:
maze = ["XSXX","....","XXX.","X.E."]输出: 0解释:
不需要推倒任何墙就可以直接从入口到达出口,下图是一种可行的方案:

示例 2:

输入:
maze = ["XSXX","X...","XX..","EX.."]输出: 1解释:
最少需要推倒 1 次墙,下图是一种可行的方案:

提示:

  • 1 <= maze.length, maze[i].length <= 100

问题分析

这是一个带权重的最短路径问题,具有以下特点:

  • 移动规则:
    • 移动到空地(".")或出口("E"):代价为 0
    • 推倒墙("X")并移动:代价为 1
  • 目标:找到从入口("S")到出口("E")的最小推墙次数
  • 算法选择:
    • 由于只有两种权重(0和1),最适合使用 0-1 BFS
    • 也可以使用 Dijkstra 算法

解题思路

0-1 BFS 方法

核心思想:

  • 使用双端队列(Deque)
  • 代价为0的移动加入队列头部
  • 代价为1的移动加入队列尾部
  • 这样保证每次取出的都是当前代价最小的状态

算法步骤:

  1. 找到起点和终点位置
  2. 使用双端队列进行BFS
  3. 对于每个位置,尝试向四个方向移动
  4. 根据目标位置的类型决定代价和入队方式

解法实现

Java 实现

import java.util.ArrayDeque;
import java.util.Deque;/*** 3. 走迷宫 II* 0-1 BFS*/
class Solution {// 四个方向:上下左右private static final int[][] DIRS = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};public int solve(String[] maze) {int rows = maze.length;int cols = maze[0].length();// 找到起点和终点int startRow = -1, startCol = -1;int endRow = -1, endCol = -1;for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {if (maze[i].charAt(j) == 'S') {startRow = i;startCol = j;}if (maze[i].charAt(j) == 'E') {endRow = i;endCol = j;}}}return bfs(maze, startRow, startCol, endRow, endCol);}private int bfs(String[] maze, int startRow, int startCol, int endRow, int endCol) {int rows = maze.length;int cols = maze[0].length();// 距离数组,记录到达每个位置的最少推墙次数int[][] dist = new int[rows][cols];for (int i = 0; i < rows; i++) {for (int j = 0; j < cols; j++) {dist[i][j] = Integer.MAX_VALUE;}}// 双端队列用于0-1 BFSDeque<int[]> deque = new ArrayDeque<>();deque.offer(new int[]{startRow, startCol});dist[startRow][startCol] = 0;while (!deque.isEmpty()) {int[] cur = deque.poll();int curRow = cur[0];int curCol = cur[1];// 如果到达终点,返回结果if (curRow == endRow && curCol == endCol) {return dist[curRow][curCol];}// 尝试四个方向for (int[] dir : DIRS) {int newRow = curRow + dir[0];int newCol = curCol + dir[1];// 检查边界if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols) {continue;}char cell = maze[newRow].charAt(newCol);int newCost;// 计算移动代价if (cell == '.' || cell == 'E') {newCost = dist[curRow][curCol]; // 移动到空格或终点,代价不变} else if (cell == 'X') {newCost = dist[curRow][curCol] + 1; // 移动到墙,代价加1} else {continue; // 遇到其他字符,跳过}// 如果找到更优路径,更新距离并加入队列if (newCost < dist[newRow][newCol]) {dist[newRow][newCol] = newCost;// 0-1 BFS的关键:根据代价决定加入队列的位置if (cell == '.' || cell == 'E') {deque.offerFirst(new int[]{newRow, newCol}); // 代价为0,加入队列头部} else {deque.offerLast(new int[]{newRow, newCol}); // 代价为1,加入队列尾部}}}}// 无法到达终点return -1;}
}

C# 实现

/*** 3. 走迷宫 II* 0-1 BFS*/
public class Solution
{// 四个方向:上下左右private int[,] _dirs = new int[,] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };public int Solve(string[] maze){int rows = maze.Length;int cols = maze[0].Length;// 找到起点和终点int startRow = -1, startCol = -1;int endRow = -1, endCol = -1;for (int i = 0; i < rows; i++){for (int j = 0; j < cols; j++){if (maze[i][j] == 'S'){startRow = i;startCol = j;}else if (maze[i][j] == 'E'){endRow = i;endCol = j;}}}return Bfs(maze, startRow, startCol, endRow, endCol);}private int Bfs(string[] maze, int startRow, int startCol, int endRow, int endCol){int rows = maze.Length;int cols = maze[0].Length;// 距离数组,记录到达每个位置的最少推墙次数int[,] dist = new int[rows, cols];for (int i = 0; i < rows; i++){for (int j = 0; j < cols; j++){dist[i, j] = int.MaxValue;}}// 双端队列用于0-1 BFSLinkedList<(int, int)> deque = new LinkedList<(int, int)>();deque.AddLast((startRow, startCol));dist[startRow, startCol] = 0;while (deque.Count > 0){(int, int) cur = deque.First.Value;deque.RemoveFirst();int curRow = cur.Item1;int curCol = cur.Item2;// 如果到达终点,返回结果if (curRow == endRow && curCol == endCol){return dist[curRow, curCol];}// 尝试四个方向for (int i = 0; i < 4; i++){int newRow = curRow + _dirs[i, 0];int newCol = curCol + _dirs[i, 1];// 检查边界if (newRow < 0 || newRow >= rows || newCol < 0 || newCol >= cols){continue;}char cell = maze[newRow][newCol];// 计算移动代价int newCost;if (cell == '.' || cell == 'E'){newCost = dist[curRow, curCol]; // 移动到空格或终点,代价为当前代价}else if (cell == 'X'){newCost = dist[curRow, curCol] + 1; // 移动到墙,代价为当前代价加1}else{continue; // 忽略其他字符}// 如果找到更优路径,更新距离并加入队列if (newCost < dist[newRow, newCol]){dist[newRow, newCol] = newCost;// 0-1 BFS的关键:根据代价决定加入队列的位置if (cell == '.' || cell == 'E'){deque.AddFirst((newRow, newCol)); // 移动到空格或终点,加入队列头部}else{deque.AddLast((newRow, newCol)); // 移动到墙,加入队列尾部}}}}return -1;}
}

算法复杂度分析

时间复杂度: O(M × N)

  • M 和 N 分别是迷宫的行数和列数
  • 每个位置最多被访问一次

空间复杂度: O(M × N)

  • 需要 dist 数组存储到达每个位置的最小代价
  • 双端队列在最坏情况下可能存储所有位置

文章转载自:

http://tHNLJUAd.nrftd.cn
http://qY8a16sJ.nrftd.cn
http://MKOgVqLX.nrftd.cn
http://JA05o4Rx.nrftd.cn
http://mwgBc33A.nrftd.cn
http://6ToEMBZS.nrftd.cn
http://ItdFKFbQ.nrftd.cn
http://vKJyiUak.nrftd.cn
http://VkL0KNe1.nrftd.cn
http://NVfq5avx.nrftd.cn
http://rlrZwWli.nrftd.cn
http://6LxaHcL5.nrftd.cn
http://3doqn3K1.nrftd.cn
http://kp9wnDWO.nrftd.cn
http://YeQ3ZlYC.nrftd.cn
http://6RszjHy0.nrftd.cn
http://33P8Vhz8.nrftd.cn
http://UWchkNgQ.nrftd.cn
http://3NQiSd2Y.nrftd.cn
http://nXh7Te0q.nrftd.cn
http://MfyK5Nj1.nrftd.cn
http://81X7Aw6Y.nrftd.cn
http://Hza0SR9Z.nrftd.cn
http://Ft64cota.nrftd.cn
http://actzdDQq.nrftd.cn
http://isObAsKc.nrftd.cn
http://97z6QM9B.nrftd.cn
http://ZawSAPfs.nrftd.cn
http://jjUV1DGV.nrftd.cn
http://3XeD1Eiu.nrftd.cn
http://www.dtcms.com/wzjs/772784.html

相关文章:

  • 网站建设服务器维护内容高州网站开发公司
  • 极速建站 哪家好网站建设基础功能
  • 域名怎么做网站内容厦门做网页网站的公司
  • 大型网站开发软件移动互联网时代的信息安全与防护超星网课答案
  • 硬件开发一站式平台网页设计实训报告摘要
  • 织梦模板更新网站金融公司网站制作
  • 怎么申请域名建网站中国证券登记结算有限公司官网
  • 提供常州微信网站建设如何建一个网站多少钱
  • 自己做网站服务器静态网站的好处
  • 网站开发需要多少钱销售大庆做网站找谁
  • 我做网站了四川建设主管部门网站
  • 网站开发运营经理如何做大型网站
  • 浙江网站建设流程wordpress搬迁后多媒体库无法
  • 临泉建设网站中企动力服务怎么样
  • 网站怎么做运营推广正品海外购网站有哪些
  • 做app模板下载网站个人网站 前置审批
  • 计算机编程与网站建设百度手机版网页
  • 海南建设银行官网招聘网站中国建筑企业网
  • 怎样在工商局网站做申请登记产品软文范例
  • 做电影网站的图片素材找人制作网站 优帮云
  • 网站开发费是无形资产吗泉州制作网站设计
  • 苏州网站开发公司电话成都展厅设计公司
  • 织梦网站栏目建设哪家公司做推广优化好
  • wordpress the_title() 字数泉州seo托管
  • 电子商务网站运营与...做海报的软件
  • 网站集约化建设存在的困难漳州北京网站建设公司哪家好
  • 企业网站建设多少钱建设信用卡个人网站
  • 河南做网站公司哪家专业织梦手机网站模板下载
  • 怎么让人理解网站建设软件开发项目流程
  • 外贸 企业网站 建设页面开发