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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| #include <bits/stdc++.h> using namespace std; const int iol = 1024 * 1024; char buf[iol], *s, *t, ioc; bool iosig; inline char read() { if(s == t) { t = (s = buf) + fread(buf, 1, iol, stdin); if(s == t) return -1; } return *s++; } template<class T> inline bool read(T& x) { iosig = false; for(ioc = read(); !isdigit(ioc); ioc = read()) { if(ioc == -1) return false; if(ioc == '-') iosig = true; } x = 0; while(ioc == '0') ioc = read(); for(; isdigit(ioc); ioc = read()) x = (x << 1) + (x << 3) + (ioc ^ '0'); s--; if(iosig) x = -x; return true; } #define MAXN 13
typedef unsigned long long Status;
struct HashMap { const static int HASH_SIZE = 7979717; struct Node { Status key; int val, time; } N[HASH_SIZE]; int time; HashMap() : time(1) {} inline int locate(const Status &key) { register int i; for (i = key % HASH_SIZE; N[i].time == time && N[i].key != key; (i < HASH_SIZE - 1) ? (i++) : (i = 0)); if (N[i].time != time) { N[i].time = time; N[i].key = key; N[i].val = INT_MAX; } return i; } inline int &operator[](const Status &key) { return N[locate(key)].val; } inline void clear() { time++; } } h; inline int getid(const int x) { if (x == 1) return 13; return (x >= 2 && x <= 13) ? (x - 1) : x; } inline int getCard(const Status &s, const int index) { return (s >> (index << 2)) & 15ll; }
inline void setCard(Status &s, const int index, const int val) {
s &= ~(15ll << (index << 2)); s |= (val & 15ll) << (index << 2);
} inline void changeCard(Status &s, const int index, const int delta) { setCard(s, index, getCard(s, index) + delta); } int n, ans; inline void dfs(const Status &s, const int step = 0) {
if (step >= ans) return; int &x = h[s]; if (step >= x) return; x = step; if (!s) { ans = step; return; } for (register int i = 2; i <= 12; i++) { Status next = s; register int x = getCard(next, i); if (x < 3) { continue; } else setCard(next, i, x - 3); for (register int j = i + 1; j <= 13; j++) { int x = getCard(next, j); if (x < 3) { break; } else { setCard(next, j, x - 3); dfs(next, step + 1); } } } for (register int i = 2; i <= 11; i++) { bool valid = true; Status next = s; for (register int j = i; j < i + 2; j++) { register int x = getCard(next, j); if (x < 2) { valid = false; break; } else setCard(next, j, x - 2); } if (!valid) continue; for (register int j = i + 2; j <= 13; j++) { register int x = getCard(next, j); if (x < 2) { break; } else { setCard(next, j, x - 2); dfs(next, step + 1); } } } for (register int i = 2; i <= 9; i++) { bool valid = true; Status next = s; for (register int j = i; j < i + 4; j++) { register int x = getCard(next, j); if (x < 1) { valid = false; break; } else setCard(next, j, x - 1); } if (!valid) continue; for (register int j = i + 4; j <= 13; j++) { register int x = getCard(next, j); if (x < 1) { break; } else { setCard(next, j, x - 1); dfs(next, step + 1); } } } vector<int> four, three, two, one; for (register int i = 0; i <= 13; i++) { register int x = getCard(s, i); if (x == 4) four.push_back(i); else if (x == 3) three.push_back(i); else if (x == 2) two.push_back(i); else if (x == 1) one.push_back(i); } for (vector<int>::const_iterator it = four.begin(); it != four.end(); it++) { Status tmp = s; setCard(tmp, *it, 0); for (vector<int>::const_iterator a = two.begin(); a != two.end(); a++) { if (*a == 0) continue; for (vector<int>::const_iterator b = a + 1; b != two.end(); b++) { if (*b == 0) continue; Status next = tmp; setCard(next, *a, 0); setCard(next, *b, 0); dfs(next, step + 1); } for (vector<int>::const_iterator b = three.begin(); b != three.end(); b++) { Status next = tmp; changeCard(next, *a, -2); changeCard(next, *b, -2); dfs(next, step + 1); } Status next = tmp; setCard(next, *a, 0); dfs(next, step + 1); } for (vector<int>::const_iterator a = four.begin(); a != four.end(); a++) { if (*a == *it) continue; Status next = tmp; setCard(next, *a, 0); dfs(next, step + 1); } for (vector<int>::const_iterator a = one.begin(); a != one.end(); a++) { for (vector<int>::const_iterator b = a + 1; b != one.end(); b++) { Status next = tmp; setCard(next, *a, 0); setCard(next, *b, 0); dfs(next, step + 1); } for (vector<int>::const_iterator b = three.begin(); b != three.end(); b++) { Status next = tmp; changeCard(next, *a, -1); changeCard(next, *b, -1); dfs(next, step + 1); } } dfs(tmp, step + 1); } for (vector<int>::const_iterator it = three.begin(); it != three.end(); it++) { Status tmp = s; setCard(tmp, *it, 0); for (vector<int>::const_iterator a = three.begin(); a != three.end(); a++) { if (*a == *it) continue; Status next = tmp; changeCard(next, *a, -1); dfs(next, step + 1); changeCard(next, *a, -1); dfs(next, step + 1); } for (vector<int>::const_iterator a = two.begin(); a != two.end(); a++) { if (*a == 0) { Status next = tmp; setCard(next, *a, 1); dfs(next, step + 1); continue; } Status next = tmp; setCard(next, *a, 0); dfs(next, step + 1); } for (vector<int>::const_iterator a = one.begin(); a != one.end(); a++) { Status next = tmp; setCard(next, *a, 0); dfs(next, step + 1); } dfs(tmp, step + 1); } for (vector<int>::const_iterator it = two.begin(); it != two.end(); it++) { Status next = s; setCard(next, *it, 0); dfs(next, step + 1); } for (vector<int>::const_iterator it = one.begin(); it != one.end(); it++) { Status next = s; setCard(next, *it, 0); dfs(next, step + 1); } } int main() { register int t; read(t), read(n); while (t--) { Status init = 0; for (register int i = 0; i < n; i++) { register int a, b; read(a), read(b); changeCard(init, getid(a), 1); } h.clear(); ans = INT_MAX; dfs(init); cout << ans << "\n"; } return 0; }
|