756. 金字塔转换矩阵
1 class Solution 2 { 3 unordered_map<string,vector<char>> hash; 4 public: 5 bool pyramidTransition(string bottom, vector<string>& allowed) 6 { 7 for(auto a : allowed) hash[a.substr(0,2)].push_back(a[2]); 8 return DFS(bottom,"",0); 9 } 10 11 bool DFS(string& bottom,string upper,int site) 12 { 13 if(bottom.size() == 1) return true; 14 if(upper.size() + 1 == bottom.size()) return DFS(upper,"",0); 15 for(auto it: hash[bottom.substr(site,2)]) 16 { 17 upper.push_back(it); 18 if(DFS(bottom,upper,site+1)) return true; 19 upper.pop_back(); 20 } 21 return false; 22 } 23 };
Mamba never out

浙公网安备 33010602011771号