- Leetcode - Word Search
	- Times:
		- Time when completed: 10:47
		- Time taken to complete: 30mins
		- LATER Revisions:
		  SCHEDULED: <2023-05-02 Tue ++2w>
	- Tags:
		- Algorithms: #DFS #DFS_preorder #backtrack
		- Data structures: #vector_2d
		- Difficulty: #difficulty_medium
		- Platforms: #leetcode
	- Links:
		- [Link to the problem](https://leetcode.com/problems/word-search/description/)
	- Problem:
		-
	- Examples:
		- ```
		  
		  ```
	- Constraints:
		- to-be-filled
	- Thoughts:
		- Intuition:
			- The solution is simple, first loop over the board to find a starting point, then start dfs there.
			- The only caveat is remember to add a simple backtracking to prevent the case when a route is invalid, but still used the visited[][] array.
		- Approach:
			- Loop over the board
			- DFS from that point
	- Solution:
		- Code
			- ``` java
			  class Solution {
			      boolean[][] visited;
			      boolean dfs(char[][] board, String word, int x, int y, int start) {
			          if (start == word.length()) {
			              return true;
			          }
			          if (x < 0 || x >= board.length || y < 0 || y >= board[0].length) {
			              return false;
			          }
			          if (visited[x][y]) {
			              return false;
			          }
			          if (board[x][y] == word.charAt(start)) {
			              visited[x][y] = true;
			              boolean found = dfs(board, word, x + 1, y, start + 1) ||
			              dfs(board, word, x - 1, y, start + 1) ||
			              dfs(board, word, x, y + 1, start + 1) ||
			              dfs(board, word, x, y - 1, start + 1);
			              if (!found) { // the backtracking
			                  visited[x][y] = false;
			                  return false;
			              } else {
			                  return true;
			              }
			          } else {
			              return false;
			          }
			      }
			      public boolean exist(char[][] board, String word) {
			          visited = new boolean[board.length][board[0].length];
			          // first, loop over the problems, then use dfs.
			          for (int i = 0; i < board.length; i++) {
			              for (int j = 0; j < board[0].length; j++) {
			                  if (word.charAt(0) == board[i][j]) {
			                      visited = new boolean[board.length][board[0].length];
			                      if (dfs(board, word, i, j, 0)) {
			                          return true;
			                      }
			                  }
			              }
			          }
			          return false;
			      }
			  }
			  ```
-