当前位置:网站首页>2021-10-02: word search. Given an M x n two-dimensional character grid boa
2021-10-02: word search. Given an M x n two-dimensional character grid boa
2022-06-24 03:30:00 【Fuda scaffold constructor's daily question】
2021-10-02: Word search . Given a m x n Two dimensional character grid board And a string word word . If word Exists in the grid , return true ; otherwise , return false . The words must be in alphabetical order , It's made up of letters in adjacent cells , among “ adjacent ” Cells are those adjacent horizontally or vertically . Letters in the same cell are not allowed to be reused . Power button 79.
Fuda answer 2021-10-02:
Natural intelligence is enough .
recursive . about boardi, Go up, down, left and right .1. Manufacturing site ;2. recursive ;3. Restore the scene .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
board := [][]byte{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}}
word := "ABCCED"
ret := exist(board, word)
fmt.Println(ret)
}
func exist(board [][]byte, word string) bool {
w := []byte(word)
for i := 0; i < len(board); i++ {
for j := 0; j < len(board[0]); j++ {
if f(board, i, j, w, 0) {
return true
}
}
}
return false
}
// It has arrived b[i][j],word[k....]
// from b[i][j] set out , Can we take care of it word[k....] true false
func f(b [][]byte, i int, j int, w []byte, k int) bool {
if k == len(w) {
return true
}
// word[k.....] Have a character
// If (i,j) Transboundary , return false
if i < 0 || i == len(b) || j < 0 || j == len(b[0]) {
return false
}
if b[i][j] != w[k] {
return false
}
tmp := b[i][j]
b[i][j] = 0
ans := f(b, i-1, j, w, k+1) || f(b, i+1, j, w, k+1) || f(b, i, j-1, w, k+1) || f(b, i, j+1, w, k+1)
b[i][j] = tmp
return ans
}The results are as follows :
边栏推荐
- How to pair cloud game servers? Is the cloud game server expensive?
- Tencent Mu Lei: real scene 3D linking industrial Internet and consumer Internet
- What does cloud desktop mean? What are the characteristics of cloud desktop?
- How to access the server through the fortress machine? What's the use of the fortress machine?
- What are the configuration requirements for cloud desktop servers? What are the main characteristics of the three points?
- How to build glasses website what are the functions of glasses website construction
- Independent innovation and localization technology: SMT production line monitoring and management visualization of intelligent manufacturing
- Storage crash MySQL database recovery case
- Ligature in font design
- What does elastic public IP mean? The advantages of elastic public IP
猜你喜欢

On Sunday, I rolled up the uni app "uview excellent UI framework"

Simple and beautiful weather code

Get to know MySQL database
![[summary of interview questions] zj5](/img/d8/ece82f8b2479adb948ba706f6f5039.jpg)
[summary of interview questions] zj5

Community pycharm installation visual database

QT creator tips

Sorting out of key vulnerabilities identified by CMS in the peripheral management of red team (I)
![[summary of interview questions] zj6 redis](/img/4b/eadf66ca8d834f049f3546d348fa32.jpg)
[summary of interview questions] zj6 redis

Ar 3D map technology
随机推荐
What is the all-in-one backup machine? How about its cost performance
Why do cloud desktops use rack servers? Why choose cloud desktop?
Tens of millions of Android infected with malicious virus and Microsoft disabled a function of Excel | global network security hotspot on October 9
Dry goods how to build a data visualization project from scratch?
Get to know MySQL database
What is edge computing? What are the characteristics of the Internet platform edge calculator?
New Google brain research: how does reinforcement learning learn to observe with sound?
Differences between EDI and VMI
Grpc: how to implement distributed log tracing?
What is the GPU usage for cloud desktops and servers? What can cloud desktop do?
How to solve the problem of easycvr playing the total recording time in the specified time period?
What is the principle of intelligent image recognition? What are the applications of intelligent image recognition?
How to handle the uplink and downlink silence of TRTC
On Sunday, I rolled up the uni app "uview excellent UI framework"
System library golang Org/x/time/rate frequency limiter bug
Lua language development, esp8266 access to Bafa cloud, mqtt and TCP protocols
"Sharp weapon" for enterprise resumption? When the sale comes, the contract should be signed like this!
Chapter 4: LED flash case of PS bare metal and FreeRTOS case development
What does elastic public IP mean? The advantages of elastic public IP
LeetCode 1047. Delete all adjacent duplicates in the string