<p>Given an <code>m x n</code> <code>board</code>&nbsp;of characters and a list of strings <code>words</code>, return <em>all words on the board</em>.</p>

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/07/search1.jpg" style="width: 322px; height: 322px;" />
<pre>
<strong>Input:</strong> board = [[&quot;o&quot;,&quot;a&quot;,&quot;a&quot;,&quot;n&quot;],[&quot;e&quot;,&quot;t&quot;,&quot;a&quot;,&quot;e&quot;],[&quot;i&quot;,&quot;h&quot;,&quot;k&quot;,&quot;r&quot;],[&quot;i&quot;,&quot;f&quot;,&quot;l&quot;,&quot;v&quot;]], words = [&quot;oath&quot;,&quot;pea&quot;,&quot;eat&quot;,&quot;rain&quot;]
<strong>Output:</strong> [&quot;eat&quot;,&quot;oath&quot;]
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/07/search2.jpg" style="width: 162px; height: 162px;" />
<pre>
<strong>Input:</strong> board = [[&quot;a&quot;,&quot;b&quot;],[&quot;c&quot;,&quot;d&quot;]], words = [&quot;abcb&quot;]
<strong>Output:</strong> []
</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;= 12</code></li>
	<li><code>board[i][j]</code> is a lowercase English letter.</li>
	<li><code>1 &lt;= words.length &lt;= 3 * 10<sup>4</sup></code></li>
	<li><code>1 &lt;= words[i].length &lt;= 10</code></li>
	<li><code>words[i]</code> consists of lowercase English letters.</li>
	<li>All the strings of <code>words</code> are unique.</li>
</ul>

class Solution {
public:
    struct Node
    {
        char val;
        unordered_map<char,vector<Node*>> adj;
    };
    struct wordNode
    {
        char val;
        unordered_map<char,wordNode*> adj;
    };
    unordered_map<char,vector<Node*>> um;
    unordered_set<string> ans;
    unordered_map<char, wordNode*> wordTree;
    void CreateNode(int i, int j, const vector<vector<char>>& board, vector<vector<Node*>>& nodeList)
    {
        if(nodeList[i][j] != nullptr)
        {
            return;
        }

        nodeList[i][j] = new Node;
        nodeList[i][j]->val = board[i][j];
        if(um.contains(board[i][j])==false)
        {
            um[board[i][j]] = vector<Node*>();
        }
        um[board[i][j]].push_back(nodeList[i][j] );
        static const int dx[4] = {1, -1, 0, 0};
        static const int dy[4] = {0,0,1,-1};
        for(int k = 0; k<4; k++)
        {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if(nx<0 || nx>=board.size() || ny <0 || ny >=board[0].size())
            {
                continue;
            }
            if(nodeList[nx][ny] == nullptr)
            {
                CreateNode(nx,ny, board, nodeList);
            }
            if(nodeList[i][j]->adj.contains(board[nx][ny]) == false)
            {
                nodeList[i][j]->adj[board[nx][ny]] = vector<Node*>();
            }
            nodeList[i][j]->adj[board[nx][ny]].push_back(nodeList[nx][ny]);
        }
    }
    void CreateTree(wordNode* node, string& word, int index)
    {
        if(index == word.size())
        {
            if(node->adj.contains(char(';')) == false)
            {
                node->adj[char(';')] = new wordNode;
                node->adj[char(';')]->val = ';';
            }
            return;
        }
        if(node->adj.contains(word[index]) == false)
        {
            node->adj[word[index]] = new wordNode;
            node->adj[word[index]]->val = word[index];
        }
        CreateTree(node->adj[word[index]], word, index+1);
    }
    // 인덱스는 현재 시점
    bool DFS(Node* node, wordNode* wNode, unordered_set<Node*>& s, string& ansWord)
    {
        if(wNode == nullptr)
        {
            return true;
        }
        if(wNode->adj.contains(char(';')))
        {
            ans.insert(ansWord);
            if(wNode->adj.size() == 1)
            {
                return true;
            }
        }
        for(auto& wn : wNode->adj)
        {
            if(wn.second == nullptr)
            {  
                continue;
            }
            for(Node* an : node->adj[wn.second->val])
            {
                if(s.contains(an))
                {
                    continue;
                }
                s.insert(an);
                string newWord = ansWord + an->val;

                if(DFS(an,wn.second, s, newWord))
                {
                    wn.second = nullptr;
                    s.erase(an);
                    break;
                }
                s.erase(an);
            }
        }

        return false;
    }
    vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
        vector<vector<Node*>> nodeList(board.size(), vector<Node*>(board[0].size(), nullptr));
        // O(n^2)
        for(int i = 0; i<board.size(); i++)
        {
            for(int j = 0; j<board[0].size(); j++)
            {
                CreateNode(i,j,board, nodeList);
            }
        }

        for(int i = 0; i<words.size(); i++)
        {
            if(wordTree.contains(words[i][0]) == false)
            {
                wordTree[words[i][0]] = new wordNode;
                wordTree[words[i][0]]->val = words[i][0];
            }
            CreateTree(wordTree[words[i][0]],words[i],1);
        }
        for(const auto& W : wordTree)
        {
            for(Node* n : um[W.first])
            {
                unordered_set<Node*> s;
                s.insert(n);
                string newAns = string() +W.first;
                if(DFS(n, W.second,s, newAns))
                {
                    break;
                }
            }
        }
        vector<string> ret;
        for(auto& s : ans)
        {
            ret.push_back(s);
        }
        return ret;
    }
};
123471