I set the value of board[x][y] to '#' when it is used. In this way, I can save the memory to check whether board[x][y] is used.

```
class Solution {
public:
void helper(vector<vector<char> > &board, string word, bool &res, int &m, int &n, int x, int y, int &k)
{
if (k == word.size())
{
res = true;
return;
}
else
{
if (x > 0 && board[x - 1][y] == word[k])
{
board[x - 1][y] = '#';
helper(board, word, res, m, n, x - 1, y, ++k);
if (res) return;//return early to save time
board[x - 1][y] = word[--k];//restore board.
}
if (x < m - 1 && board[x + 1][y] == word[k])
{
board[x + 1][y] = '#';
helper(board, word, res, m, n, x + 1, y, ++k);
if (res) return;
board[x + 1][y] = word[--k];
}
if (y > 0 && board[x][y - 1] == word[k])
{
board[x][y - 1] = '#';
helper(board, word, res, m, n, x, y - 1, ++k);
if (res) return;
board[x][y - 1] = word[--k];
}
if (y < n - 1 && board[x][y + 1] == word[k])
{
board[x][y + 1] = '#';
helper(board, word, res, m, n, x, y + 1, ++k);
if (res) return;
board[x][y + 1] = word[--k];
}
}
}
bool exist(vector<vector<char> > &board, string word) {
bool res = false;
int x, y;
int k = 0;
int m = board.size(), n = board[0].size();
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if (board[i][j] == word[k])
{
board[i][j] = '#';
helper(board, word, res, m, n, i, j, ++k);
if (res) return true;
board[i][j] = word[--k];
}
return res;
}
};
```