当前位置:网站首页>剑指 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;
}
}
边栏推荐
- False breakthroughs in the trend of London Silver
- [solved] how to generate a beautiful static document description page
- [Jiudu OJ 09] two points to find student information
- A glimpse of spir-v
- Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition
- MATLB | real time opportunity constrained decision making and its application in power system
- 【Flask】获取请求信息、重定向、错误处理
- In the era of industrial Internet, we will achieve enough development by relying on large industrial categories
- 3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
- 3D模型格式汇总
猜你喜欢

Blue Bridge Cup embedded stm32g431 - the real topic and code of the eighth provincial competition

MATLB|实时机会约束决策及其在电力系统中的应用

False breakthroughs in the trend of London Silver

Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步

XSS learning XSS lab problem solution

Leetcode skimming questions_ Verify palindrome string II

Basic process and testing idea of interface automation

Cookie concept, basic use, principle, details and Chinese transmission

Xunrui CMS plug-in automatically collects fake original free plug-ins

Some features of ECMAScript
随机推荐
Leetcode skimming questions_ Verify palindrome string II
Docker compose configures MySQL and realizes remote connection
黄金价格走势k线图如何看?
【Flask】官方教程(Tutorial)-part1:项目布局、应用程序设置、定义和访问数据库
Basic operations of database and table ----- set the fields of the table to be automatically added
一图看懂!为什么学校教了你Coding但还是不会的原因...
Development trend of Ali Taobao fine sorting model
[Jiudu OJ 09] two points to find student information
Obstacle detection
ctf. Show PHP feature (89~110)
Alibaba-Canal使用详解(排坑版)_MySQL与ES数据同步
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
SPIR-V初窺
General operation method of spot Silver
How to get the PHP version- How to get the PHP Version?
Loop structure of program (for loop)
Win10 add file extension
DOM introduction
[understanding of opportunity-39]: Guiguzi - Chapter 5 flying clamp - warning 2: there are six types of praise. Be careful to enjoy praise as fish enjoy bait.
Remember that a version of @nestjs/typeorm^8.1.4 cannot be obtained Env option problem