博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
牛客OJ:矩阵中的路径(DFS模版题)
阅读量:4059 次
发布时间:2019-05-25

本文共 1093 字,大约阅读时间需要 3 分钟。

需要注意的地方,字符串结尾作为dfs跳出会有bug,当字符串长度等于矩阵所有元素时。

#include 
using namespace std;const int dirX[] = {-1,1,0,0};const int dirY[] = {0,0,-1,1};class Solution {public: bool dfs(bool* flag, const char* m,const int r,const int c, const char* str,int i,int j,int loc){ //printf("<%c %d %d %c>\n",m[i*c+j],i,j,str[loc]); if(str[loc] == 0) return true; if(str[loc] == m[i*c+j] && str[loc+1] == 0) return true; if(str[loc] != m[i*c+j]) return false; flag[i*c+j] = true; bool ans = false; for(int dir=0;dir<4;dir++){ int newi = i + dirX[dir]; int newj = j + dirY[dir]; if(newi >= 0 && newi < r && newj >= 0 && newj < c && flag[newi*c+newj] == false){ ans = ans || dfs(flag,m,r,c,str,newi,newj,loc+1); } } flag[i*c+j] = false; return ans; } bool hasPath(char* matrix, int rows, int cols, char* str) { if(strlen(str) <= 0) return true; bool ans = false; bool flag[rows*cols]; for(int i=0;i

转载地址:http://nfwji.baihongyu.com/

你可能感兴趣的文章
慢慢欣赏linux make uImage流程
查看>>
linux内核学习(7)脱胎换骨解压缩的内核
查看>>
以太网基础知识
查看>>
慢慢欣赏linux 内核模块引用
查看>>
kprobe学习
查看>>
慢慢欣赏linux phy驱动初始化2
查看>>
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>