1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#include<bits/stdc++.h> using namespace std;
template<typename T> void show(vector<T> v) { for (auto x : v) cout << x << " "; cout << endl; }
constexpr int MAX_INT = 1e9; const array<char, 5> colors = {'R', 'Y', 'B', 'G', 'W'};
class Solution { public: void eliminate (string & s) { for (int i = 0, j; i < s.length(); i = j) { for (j = i+1; j < s.length() && s[j] == s[i]; ++j); if (j - i >= 3) { s.erase(i, j - i); eliminate(s); } } }
int dfs(const string && board, const int hand, map<pair<string, int>, int> & memo) { if (memo.find({board, hand}) != memo.end()) { return memo[{board, hand}]; } if (board.empty()) return 0;
int ret = MAX_INT; for (int i = 0, c = 1; i < 5; ++i, c *= 10) if (hand / c % 10 > 0) { if (board[0] == colors[i]) { string tmp = board; tmp.insert(0, 1, colors[i]); eliminate(tmp); int r = dfs(move(tmp), hand - c, memo); if (r != -1) ret = min(ret, r + 1); } int n = board.length(); if (board[n-1] == colors[i]) { string tmp = board; tmp.insert(n, 1, colors[i]); eliminate(tmp); int r = dfs(move(tmp), hand - c, memo); if (r != -1) ret = min(ret, r + 1); } for (int j = 1; j < board.size(); j++) { if (board[j-1] == board[j]) { string tmp = board; tmp.insert(j, 1, colors[i]); eliminate(tmp); int r = dfs(move(tmp), hand - c, memo); if (r != -1) ret = min(ret, r + 1); } else if (board[j-1] == colors[i] || board[j] == colors[i]) { string tmp = board; tmp.insert(j, 1, colors[i]); eliminate(tmp); int r = dfs(move(tmp), hand - c, memo); if (r != -1) ret = min(ret, r + 1); } } } memo[{board, hand}] = ret < MAX_INT ? ret : -1; return ret < MAX_INT ? ret : -1; }
int findMinStep_DFS(string board, string hand) { map<pair<string, int>, int> memo; int _hand = 0; for (char ch : hand) { for (int i = 0, c = 1; i < 5; ++i, c *= 10) { if (colors[i] == ch) _hand += c; } } return dfs(move(board), _hand, memo); }
int findMinStep(string board, string hand) { int _hand = 0; for (char ch : hand) { for (int i = 0, c = 1; i < 5; ++i, c *= 10) { if (colors[i] == ch) _hand += c; } } auto bfs = [this](string sb, int sh) { queue<tuple<string, int, int>> que; set<pair<string, int>> vis;
que.push({sb, sh, 0}); vis.insert({sb, sh}); while (!que.empty()) { auto [b, h, d] = que.front(); que.pop(); if (b.empty()) return d; for (int i = 0, c = 1; i < 5; ++i, c *= 10) { if (h / c % 10 == 0) continue; int n = b.length(); if (b[0] == colors[i]) { string tmp = b; tmp.insert(0, 1, colors[i]); eliminate(tmp); int _h = h - c; if (vis.find({tmp, _h}) == vis.end()) { vis.insert({tmp, _h}); que.push({move(tmp), move(_h), d+1}); } } if (b[n-1] == colors[i]) { string tmp = b; tmp.insert(n, 1, colors[i]); eliminate(tmp); int _h = h - c; if (vis.find({tmp, _h}) == vis.end()) { vis.insert({tmp, _h}); que.push({move(tmp), move(_h), d+1}); } } for (int j = 1; j < n; ++j) { if (b[j-1] == b[j]) { string tmp = b; tmp.insert(j, 1, colors[i]); eliminate(tmp); int _h = h - c; if (vis.find({tmp, _h}) == vis.end()) { vis.insert({tmp, _h}); que.push({move(tmp), move(_h), d+1}); } } else if (b[j-1] == colors[i] || b[j] == colors[i]) { string tmp = b; tmp.insert(j, 1, colors[i]); eliminate(tmp); int _h = h - c; if (vis.find({tmp, _h}) == vis.end()) { vis.insert({tmp, _h}); que.push({move(tmp), move(_h), d+1}); } } } } } return -1; }; return bfs(board, _hand); } };
|