LeetCode 79(Word Search)java

原题: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.

给定一个二维矩阵和一个字符串,判断该字符串能否由矩阵中相邻元素排列而成,矩阵中的相邻意味着上下左右的相邻;

思路:将矩阵看作一张图,这道题转化为对这个图的某些节点进行深度优先搜索的问题(某些节点指的是满足该节点值等于字符串首位字符)。

为了防止重复的搜索以及剪枝的需要,我们设置一张表,用于记录矩阵中某个位置是否被访问过,若访问过,则不能再沿着该路径搜索;

当字符串中所有字符都符合要求,即下一次要搜索word.charAt(word.length())时,搜索完成,找到一组可行的解,将全局变量置为真,之后返回该全局变量;

构建DFS函数:public void DFS(char[][] board,boolean[][] isVisited,String word,int index,int row,int col);

其中index代表下次要搜索的索引,row和col代表下次的搜索从这里开始(搜索他们相邻的点);

事实证明,这并不是很好的写法,更好的写法可以参考这篇博客:http://blog.csdn.net/happyaaaaaaaaaaa/article/details/50834335

我也从中收获很多;


还是放一下自己的代码:

/*
* Author:Jassy
* Time:2017/01/04
* Number:LeetCode 79(Word Search)
* Source:https://leetcode.com/problems/word-search/
* Result:AC
* Conclusion:DFS
*/
public class Solution {
    boolean resultFlag=false;
    public boolean exist(char[][] board, String word) {
        int row=board.length;
        int col=board[0].length;
        
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(board[i][j]==word.charAt(0)){
                    boolean[][] isVisited=new boolean[row][col];
                    isVisited[i][j]=true;
                    DFS(board,isVisited,word,1,i,j);
                    if(resultFlag==true){
                        return true;
                    }
                }
            }
        }
        return false;
    }
    
    public void DFS(char[][] board,boolean[][] isVisited,String word,int index,int row,int col){
        if(index==word.length()){
            resultFlag=true;
            return;
        }
        if(row-1>=0 && board[row-1][col]==word.charAt(index) && isVisited[row-1][col]==false && resultFlag==false){
            isVisited[row-1][col]=true;
            DFS(board,isVisited,word,index+1,row-1,col);
            isVisited[row-1][col]=false;
        }
        if(row+1<=board.length-1 && board[row+1][col]==word.charAt(index) && isVisited[row+1][col]==false && resultFlag==false){
            isVisited[row+1][col]=true;
            DFS(board,isVisited,word,index+1,row+1,col);
            isVisited[row+1][col]=false;
        }
        if(col-1>=0 && board[row][col-1]==word.charAt(index) && isVisited[row][col-1]==false && resultFlag==false){
            isVisited[row][col-1]=true;
            DFS(board,isVisited,word,index+1,row,col-1);
            isVisited[row][col-1]=false;
        }
        if(col+1<=board[0].length-1 && board[row][col+1]==word.charAt(index) && isVisited[row][col+1]==false && resultFlag==false){
            isVisited[row][col+1]=true;
            DFS(board,isVisited,word,index+1,row,col+1);
            isVisited[row][col+1]=false;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值