<p>Given an <code>m x n</code> grid of characters <code>board</code> and a string <code>word</code>, return <code>true</code> <em>if</em> <code>word</code> <em>exists in the grid</em>.</p>

<p>The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word2.jpg" style="width: 322px; height: 242px;" />
<pre>
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;ABCCED&quot;
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg" style="width: 322px; height: 242px;" />
<pre>
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;SEE&quot;
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/15/word3.jpg" style="width: 322px; height: 242px;" />
<pre>
<strong>Input:</strong> board = [[&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;E&quot;],[&quot;S&quot;,&quot;F&quot;,&quot;C&quot;,&quot;S&quot;],[&quot;A&quot;,&quot;D&quot;,&quot;E&quot;,&quot;E&quot;]], word = &quot;ABCB&quot;
<strong>Output:</strong> false
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == board.length</code></li>
	<li><code>n = board[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 6</code></li>
	<li><code>1 &lt;= word.length &lt;= 15</code></li>
	<li><code>board</code> and <code>word</code> consists of only lowercase and uppercase English letters.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong> Could you use search pruning to make your solution faster with a larger <code>board</code>?</p>

class Solution {
private:
    unordered_map<char, vector<pair<int, int>>> char_pos;

    void init(const vector<vector<char>>& board) {
        for (int i = 0; i < board.size(); ++i)
            for (int j = 0; j < board[i].size(); ++j)
                char_pos[board[i][j]].push_back({i, j});
    }

    bool check_word(const string &word) {
        for (char c: word) {
            if (char_pos.find(c) == char_pos.end())
                return false;
        }
        return true;
    }

    bool dfs(int i, int j, const string &word, 
    set<pair<int, int>> &visited, 
    const vector<vector<char>>& board, int pos = 0) {
        if (i < 0 || i >= board.size()) return false;
        if (j < 0 || j >= board[i].size()) return false;
        if (board[i][j] != word[pos]) return false;
        if (visited.find({i, j}) != visited.end())
            return false;
        if (pos == word.size() - 1) return true;

        visited.insert({i, j});
        // cout << "add " << word[pos] << "\n";

        bool flag = dfs(i+1, j, word, visited, board, pos+1)
            || dfs(i-1, j, word, visited, board, pos+1)
            || dfs(i, j+1, word, visited, board, pos+1)
            || dfs(i, j-1, word, visited, board, pos+1);

        visited.erase({i, j});
        // cout << "remove " << word[pos] << "\n";

        return flag;
    }

public:
    bool exist(vector<vector<char>>& board, string word) {
        if (word.size() == 0) return true;

        init(board);

        if (!check_word(word)) return false;

        char zero_char = word[0];

        for (auto [i, j]: char_pos[zero_char]) {
            set<pair<int, int>> visited;

            if (dfs(i, j, word, visited, board)) 
                return true;
        }

        return false;
    }
};
183406