Leetcode_79 单词搜索

题目描述

这里写图片描述

这题目代码稍微多了一些,不过思路也很清晰

题解

java

import java.util.*;

public class Leetcode_79 {
    private int m;
    private int n;
    private boolean[][] visited;
    int[][] d = {{-1,0},{0,1},{1,0},{0,-1}};


    private boolean isArea(int x, int y)
            //判断坐标是否越界
    {
        return x>=0 && x<m && y>=0 && y<n;
    }


    public boolean exist(char[][] board, String word) {
        m = board.length;
        n = board[0].length;
        visited = new boolean[m][n];
        for(int i = 0; i<m; i++)
            for(int j = 0; j<n; j++) {
                //遍历整个矩阵,寻找第一个字母的匹配
                if (searchWord(board, word, 0, i, j))
                    return true;
            }
        return false;


    }



    private boolean searchWord(char[][]board,String word, int index, int startx, int starty)
    {
        //终止条件
        if(index == word.length()-1)
            return board[startx][starty] == word.charAt(word.length()-1);

        //递归主体
        if(board[startx][starty] == word.charAt(index))
        {
            visited[startx][starty] = true;
            for(int i = 0; i<4; i++)
            {
                int newx = startx+d[i][0];
                int newy = starty+d[i][1];
                //坐标不越界,同时没有访问过,然后再看下一个index的字母是否检测到
                if(isArea(newx,newy)&&!visited[newx][newy] &&
                    searchWord(board,word,index+1,newx,newy))
                        return true;
            }
            visited[startx][starty] = false;

        }

        return false;
    }



    public static void main(String[] args)
    {
        Leetcode_79 test = new Leetcode_79();
        char[][] board = {{'A','B','C','E'},{'S','F','C','S'},{'A','D','E','E'}};
        String word = "ABCC";
        boolean ret = test.exist(board,word);


        System.out.println(ret);
    }


}

思路完全一模一样的cpp代码

class Solution {
private:
    int m,n;
    vector<vector<bool>> visited;
    int d[4][2] = {{-1, 0}, {0,1}, {1, 0}, {0, -1}};

    bool inArea( int x , int y ){
        return x >= 0 && x < m && y >= 0 && y < n;
    }


    //start from board[startx][starty], find word[index...word.size())
    bool searchWord( const vector<vector<char>> &board, const string& word, int index,
                    int startx, int starty )
    {
        if(index == word.size()-1)
            return board[startx][starty] == word[index];
        if(board[startx][starty] == word[index])
        {
            visited[startx][starty] = true;
            for(int i = 0;i<4;i++)
            {
                int newx = startx+d[i][0];
                int newy = starty+d[i][1];
                if(inArea(newx,newy)&&!visited[newx][newy]&&searchWord(board,word,index+1,newx,newy))
                    return true;

            }
            visited[startx][starty] = false;
        }
        return false;
    }

public:
    bool exist(vector<vector<char>>& board, string word) {
        m = board.size();
        assert( m > 0 );
        n = board[0].size();
        visited = vector<vector<bool>>(m, vector<bool>(n, false));
        for(int i = 0; i<board.size();i++)
            for(int j = 0; j<board[i].size();j++)
                if(searchWord(board,word,0,i,j))
                    return true;
        return false;

    }
};

猜你喜欢

转载自blog.csdn.net/Ding_xiaofei/article/details/82463347