-
Notifications
You must be signed in to change notification settings - Fork 0
/
0079_Word_Search.cs
50 lines (44 loc) · 1.26 KB
/
0079_Word_Search.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Problem statement: https://leetcode.com/problems/word-search/
public class Solution {
char[][] board;
bool[,] used;
string word;
int m, n, len;
static int[] dr = {-1, 0, 1, 0};
static int[] dc = { 0, 1, 0,-1};
bool Find(int r, int c, int idx) {
if (idx == len-1) {
return true;
}
used[r, c] = true;
for (int d = 0; d < 4; d++) {
int r1 = r + dr[d];
int c1 = c + dc[d];
if (r1 >= 0 && c1 >= 0 && r1 < m && c1 < n && !used[r1, c1]) {
if (board[r1][c1] == word[idx+1] && Find(r1, c1, idx+1)) {
return true;
}
}
}
used[r, c] = false;
return false;
}
public bool Exist(char[][] board, string word) {
m = board.Length;
n = board[0].Length;
this.board = board;
this.word = word;
len = word.Length;
used = new bool[m, n];
for (int r = 0; r < m; r++) {
for (int c = 0; c < n; c++) {
if (board[r][c] == word[0]) {
if (Find(r, c, 0)) {
return true;
}
}
}
}
return false;
}
}