Skip to content

Commit f522fdb

Browse files
committed
feat: exit
1 parent 6f38b25 commit f522fdb

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

word-search/soobing3.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function exist(board: string[][], word: string): boolean {
2+
const move = [[1, 0], [-1, 0], [0, 1], [0, -1]];
3+
4+
function dfs(row: number, col: number, index: number) {
5+
if(board[row][col] !== word[index]) return false;
6+
7+
const temp = board[row][col];
8+
board[row][col] = '#';
9+
10+
if(index === word.length - 1) return true;
11+
for(const [dx, dy] of move) {
12+
if(row + dx < 0 || row + dx >= board.length) continue;
13+
if(col + dy < 0 || col + dy >= board[0].length) continue;
14+
if(dfs(row + dx, col + dy, index + 1)) return true;
15+
}
16+
17+
board[row][col] = temp;
18+
}
19+
20+
for (let r = 0; r < board.length; r++) {
21+
for (let c = 0; c < board[0].length; c++) {
22+
if (dfs(r, c, 0)) return true;
23+
}
24+
}
25+
return false;
26+
};

0 commit comments

Comments
 (0)