#include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_set> #include <algorithm> #include <vector> #include <functional> #include <deque> using namespace std; struct State { int32_t iContains0; int32_t iContains1; int32_t iLevel; }; static uint32_t iMoveID; static int32_t iUnknownSizes, iTotalContent; static int32_t aSizes[3]; static int32_t aContains[3]; static deque<State> dqStates; static uint32_t aAnswers[100002]; static uint32_t aMoveIDs[3][100002] = { 0, }; struct FullState { int32_t aContains[3]; FullState() {} FullState(const State & st) { aContains[0] = st.iContains0; aContains[1] = st.iContains1; aContains[2] = iTotalContent - st.iContains0 - st.iContains1; } }; class Assistant { unordered_set<uint64_t> us; static uint64_t GetKey(int32_t iContains0, int32_t iContains1) { return (iContains0 << 17) | iContains1; } public: void MarkVisited(int32_t iContains0, int32_t iContains1) { if (us.size() > 40000000) { return; } uint64_t iKey = GetKey(iContains0, iContains1); us.insert(iKey); } bool HasBeenVisited(int32_t iContains0, int32_t iContains1) { uint64_t iKey = GetKey(iContains0, iContains1); return us.find(iKey) != us.end(); } }; static Assistant oAsistant; static void ReadData() { cin >> aSizes[0] >> aSizes[1] >> aSizes[2]; cin >> aContains[0] >> aContains[1] >> aContains[2]; } static void MakeFirstMove() { iTotalContent = 0; for (int i = 0; i < 3; ++i) { iTotalContent += aContains[i]; aAnswers[aContains[i]] = 0; aMoveIDs[i][aContains[i]] = 1; iUnknownSizes--; } } static void Init() { for (int i = 0; i <= aSizes[2]; ++i) { aAnswers[i] = (uint32_t) -1; } MakeFirstMove(); iUnknownSizes = min(iTotalContent, aSizes[2]) + 1; iMoveID = 2; } static inline bool MakeMove(State & stNew, const FullState & stOld, int iSrc, int iDst) { if (stOld.aContains[iSrc] == 0 || stOld.aContains[iDst] == aSizes[iDst]) { return false; } int i2Transfer = min(stOld.aContains[iSrc], aSizes[iDst] - stOld.aContains[iDst]); int32_t iNewSrc = stOld.aContains[iSrc] - i2Transfer; int32_t iNewDst = stOld.aContains[iDst] + i2Transfer; FullState newFullState; newFullState.aContains[iSrc] = iNewSrc; newFullState.aContains[iDst] = iNewDst; newFullState.aContains[3 - iSrc - iDst] = iTotalContent - iNewSrc - iNewDst; if (oAsistant.HasBeenVisited(newFullState.aContains[0], newFullState.aContains[1])) { return false; } for (int i = 0; i < 2; ++i) { if (aMoveIDs[i][newFullState.aContains[i]] == 0) continue; for (int j = i + 1; j < 3; ++j) { if (aMoveIDs[i][newFullState.aContains[i]] == aMoveIDs[j][newFullState.aContains[j]]) { return false; // We have been here ("Nasi tu byli!"). } } } uint32_t iNewMoveID = iMoveID++; for (int i = 0; i < 2; ++i) { if (aMoveIDs[i][newFullState.aContains[i]] != 0) continue; for (int j = i + 1; j < 3; ++j) { if (aMoveIDs[j][newFullState.aContains[j]] == 0) { aMoveIDs[i][newFullState.aContains[i]] = iNewMoveID; aMoveIDs[j][newFullState.aContains[j]] = iNewMoveID; goto lblOut; } } } lblOut: if (aAnswers[iNewSrc] == (uint32_t) -1) { aAnswers[iNewSrc] = stNew.iLevel; iUnknownSizes--; } if (aAnswers[iNewDst] == (uint32_t) -1) { aAnswers[iNewDst] = stNew.iLevel; iUnknownSizes--; } stNew.iContains0 = newFullState.aContains[0]; stNew.iContains1 = newFullState.aContains[1]; oAsistant.MarkVisited(newFullState.aContains[0], newFullState.aContains[1]); return true; } static void Solve() { State st, stNew; st.iContains0 = aContains[0]; st.iContains1 = aContains[1]; st.iLevel = 0; dqStates.push_back(st); while (!dqStates.empty() && iUnknownSizes > 0) { if (dqStates.size() > 5000000) break; st = dqStates.front(); dqStates.pop_front(); FullState fst(st); stNew.iLevel = st.iLevel + 1; for (int iSrc = 0; iSrc < 3; ++iSrc) { for (int iDst = 0; iDst < 3; ++iDst) { if (iSrc == iDst) continue; if (!MakeMove(stNew, fst, iSrc, iDst)) continue; dqStates.push_back(stNew); } } } } static void Answer() { for (int i = 0; i <= aSizes[2]; ++i) { int32_t iAnswer = (int32_t) aAnswers[i]; cout << iAnswer; if (i != aSizes[2]) { cout << " "; } } } int main(int argc, char * argv[]) { ReadData(); Init(); Solve(); Answer(); return 0; }
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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | #include <cstdio> #include <stdio.h> #include <iostream> #include <string> #include <stdint.h> #include <unordered_set> #include <algorithm> #include <vector> #include <functional> #include <deque> using namespace std; struct State { int32_t iContains0; int32_t iContains1; int32_t iLevel; }; static uint32_t iMoveID; static int32_t iUnknownSizes, iTotalContent; static int32_t aSizes[3]; static int32_t aContains[3]; static deque<State> dqStates; static uint32_t aAnswers[100002]; static uint32_t aMoveIDs[3][100002] = { 0, }; struct FullState { int32_t aContains[3]; FullState() {} FullState(const State & st) { aContains[0] = st.iContains0; aContains[1] = st.iContains1; aContains[2] = iTotalContent - st.iContains0 - st.iContains1; } }; class Assistant { unordered_set<uint64_t> us; static uint64_t GetKey(int32_t iContains0, int32_t iContains1) { return (iContains0 << 17) | iContains1; } public: void MarkVisited(int32_t iContains0, int32_t iContains1) { if (us.size() > 40000000) { return; } uint64_t iKey = GetKey(iContains0, iContains1); us.insert(iKey); } bool HasBeenVisited(int32_t iContains0, int32_t iContains1) { uint64_t iKey = GetKey(iContains0, iContains1); return us.find(iKey) != us.end(); } }; static Assistant oAsistant; static void ReadData() { cin >> aSizes[0] >> aSizes[1] >> aSizes[2]; cin >> aContains[0] >> aContains[1] >> aContains[2]; } static void MakeFirstMove() { iTotalContent = 0; for (int i = 0; i < 3; ++i) { iTotalContent += aContains[i]; aAnswers[aContains[i]] = 0; aMoveIDs[i][aContains[i]] = 1; iUnknownSizes--; } } static void Init() { for (int i = 0; i <= aSizes[2]; ++i) { aAnswers[i] = (uint32_t) -1; } MakeFirstMove(); iUnknownSizes = min(iTotalContent, aSizes[2]) + 1; iMoveID = 2; } static inline bool MakeMove(State & stNew, const FullState & stOld, int iSrc, int iDst) { if (stOld.aContains[iSrc] == 0 || stOld.aContains[iDst] == aSizes[iDst]) { return false; } int i2Transfer = min(stOld.aContains[iSrc], aSizes[iDst] - stOld.aContains[iDst]); int32_t iNewSrc = stOld.aContains[iSrc] - i2Transfer; int32_t iNewDst = stOld.aContains[iDst] + i2Transfer; FullState newFullState; newFullState.aContains[iSrc] = iNewSrc; newFullState.aContains[iDst] = iNewDst; newFullState.aContains[3 - iSrc - iDst] = iTotalContent - iNewSrc - iNewDst; if (oAsistant.HasBeenVisited(newFullState.aContains[0], newFullState.aContains[1])) { return false; } for (int i = 0; i < 2; ++i) { if (aMoveIDs[i][newFullState.aContains[i]] == 0) continue; for (int j = i + 1; j < 3; ++j) { if (aMoveIDs[i][newFullState.aContains[i]] == aMoveIDs[j][newFullState.aContains[j]]) { return false; // We have been here ("Nasi tu byli!"). } } } uint32_t iNewMoveID = iMoveID++; for (int i = 0; i < 2; ++i) { if (aMoveIDs[i][newFullState.aContains[i]] != 0) continue; for (int j = i + 1; j < 3; ++j) { if (aMoveIDs[j][newFullState.aContains[j]] == 0) { aMoveIDs[i][newFullState.aContains[i]] = iNewMoveID; aMoveIDs[j][newFullState.aContains[j]] = iNewMoveID; goto lblOut; } } } lblOut: if (aAnswers[iNewSrc] == (uint32_t) -1) { aAnswers[iNewSrc] = stNew.iLevel; iUnknownSizes--; } if (aAnswers[iNewDst] == (uint32_t) -1) { aAnswers[iNewDst] = stNew.iLevel; iUnknownSizes--; } stNew.iContains0 = newFullState.aContains[0]; stNew.iContains1 = newFullState.aContains[1]; oAsistant.MarkVisited(newFullState.aContains[0], newFullState.aContains[1]); return true; } static void Solve() { State st, stNew; st.iContains0 = aContains[0]; st.iContains1 = aContains[1]; st.iLevel = 0; dqStates.push_back(st); while (!dqStates.empty() && iUnknownSizes > 0) { if (dqStates.size() > 5000000) break; st = dqStates.front(); dqStates.pop_front(); FullState fst(st); stNew.iLevel = st.iLevel + 1; for (int iSrc = 0; iSrc < 3; ++iSrc) { for (int iDst = 0; iDst < 3; ++iDst) { if (iSrc == iDst) continue; if (!MakeMove(stNew, fst, iSrc, iDst)) continue; dqStates.push_back(stNew); } } } } static void Answer() { for (int i = 0; i <= aSizes[2]; ++i) { int32_t iAnswer = (int32_t) aAnswers[i]; cout << iAnswer; if (i != aSizes[2]) { cout << " "; } } } int main(int argc, char * argv[]) { ReadData(); Init(); Solve(); Answer(); return 0; } |