[LeetCode][JavaScript]Word Search

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 =

[
  ['A','B','C','E'],
  ['S','F','C','S'],
  ['A','D','E','E']
]

word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.

https://leetcode.com/problems/word-search/
 
 
 

 
 
找单词,DFS。
每一步都可以上下左右四个方向走,开一个数组visited记录已经走过的格子。
 
 1 /**
 2  * @param {character[][]} board
 3  * @param {string} word
 4  * @return {boolean}
 5  */
 6 var exist2 = function(board, word) {
 7     if(word === "") return true;
 8     if(board.length === 0) return word === "" ? true : false;
 9     var i, j, rowLen = board.length, colLen = board[0].length, visited = [];
10     for(i = 0; i < rowLen; i++)    visited[i] = [];
11     for(i = 0; i < board.length; i++)
12         for(j = 0; j < colLen; j++)
13             if(board[i][j] === word[0]) 
14                 if(dfs(0, i, j)) return true;
15     return false;
16     
17     function dfs(index, x, y){
18         if(index === word.length - 1) return true;
19         visited[x][y] = true;
20         if(dfsNeighbor(index, x - 1, y)) return true; //up
21         if(dfsNeighbor(index, x + 1, y)) return true; //down
22         if(dfsNeighbor(index, x, y - 1)) return true; //left
23         if(dfsNeighbor(index, x, y + 1)) return true; //right
24         visited[x][y] = false;
25         return false;
26     }
27     function dfsNeighbor(index, x, y){
28         if(!board[x] || !board[x][y]) return false;
29         if(!visited[x][y] && board[x][y] === word[index + 1]){
30             return dfs(index + 1, x, y);
31         }
32         return false;
33     }
34 };

 

优化了一下,不用开visited数组,直接用入参的board记录是否访问过。

 

 1 /**
 2  * @param {character[][]} board
 3  * @param {string} word
 4  * @return {boolean}
 5  */
 6 var exist = function(board, word) {
 7     if(word === "") return true;
 8     var i, j;
 9     for(i = 0; i < board.length; i++)
10         for(j = 0; j < board[i].length; j++)
11             if(board[i][j] === word[0]) 
12                 if(dfs(0, i, j)) return true;
13     return false;
14     
15     function dfs(index, x, y){
16         if(index === word.length) return true;
17         if(!board[x] || !board[x][y]) return false;
18         if(board[x][y] !== '#' && board[x][y] === word[index]){
19             var ch = board[x][y];
20             board[x][y] = '#';
21             if(dfs(index + 1, x - 1, y)) return true; //up
22             if(dfs(index + 1, x + 1, y)) return true; //down
23             if(dfs(index + 1, x, y - 1)) return true; //left
24             if(dfs(index + 1, x, y + 1)) return true; //right
25             board[x][y] = ch;
26         }
27         return false;
28     }
29 };

 

 
posted @ 2015-12-03 16:25  `Liok  阅读(1044)  评论(0编辑  收藏  举报