[Depth-first search] leetcode 756 Pyramid Transition Matrix

problem:

       A simulation title, with a dark match what can be found in all cases.

class Solution {
public:
    vector<vector<unordered_set<char>>> pair;
    
    bool dfs(string cur, string next, int i)
    {
        if(cur.empty()) return true; // 搜到顶层了,返回true
        for(int j = i;j < cur.size();j++)
        {
            unordered_set<char>& candidate = pair[cur[j - 1] - 'A'][cur[j] - 'A'];
             IF (candidate.size () == 0 ) return  to false ;
             IF (candidate.size () == . 1 ) // only one choice, directly elected 
            { 
                next.push_back ( * candidate.begin ()) ; 
            } 
            the else 
            { 
                for (Auto & CH: Candidate) // there are many options on each try 
                { 
                    IF (DFS (CUR, Next CH +, J + . 1 )) 
                    { 
                        return  to true ; 
                    } 
                } 
                return  to false ;
            }
        }
        return dfs(next, "", 1); // 搜索下一层
    }
    bool pyramidTransition(string bottom, vector<string>& allowed) {
        pair.resize(8, vector<unordered_set<char>>(8));
        for(int i = 0;i < allowed.size(); i++)
        {
           pair[allowed[i][0] - 'A'][allowed[i][1] - 'A'].insert(allowed[i][2]);
        }
        
        return dfs(bottom, "", 1);
    }
};

 

Guess you like

Origin www.cnblogs.com/fish1996/p/11294835.html