Word Search

二维矩阵单词搜索算法实现
本文介绍了一种在二维矩阵中查找指定单词的算法实现,包括递归深度优先搜索(DFS)方法,以及如何处理边界条件和重复字符使用的问题。

1. Title

Word Search

2.   Http address

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

3. The question

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.

4. My code (AC)

 1 public class WordSearch {
 2     
 3     public static void main(String[] args) {
 4         
 5     }
 6 
 7     public boolean DFS(char[][] board,int i, int j, String word,  boolean [][]visited) {
 8         
 9         if( word == null || word.equals("") )
10                 return true;
11 
12         if( word.length() == 1 && word.charAt(0) == board[i][j] )
13         {
14         //        System.out.println(true);    
15                 return true;
16         }
17         if( word.length() == 1 && word.charAt(0) != board[i][j] )
18                 return false;
19         int x = 0 , y = 0;
20         String subStr = word.substring(1);
21 
22         visited[i][j] = true;
23 
24         if( board[i][j] != word.charAt(0) )
25         {
26             visited[i][j] = false;
27             return false;
28         }
29 
30         //up
31         x = i - 1;
32         y = j;
33         if( x >= 0  && visited[x][y] == false)
34         {
35             if ( DFS(board, x, y, word.substring(1), visited) )
36                     return true;
37         }
38         //down
39         x = i + 1;
40         y = j;
41         if( x  < board.length && visited[x][y] == false)
42         {
43             if ( DFS(board, x, y, word.substring(1), visited) )
44                     return true;
45         }
46         //left
47         x = i;
48         y = j - 1;
49         if( y >= 0  && visited[x][y] == false)
50         {
51             if ( DFS(board, x, y, word.substring(1), visited) )
52                 return true;
53         }
54         //right
55         x = i;
56         y = j + 1;
57         if( y < board[0].length && visited[x][y] == false)
58         {
59             if ( DFS(board, x, y, word.substring(1), visited) )
60                 return true;
61         }
62         visited[i][j] = false;
63         return false;
64     }
65     // Accepted
66       public boolean exist(char[][] board, String word) {
67          
68           boolean [][]visited = new boolean[board.length][board[0].length];
69             for(int i = 0 ; i < board.length; i++ )
70             {
71 
72                 for(int j = 0 ; j < board[0].length; j++ )
73                 {
74                     if(  DFS(board, i , j, word, visited) )
75                             return true;
76                 }
77             }
78             return false;
79         }
80 }

 

转载于:https://www.cnblogs.com/ordili/p/4928287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值