Word Search
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[ ["ABCE"], ["SFCS"], ["ADEE"] ]
word = "ABCCED"
, -> returns true
,
word = "SEE"
, -> returns true
,
word = "ABCB"
, -> returns false
.
这道题虽然代码写得有点长,但是思路还是比较清晰的
思路:
遍历word从0-length - 1,找出在board中的位置i,j从i,j的上下左右开始递归查找下一个字符。由于字符在一次匹配中不能使用两次,这里用了一个isVisited[][]进行访问控制。
1 public class Solution { 2 private boolean isExist = false; 3 private boolean isVisited[][]; 4 5 public boolean exist(char[][] board, String word) { 6 if(word.length() == 0) 7 return false; 8 int position_x; 9 int position_y; 10 char firstChar = word.charAt(0); 11 isVisited = new boolean[board.length][board[0].length]; 12 13 for(int i = 0; i < board.length; i++){ 14 if(isExist == true){ 15 break; 16 } 17 for(int j = 0; j < board[0].length; j++){ 18 if(isExist == true){ 19 break; 20 } 21 if(firstChar == board[i][j]){ 22 position_x = i; 23 position_y = j; 24 String curWord = String.valueOf(firstChar); 25 isVisited[position_x][position_y] = true; 26 isExit(board, word, 0, position_x, position_y, curWord); 27 initVisited(); 28 }//if 29 }//for 30 }//for 31 32 return isExist; 33 } 34 private void isExit(char [][]board, String word, int i, int position_x, int position_y, String curWord){ 35 // System.out.println("curWord = " + curWord); 36 if(curWord.length() == word.length()){ 37 if(board[position_x][position_y] == word.charAt(word.length() - 1)) 38 isExist = true; 39 return; 40 }else{ 41 if(isExist) 42 return; 43 if(board[position_x][position_y] == word.charAt(i)){ //遍历上线左右满足条件的 44 if(position_x - 1 >= 0 && isVisited[position_x - 1][position_y] == false){ 45 isVisited[position_x - 1][position_y] = true; 46 isExit(board, word, i + 1, position_x - 1, position_y, curWord + String.valueOf(word.charAt(i + 1))); //上 47 isVisited[position_x - 1][position_y] = false; 48 } 49 if(position_x + 1 < board.length && isVisited[position_x + 1][position_y] == false){ 50 isVisited[position_x + 1][position_y] = true; 51 isExit(board, word, i + 1, position_x + 1, position_y, curWord + String.valueOf(word.charAt(i + 1))); //下 52 isVisited[position_x + 1][position_y] = false; 53 } 54 if(position_y - 1 >= 0 && isVisited[position_x][position_y - 1] == false){ 55 isVisited[position_x][position_y - 1] = true; 56 isExit(board, word, i + 1, position_x, position_y - 1, curWord + String.valueOf(word.charAt(i + 1))); //左 57 isVisited[position_x][position_y - 1] = false; 58 } 59 if(position_y + 1 < board[0].length && isVisited[position_x][position_y + 1] == false){ 60 isVisited[position_x][position_y + 1] = true; 61 isExit(board, word, i + 1, position_x, position_y + 1, curWord + String.valueOf(word.charAt(i + 1))); //右 62 isVisited[position_x][position_y + 1] = false; 63 } 64 } 65 } 66 } 67 private void initVisited(){ 68 for(int i = 0; i < isVisited.length;i++){ 69 for(int j = 0; j < isVisited[0].length; j++){ 70 isVisited[i][j] = false; 71 } 72 } 73 } 74 }
Please call me JiangYouDang!