leetcode Word Search 待解决?

 终于搞定了这个DFS,最近这个DFS写的很不顺手,我一直以为递归这种东西只是在解重构时比较麻烦,现在看来,连最简单的返回true和false的逻辑关系都不能说one hundred present 搞定。

人品啊TLE:

 1 class Solution {

 2 public:

 3 bool legal(int i, int j, vector<vector<char>> board)

 4 {

 5     if (i >= 0 && i < board.size() && j >= 0 && j < board[i].size())

 6         return true;

 7     return false;

 8 }

 9 

10    bool dfs(vector<vector<char>>& board, string word, int t, int i, int j, vector<vector<bool>> visited)

11 {

12     if (t == word.length())

13         return true;

14     if (board[i][j] == word[t])

15     {

16         visited[i][j] = true;

17         if (legal(i - 1, j, board) && !visited[i - 1][j] && dfs(board, word, t + 1, i - 1, j, visited))

18             return true;

19         if (legal(i, j + 1, board) && !visited[i][j + 1] && dfs(board, word, t + 1, i, j + 1, visited))

20             return true;

21         if (legal(i + 1, j, board) && !visited[i + 1][j] && dfs(board, word, t + 1, i + 1, j, visited))

22             return true;

23         if (legal(i, j - 1, board) && !visited[i][j - 1] && dfs(board, word, t + 1, i, j - 1, visited))

24             return true;

25     }

26     visited[i][j] = false;

27     return false;

28 }

29 

30 bool exist(vector<vector<char>>& board, string word)

31 {

32     int m = board.size();

33     int n = board[0].size();

34     vector<vector<bool>> visited(m, vector<bool>(n, false));

35     for (int i = 0; i < board.size(); i++)

36     {

37         for (int j = 0; j < board[0].size(); j++)

38         {

39             if (dfs(board, word, 0, i, j, visited))

40                 return true;

41         }

42     }

43     return false;

44 }

45 };
View Code

 为什么?

下面的依然是超时的, 无论怎么尝试都是超时的,觉得和提交的AC代码并没有什么区别啊?

 1 #include<iostream>

 2 #include<vector>

 3 

 4 using namespace std;

 5 

 6 

 7 bool isLegal(int i, int j, vector<vector<char>> board)

 8 {

 9     int H = board.size();

10     int L = board[0].size();

11     if (i >= 0 && i < H&&j >= 0 && j < L)

12         return true;

13     return false;

14 }

15 

16 bool searchWord(vector<vector<char>> board, vector<vector<bool>> visited, string word, int i, int j, int index)

17 {

18     if (index >= word.length())

19     {

20         return true;

21     }

22     /*if (word[index] == board[i][j])

23     {*/

24         visited[i][j] = true;

25         int dx[4] = { 1, 0, -1, 0 };

26         int dy[4] = { 0, 1, 0, -1 };

27 

28         for (int x = 0; x < 4; ++x)

29         {

30             int ii = i + dx[x];

31             int jj = j + dy[x];

32             if (isLegal(ii, jj, board) && !visited[ii][jj] && board[ii][jj] == word[index] && searchWord(board, visited, word, ii, jj, index + 1))

33                 return true;

34         }

35 

36         /*if (isLegal(i - 1, j, board) && !visited[i - 1][j] && board[i - 1][j]==word[index] && searchWord(board, visited, word, i - 1, j, index + 1))

37             return true;

38         if (isLegal(i, j - 1, board) && !visited[i][j - 1] && board[i][j - 1] == word[index] && searchWord(board, visited, word, i, j - 1, index + 1))

39             return true;

40         if (isLegal(i + 1, j, board) && !visited[i + 1][j] && board[i + 1][j] == word[index] && searchWord(board, visited, word, i + 1, j, index + 1))

41             return true;

42         if (isLegal(i, j + 1, board) && !visited[i][j + 1] && board[i][j + 1] == word[index] && searchWord(board, visited, word, i, j + 1, index + 1))

43             return true;*/

44     /*}*/

45     visited[i][j] = false;

46     return false;

47 }

48 

49 bool exist(vector<vector<char>>& board, string word) {

50     if (word.empty()) return false;

51     int H = board.size();

52     int L = board[0].size();

53     int i, j;

54     vector<vector<bool>> visited(H, vector<bool>(L, false));

55     for (i = 0; i < H; i++)

56     {

57         for (j = 0; j < L; j++)

58         {

59             if (word[0]==board[i][j]&&searchWord(board, visited, word, i, j, 1))

60             {

61                 return true;

62             }

63         }

64     }

65     return false;

66 }

67 

68 

69 int main()

70 {

71     char boardArray[][4] =

72     {

73         { 'A', 'B', 'C', 'E' },

74         { 'S', 'F', 'C', 'S' },

75         { 'A', 'D', 'E', 'E' }

76     };

77     vector<vector<char>> board1(3);

78     for (int i = 0; i < 3; i++)

79     {

80         vector<char> temp(4);

81         temp[0] = 'A';

82         board1[i] = temp;

83         for (int j = 0; j < 4; j++)

84         {

85             board1[i][j] = boardArray[i][j];

86         }

87     }

88 

89     if (exist(board1, "ABCCED"))

90         cout << 1<<endl;

91     else

92         cout << 0 << endl;

93 }
View Code

 

你可能感兴趣的:(LeetCode)