[leetcode]5366. 检查网格中是否存在有效路径
解题思路:BFS
AC的代码:
struct Point
{int x,y;int d;Point(int _x, int _y, int _d){x = _x, y = _y, d = _d;}
};
class Solution {bool isOk(int cur, int next, int dir){if(dir == 0 && (cur == 2 || cur == 5 || cur == 6) && (next == 2 || next == 3 || next == 4)){return true;}else if(dir == 1 && (cur == 1 || cur == 4 || cur == 6) && (next == 1 || next == 3 || next == 5)){return true;}else if(dir == 2 && (cur == 2 || cur == 3 || cur == 4) && (next == 2 || next == 5 || next == 6)){return true;}else if(dir == 3 && (cur == 1 || cur == 3 || cur == 5) && (next == 1 || next == 4 || next == 6)){return true;}return false;}
public:bool hasValidPath(vector<vector<int>>& grid) {int m = grid.size(), n = grid[0].size();vector<vector<bool>>visited(m, vector<bool>(n,false));queue<Point>q;q.push(Point(0,0,grid[0][0]));int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};while(!q.empty()){Point cur = q.front(); q.pop();visited[cur.x][cur.y] = true;if(cur.x == m-1 && cur.y == n-1){return true;}for(int i = 0; i < 4; i++){int nextX = cur.x + dx[i], nextY = cur.y + dy[i];if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visited[nextX][nextY]) continue;if(isOk(cur.d, grid[nextX][nextY], i)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}}}return false;}
};
没通过的代码:
struct Point
{int x,y;int d;Point(int _x, int _y, int _d){x = _x, y = _y, d = _d;}
};
class Solution {public:bool hasValidPath(vector<vector<int>>& grid) {int m = grid.size(), n = grid[0].size();vector<vector<bool>>visited(m, vector<bool>(n,false));queue<Point>q;q.push(Point(0,0,grid[0][0]));int dx[4] = {-1, 0, 1, 0};int dy[4] = {0, 1, 0, -1};while(!q.empty()){Point cur = q.front(); q.pop();visited[cur.x][cur.y] = true;if(cur.x == m-1 && cur.y == n-1){return true;}for(int i = 0; i < 4; i++){int nextX = cur.x + dx[i], nextY = cur.y + dy[i];if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || visited[nextX][nextY]) continue;if(i == 1 && cur.d == 1 && ( grid[nextX][nextY] == 1 || grid[nextX][nextY] == 3 || grid[nextX][nextY] == 5)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 3 && cur.d == 1 && (grid[nextX][nextY] == 1 ||grid[nextX][nextY] == 4 || grid[nextX][nextY] == 6)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 2 && cur.d == 2 && grid[nextX][nextY] != 1){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 0 && cur.d == 2 && (grid[nextX][nextY] == 3 || grid[nextX][nextY] == 4 || grid[nextX][nextY] == 2)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 2 && cur.d == 3 && grid[nextX][nextY] != 3 && grid[nextX][nextY] != 1 && grid[nextX][nextY] != 4){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 3 && cur.d == 3 && (grid[nextX][nextY] == 6 || grid[nextX][nextY] == 4 || grid[nextX][nextY] == 1)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 2 &&cur.d == 4 && (grid[nextX][nextY] == 2 || grid[nextX][nextY] == 5 || grid[nextX][nextY] == 6)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 1 &&cur.d == 4 && (grid[nextX][nextY] == 3 || grid[nextX][nextY] == 5 || grid[nextX][nextY] == 1)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 0 && cur.d == 5 && grid[nextX][nextY] != 5 && grid[nextX][nextY] != 1 && grid[nextX][nextY] != 6){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 2 && cur.d == 5 && (grid[nextX][nextY] == 1 || grid[nextX][nextY] == 6)){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 0 && cur.d == 6 && grid[nextX][nextY] != 1 && grid[nextX][nextY] != 6 && grid[nextX][nextY] != 5){q.push(Point(nextX, nextY, grid[nextX][nextY]));}if(i == 1 && cur.d == 6 && grid[nextX][nextY] != 2 && grid[nextX][nextY] != 6 && grid[nextX][nextY] != 4){q.push(Point(nextX, nextY, grid[nextX][nextY]));}}}return false;}
};
[leetcode]5366. 检查网格中是否存在有效路径相关推荐
- LeetCode 5366. 检查网格中是否存在有效路径
5366. 检查网格中是否存在有效路径 思路:分好上下左右的情况即可,比bfs,dfs那些简单一点.从0,0开始,走到m-1,n-1就返回true,go_next(判断下一步) class Solut ...
- LeetCode 1391. 检查网格中是否存在有效路径(BFS)
文章目录 1. 题目 2. 解题 2.1 BFS 2.2 爆栈的DFS 2.3 不爆栈的DFS 1. 题目 给你一个 m x n 的网格 grid.网格里的每个单元都代表一条街道.grid[i][j] ...
- LeetCode 2042. 检查句子中的数字是否递增
文章目录 1. 题目 2. 解题 1. 题目 句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔,句子没有前导或尾随空格. 每个 token 要么是一个由数字 0-9 组成的不 ...
- LeetCode简单题之检查句子中的数字是否递增
题目 句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔,句子没有前导或尾随空格.每个 token 要么是一个由数字 0-9 组成的不含前导零的 正整数 ,要么是一个由小写英文 ...
- Leetcode刷题100天—2042. 检查句子中的数字是否递增—day70
前言: 作者:神的孩子在歌唱 大家好,我叫智 2042. 检查句子中的数字是否递增 难度简单2收藏分享切换为英文接收动态反馈 句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔 ...
- SAP ABAP ALV控制显示的网格中的每一个字段属性
字段目录是用来控制ALV显示的网格中每个字段的属性的,比如字段的顺序,对齐方式,可编辑状态,颜色,等等.常用的字段如下: row_pos:默认值为0,可选值为1.2.3,既最大分3级别显示 field ...
- 【Unity3D】材质 Material ( 材质简介 | 创建材质 | 设置材质属性 | 对 3D 物体应用材质 | 资源拖动到 Inspector 检查器中的 Material 属性中 )
文章目录 一.材质 Material 简介 二.创建材质 三.设置材质属性 四.对 3D 物体应用材质 五.资源拖动到 Inspector 检查器中的 Material 属性中 一.材质 Materi ...
- 单词搜索(二维字符网格中)
给定一个 m x n 二维字符网格 board 和一个字符串单词 word .如果 word 存在于网格中,返回 true :否则,返回 false . 单词必须按照字母顺序,通过相邻的单元格内的字母 ...
- R语言ggplot2可视化抑制可视化网格中的竖线输出、抑制可视化网格中的横线线输出、抑制背景网格输出实战
R语言ggplot2可视化抑制可视化网格中的竖线输出.抑制可视化网格中的横线线输出.抑制背景网格输出实战 目录
最新文章
- HP存储raid5两块硬盘离线lvm下vxfs文件系统恢复数据过程
- 栈和队列的算法题总结
- matlab错误:Variable 'a' cannot be saved to a MAT-file whose version is older than 7.3.
- php如何给数组取名,给表单取名可以为数组,并且可以通过php打印出来
- Mybatis.cfg配置标签的顺序问题
- Programming WCF Services中文翻译(3)-契约
- PIC单片机应用开发实践教程(五): 烧录器简介
- pycharm连接github
- FLV格式的视频歌曲地址600首,复制地址可插入外链播放器专用
- [Java学习笔记]-数组
- js 获取服务器时间——IE浏览器出现1970问题
- 游戏的初级体验,三围:视、听、触的展示
- STL容器之vector
- 浅谈Android视频缓存库
- 游走仙境稻城亚丁,稻城亚丁新攻略
- 请使用netty框架实现高效稳定的websocket通信
- MacOS 磁盘管理工具 diskutil 介绍
- 。Windows 键盘快捷键
- qiankun微前端实践
- Autodesk Maya 2019 安装
热门文章
- 编程语言python怎么读-0编程基础,什么语言也没学过,请问学Python怎样入门?...
- python干嘛用的-学 Python 都用来干嘛的?
- python3入门代码-Python3 教程 | 菜鸟教程
- 零基础学python全彩版-零基础轻松学Python:青少年趣味编程(全彩版)
- python计算工资编程-老男孩学Python编程后薪资待遇高吗?
- 『开源项目』基于STM32的智能垃圾桶之语音识别
- 行业研究 | 语音识别行业发展现状解读
- 国家机构测评主流电视品牌语音识别 长虹Q5K综合评价最佳
- php分页显示多少页,php实现分页显示
- Web API-定时器