方法1—回溯

很简单的回溯题

给出两种实现方式,思路完全一样的。

c++代码

  1. class Solution {
  2. public:
  3. int flag;
  4. vector<vector<char>> vocab;
  5. vector<vector<int>> vv;
  6. void dfs(int i, int j, int bottom, int right, string word, int idx){
  7. if(idx == word.size()){
  8. flag = 1;
  9. return;
  10. }
  11. if(i < 0 || i > bottom || j < 0 || j > right) return;
  12. if(flag==1) return;
  13. if(j < right && vv[i][j+1] == 0 && vocab[i][j+1] == word[idx]){
  14. vv[i][j+1] = 1;
  15. dfs(i, j+1, bottom, right, word, idx+1);
  16. vv[i][j+1] = 0;
  17. }
  18. if(flag==1) return;
  19. if(j > 0 && vv[i][j-1] == 0 && vocab[i][j-1] == word[idx]){
  20. vv[i][j-1] = 1;
  21. dfs(i, j-1, bottom, right, word, idx+1);
  22. vv[i][j-1] = 0;
  23. }
  24. if(flag==1) return;
  25. if(i > 0 && vv[i-1][j] == 0 && vocab[i-1][j] == word[idx]){
  26. vv[i-1][j] = 1;
  27. dfs(i-1, j, bottom, right, word, idx+1);
  28. vv[i-1][j] = 0;
  29. }
  30. if(flag==1) return;
  31. if(i < bottom && vv[i+1][j] == 0 && vocab[i+1][j] == word[idx]){
  32. vv[i+1][j] = 1;
  33. dfs(i+1, j, bottom, right, word, idx+1);
  34. vv[i+1][j] = 0;
  35. }
  36. }
  37. bool exist(vector<vector<char>>& board, string word) {
  38. vocab = board;
  39. flag = 0;
  40. int raw = board.size(), column = board[0].size();
  41. vector<int> v(column, 0);
  42. for(int i=0; i<raw; i++) vv.push_back(v);
  43. for(int i=0; i<raw; i++){
  44. for(int j=0; j<column; j++){
  45. if(board[i][j] == word[0]){
  46. vv[i][j] = 1;
  47. // 当前元素的行、列,board的下边界、右边界,单词及索引
  48. dfs(i, j, raw-1, column-1, word, 1);
  49. vv[i][j] = 0;
  50. }
  51. if(flag) return true;
  52. }
  53. }
  54. return false;
  55. }
  56. };

java代码

class Solution {
    private boolean[][] marked;
    private int[][] direction = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    private int raw, column;   //board的行,列
    private char[][] board;
    private String word;

    boolean inArea(int i, int j){
        if(i >= 0 && i < raw && j >= 0 && j < column)
            return true;
        return false;
    }

    boolean dfs(int i, int j, int idx){
        if(idx == word.length()-1)
            return board[i][j] == word.charAt(idx);

        if(board[i][j] == word.charAt(idx)){
            marked[i][j] = true;
            for(int k=0; k<4; k++){
                int new_x = i + direction[k][0];
                int new_y = j + direction[k][1];
                if(inArea(new_x, new_y) && !marked[new_x][new_y]){
                    if(dfs(new_x, new_y, idx+1))    return true;
                }
            }
            marked[i][j] = false;
        }
        return false;
    }

    public boolean exist(char[][] board, String word) {
        this.word = word;
        this.board = board;
        raw = board.length;
        column = board[0].length;
        marked = new boolean[raw][column];

        for(int i=0; i<raw; i++){
            for(int j=0; j<column; j++){
                if(dfs(i, j, 0))    return true;
            }
        }
        return false;

    }
}