leetcode_212:单词搜索II

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sword_anyone/article/details/80547544

题目:
这里写图片描述


我的思路:使用回溯,重要是剪枝,这点可以让程序不用花太多时间做无用功。


我的解答:

class Solution {

    public   List<String> findWords(char[][] board, String words[]) {
        List<String>  finals = new ArrayList<>();
        int used[][] = new int[board.length][board[0].length];
        Map<String,List<String>>   unity = new HashMap<>();

        for(int k=0;k<words.length;k++) {
            String word = words[k];
            List<String> position = new ArrayList<>();
            for (int i = 0; i < board.length; i++) {
                for (int j = 0; j < board[i].length; j++) {
                    if (word.charAt(0) == board[i][j]) {
                        position.add(i + "--" + j);
                    }
                }
            }

            for (int i = 0; i < position.size(); i++) {
                String str[] = position.get(i).split("--");
                int x = Integer.parseInt(str[0]);
                int y = Integer.parseInt(str[1]);

                used[x][y] = 1;//已被使用

                if (move(used, board, x, y, word, 1)) {
                    if(!finals.contains(word)){
                        finals.add(word);
                    }
                    used = new int[board.length][board[0].length];
                }
                used[x][y] = 0;//已被使用
            }
        }
        return finals;
    }

    private static boolean move(int[][] used, char[][] board, int x, int y,String word,int index) {
        if(word.length()>used.length*used[0].length){
            return false;
        }

        if(word.length()==index){
            return true;
        }

        //1是左,2是下,3是→,4是上
        for(int i=1;i<=4;i++){
            switch (i){
                case 1:
                    if(y-1<0){
                        continue;
                    }else if(y-1>=0){
                        if(word.charAt(index)==board[x][y-1]){
                            if(used[x][y-1]==1){
                                continue;
                            }
                            used[x][y-1] = 1;
                            if(move(used,board,x,y-1,word,index+1)){
                                return true;
                            }
                            used[x][y-1] = 0;
                        }else{
                            continue;
                        }
                    }break;
                case 2:
                    if(x+1>=board.length){
                        continue;
                    }else if(x+1<board.length){
                        if(used[x+1][y]==1){
                            continue;
                        }
                        if(word.charAt(index)==board[x+1][y]){
                            used[x+1][y] = 1;
                            if(move(used,board,x+1,y,word,index+1)){
                                return true;
                            }
                            used[x+1][y] = 0;
                        }else{
                            continue;
                        }
                    }break;
                case 3:
                    if(y+1>=board[0].length){
                        continue;
                    }else {
                        if(word.charAt(index)==board[x][y+1]){
                            if(used[x][y+1]==1){
                                continue;
                            }
                            used[x][y+1] = 1;
                            if(move(used,board,x,y+1,word,index+1)){
                                return true;
                            }
                            used[x][y+1] = 0;
                        }
                    }break;
                case 4:
                    if(x-1<0){
                        continue;
                    }else {
                        if(word.charAt(index)==board[x-1][y]){
                            if(used[x-1][y]==1){
                                continue;
                            }
                            used[x-1][y] = 1;
                            if(move(used,board,x-1,y,word,index+1)){
                                return true;
                            };
                            used[x-1][y] = 0;
                        }
                    }break;
            }
        }
        return false;
    }

}

这种类型的题目,我一眼看到就用了回溯法,可能剪枝不够好吧,或者也许有更好的解法,导致解这一题花了952ms,挺长时间的。如果有更好的解法,还请大神不啬教诲。

猜你喜欢

转载自blog.csdn.net/sword_anyone/article/details/80547544