Word search
Title description: Given a mxn two-dimensional character grid board and a string word word. If word exists in the grid, return true; otherwise, return false.
Words must be in alphabetical order and formed by letters in adjacent cells, where "adjacent" cells are those that are adjacent horizontally or vertically. Letters in the same cell are not allowed to be reused.
Please refer to LeetCode official website for example description.
Source: LeetCode
Link: https://leetcode-cn.com/problems/word-search/
The copyright belongs to Lingkou Network. For commercial reprints, please contact the official authorization. For non-commercial reprints, please indicate the source.
Solution 1: Backtracking algorithm
First of all, directly judge 2 kinds of special scenes:
- If the string to be matched is empty, return true directly;
- If the board array is empty, return false directly.
Otherwise, first declare a boolean array of the same size as the board, record whether the current cell has been passed, and then traverse each character of the board, and call the backtracking method when each character is equal to the first character of the word Determine whether the word string can be matched with the current character as the starting point, if it can return true, otherwise continue to traverse the next character. Finally, if there is no match, false is returned.
public class LeetCode_079 {
public static boolean exist(char[][] board, String word) {
/**
* 如果要匹配的字符串为空,直接返回true
*/
if (word == null || word.length() == 0) {
return true;
}
/**
* 如果board数组为空,直接返回false
*/
if (board == null || board.length == 0 || board[0].length == 0) {
return false;
}
/**
* 声明一个和board同样大小的boolean类型的数组,记录当前单元格是否已经走过
*/
boolean[][] visited = new boolean[board.length][board[0].length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
/**
* 对每一个字符和word第一个字符相等的时候,调用方法进行判断
*/
if (board[i][j] == word.charAt(0) && exist(board, visited, word, i, j, 0)) {
return true;
}
}
}
return false;
}
/**
* 回溯算法
*
* @param board 原字符网格
* @param visited 和board大小相同的boolean类型的网格,标识当前字符是否走过
* @param word 要匹配的单词
* @param startX 当前单元格的x坐标
* @param startY 当前单元格的y坐标
* @param pos 当前已经匹配了几个字符
* @return
*/
private static boolean exist(char[][] board, boolean[][] visited, String word, int startX, int startY, int pos) {
if (board[startX][startY] != word.charAt(pos)) {
// 如果当前单元格和要匹配的字符不同,直接返回false
return false;
} else if (pos == word.length() - 1) {
// 如果已经匹配的字符数和word的长度相等,则说明已经匹配成功,返回true
return true;
}
visited[startX][startY] = true;
// 当前单元格可以往四个方向移动
int[][] directions = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
boolean result = false;
for (int[] dir : directions) {
int nextStartX = startX + dir[0], nextStartY = startY + dir[1];
if (nextStartX >= 0 && nextStartX < board.length && nextStartY >= 0 && nextStartY < board[0].length) {
if (!visited[nextStartX][nextStartY]) {
boolean flag = exist(board, visited, word, nextStartX, nextStartY, pos + 1);
if (flag) {
result = true;
break;
}
}
}
}
visited[startX][startY] = false;
return result;
}
public static void main(String[] args) {
char[][] board = new char[][]{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
// 测试用例,期望返回: true
System.out.println(exist(board, "ABCCED"));
}
}
[Daily Message] Adversity, right and wrong come, you must hold the word "wide" in your heart.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。