当前位置:网站首页>剑指 Offer 12. 矩阵中的路径
剑指 Offer 12. 矩阵中的路径
2022-07-06 01:29:00 【Yake1965】
剑指 Offer 12. 矩阵中的路径
class Solution {
int m, n, k;
char[][] board;
char[] w;
int[][] dirs = {
{
0, 1}, {
0, -1}, {
1, 0}, {
-1, 0}};
public boolean exist(char[][] board, String word) {
m = board.length;
n = board[0].length;
k = word.length();
this.board = board;
w = word.toCharArray();
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(board[i][j] == w[0]) {
board[i][j] = '!';
if(f(i, j, 1)) return true;
board[i][j] = w[0];
}
}
}
return false;
}
boolean f(int i, int j, int idx){
if(idx == k) return true;
for(int[] dir : dirs){
int x = i + dir[0];
int y = j + dir[1];
if(x >= 0 && x < m && y >= 0 && y < n && board[x][y] == w[idx]){
board[x][y] = '!';
if(f(x, y, idx + 1)) return true;
board[x][y] = w[idx];
}
}
return false;
}
}
边栏推荐
- FFT learning notes (I think it is detailed)
- Basic operations of database and table ----- set the fields of the table to be automatically added
- internship:项目代码所涉及陌生注解及其作用
- Superfluid_ HQ hacked analysis
- 【Flask】静态文件与模板渲染
- How does the crystal oscillator vibrate?
- Paddle framework: paddlenlp overview [propeller natural language processing development library]
- 【全網最全】 |MySQL EXPLAIN 完全解讀
- How to get the PHP version- How to get the PHP Version?
- Development trend of Ali Taobao fine sorting model
猜你喜欢
WordPress collection plug-in automatically collects fake original free plug-ins
yii中console方法调用,yii console定时任务
Dede collection plug-in free collection release push plug-in
有谁知道 达梦数据库表的列的数据类型 精度怎么修改呀
Basic operations of databases and tables ----- non empty constraints
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
UE4 unreal engine, editor basic application, usage skills (IV)
3D模型格式汇总
关于softmax函数的见解
VMware Tools安装报错:无法自动安装VSock驱动程序
随机推荐
Vulhub vulnerability recurrence 75_ XStream
VMware Tools安装报错:无法自动安装VSock驱动程序
WordPress collection plug-in automatically collects fake original free plug-ins
About error 2003 (HY000): can't connect to MySQL server on 'localhost' (10061)
Leetcode1961. Check whether the string is an array prefix
How to get the PHP version- How to get the PHP Version?
Basic operations of databases and tables ----- non empty constraints
Basic operations of database and table ----- delete data table
Yii console method call, Yii console scheduled task
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
[detailed] several ways to quickly realize object mapping
基于DVWA的文件上传漏洞测试
Leetcode1961. 检查字符串是否为数组前缀
ThreeDPoseTracker项目解析
Cookie concept, basic use, principle, details and Chinese transmission
Code Review关注点
yii中console方法调用,yii console定时任务
Opinions on softmax function
Pbootcms plug-in automatically collects fake original free plug-ins
General operation method of spot Silver