# Share my C++ solution,easy to understand

• ``````class Solution {
public:
bool exist(vector<vector<char>>& board, string word) {
int row = board.size();
if (row == 0)
return false;

int col = board[0].size();
if (col == 0)
return false;

int len = word.length();
if (len == 0)
return false;

vector<vector<int>> isVisit(row, vector<int>(col, 0));
int i = 0, j = 0;

for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
if (search(board, word, isVisit, i, j))
return true;
}
}

return false;
}

bool search(vector<vector<char>>&board, string word, vector<vector<int>>& isVisit, int x, int y)
{
int len = word.length();
bool ret;

if (x < 0 || y < 0 || x >= board.size() || y >= board[0].size())
return false;
if (isVisit[x][y] == 1)
return false;
if (board[x][y] != word[0])
return false;
if (len == 1)
return true;

isVisit[x][y] = 1;
word = word.substr(1);

ret = search(board, word, isVisit, x-1, y) || search(board, word, isVisit, x+1, y) || search(board, word, isVisit, x, y-1) || search(board, word, isVisit, x, y+1);
isVisit[x][y] = 0;

return ret;
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.