# 单词搜索

<p>给定一个 <code>m x n</code> 二维字符网格 <code>board</code> 和一个字符串单词 <code>word</code> 。如果 <code>word</code> 存在于网格中，返回 <code>true</code> ；否则，返回 <code>false</code> 。</p><p>单词必须按照字母顺序，通过相邻的单元格内的字母构成，其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word2.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"<strong><br />输出：</strong>true</pre><p><strong>示例 2：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word-1.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"<strong><br />输出：</strong>true</pre><p><strong>示例 3：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0079.Word%20Search/images/word3.jpg" style="width: 322px; height: 242px;" /><pre><strong>输入：</strong>board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"<strong><br />输出：</strong>false</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>m == board.length</code></li>	<li><code>n = board[i].length</code></li>	<li><code>1 <= m, n <= 6</code></li>	<li><code>1 <= word.length <= 15</code></li>	<li><code>board</code> 和 <code>word</code> 仅由大小写英文字母组成</li></ul><p> </p><p><strong>进阶：</strong>你可以使用搜索剪枝的技术来优化解决方案，使其在 <code>board</code> 更大的情况下可以更快解决问题？</p>

以下程序实现了这一功能，请你填补空白处内容：

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool dfs(char *word, char **board, bool *used,
				int row, int col, int row_size, int col_size)
{
	if (board[row][col] != *word)
	{
		return false;
	}
	used[row * col_size + col] = true;
	if (*(word + 1) == '\0')
	{
		return true;
	}
	bool result = false;
	if (row > 0 && !used[(row - 1) * col_size + col])
	{
		result = dfs(word + 1, board, used, row - 1, col, row_size, col_size);
	}
	if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col])
	{
		result = dfs(word + 1, board, used, row + 1, col, row_size, col_size);
	}
	if (!result && col > 0 && !used[row * col_size + col - 1])
	{
		result = dfs(word + 1, board, used, row, col - 1, row_size, col_size);
	}
	if (!result && col < col_size - 1 && !used[row * col_size + col + 1])
	{
		__________________________;
	}
	used[row * col_size + col] = false;
	return result;
}
static bool exist(char **board, int boardRowSize, int boardColSize, char *word)
{
	int i, j;
	int len = strlen(word);
	if (len > boardRowSize * boardColSize)
	{
		return false;
	}
	bool *used = malloc(boardRowSize * boardColSize);
	for (i = 0; i < boardRowSize; i++)
	{
		for (j = 0; j < boardColSize; j++)
		{
			memset(used, false, boardRowSize * boardColSize);
			if (dfs(word, board, used, i, j, boardRowSize, boardColSize))
			{
				return true;
			}
		}
	}
	return false;
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test word row1 row2...\n");
		exit(-1);
	}
	printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
static bool dfs(char *word, char **board, bool *used,
				int row, int col, int row_size, int col_size)
{
	if (board[row][col] != *word)
	{
		return false;
	}
	used[row * col_size + col] = true;
	if (*(word + 1) == '\0')
	{
		return true;
	}
	bool result = false;
	if (row > 0 && !used[(row - 1) * col_size + col])
	{
		result = dfs(word + 1, board, used, row - 1, col, row_size, col_size);
	}
	if (!result && row < row_size - 1 && !used[(row + 1) * col_size + col])
	{
		result = dfs(word + 1, board, used, row + 1, col, row_size, col_size);
	}
	if (!result && col > 0 && !used[row * col_size + col - 1])
	{
		result = dfs(word + 1, board, used, row, col - 1, row_size, col_size);
	}
	if (!result && col < col_size - 1 && !used[row * col_size + col + 1])
	{
		result = dfs(word + 1, board, used, row, col + 1, row_size, col_size);
	}
	used[row * col_size + col] = false;
	return result;
}
static bool exist(char **board, int boardRowSize, int boardColSize, char *word)
{
	int i, j;
	int len = strlen(word);
	if (len > boardRowSize * boardColSize)
	{
		return false;
	}
	bool *used = malloc(boardRowSize * boardColSize);
	for (i = 0; i < boardRowSize; i++)
	{
		for (j = 0; j < boardColSize; j++)
		{
			memset(used, false, boardRowSize * boardColSize);
			if (dfs(word, board, used, i, j, boardRowSize, boardColSize))
			{
				return true;
			}
		}
	}
	return false;
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test word row1 row2...\n");
		exit(-1);
	}
	printf("%s\n", exist(argv + 2, argc - 2, strlen(argv[2]), argv[1]) ? "true" : "false");
	return 0;
}
```

## 答案

```cpp
result = dfs(word + 1, board, used, row, col + 1, row_size, col_size);
```

## 选项

### A

```cpp
result = dfs(word - 1, board, used, row, col + 1, row_size, col_size);
```

### B

```cpp
result = dfs(word + 1, board, used, row, col - 1, row_size, col_size);
```

### C

```cpp
result = dfs(word - 1, board, used, row, col - 1, row_size, col_size);
```