//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (22)+7; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int plecak[maxn][maxn]; int farmer[maxn][maxn]; int czolg[maxn][maxn]; int tab[maxn][maxn]; bool used[maxn][maxn]; int n; int gold; int goldsum; int k; int tury; int farmerzy; bool czy[maxn]; int pos[maxn]; VPII mov = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; void move(int a, int b, int c, int d) { assert(abs(a - c) + abs(b - d) == 1); assert(farmer[a][b] == 1 || czolg[a][b] == 1); assert(farmer[c][d] == 0 && czolg[c][d] == 0); assert(used[a][b] == 0); assert(tab[c][d] >= 0); assert(b < n && a < n && c < n && d < n); assert(a >= 0 && b >= 0 && c >= 0 && d >= 0); cout<<"M "<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"; farmer[c][d] = farmer[a][b]; czolg[c][d] = czolg[a][b]; czolg[a][b] = farmer[a][b] = 0; used[c][d] = 1; used[a][b] = 0; plecak[c][d] = plecak[a][b]; plecak[a][b] = 0; } void buyfarmer() { assert(gold >= 100); assert(farmer[0][0] == 0 && czolg[0][0] == 0); farmerzy++; plecak[0][0] = 0; farmer[0][0] = 1; used[0][0] = 0; gold -= 100; goldsum -= 100; cout<<"R FARMER\n"; } void buytank() { assert(gold >= 100); assert(farmer[0][0] == 0 && czolg[0][0] == 0); plecak[0][0] = 0; czolg[0][0] = 1; used[0][0] = 0; gold -= 100; goldsum -= 100; cout<<"R TANK\n"; } bool endturn() { bool f = 0; for(int i = 0 ;i < n;i++) for(int j = 0;j < n;j++) { if(farmer[i][j]) { int val = min(10, tab[i][j]); tab[i][j] -= val; plecak[i][j] += val; goldsum += val; } if(czolg[i][j]) { int val = min(10, -tab[i][j]); tab[i][j] += val; } if((plecak[i][j] > 0 && (i > 0 || j > 0)) || tab[i][j] > 0) f = 1; } gold += plecak[0][0]; plecak[0][0] = 0; for(int i = 0 ;i < n;i++) for(int j = 0;j < n;j++) used[i][j] = 0; cout<<"=\n"; tury++; return f; } bool pole(int x, int y) { return (0 <= x && x < n) && (0 <= y && y < n); } bool wolne(int x, int y) { return pole(x, y) && czolg[x][y] == 0 && farmer[x][y] == 0; } void solve() { cin>>n; for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) { cin>>tab[i][j]; plecak[i][j] = 0; farmer[i][j] = 0; czolg[i][j] = 0; used[i][j] = 0; } gold = 200; goldsum = 200; tury = 0; farmerzy = 0; buyfarmer(); move(0, 0, 0, 1); buyfarmer(); move(0, 0, 1, 0); PII a = {0, 1}; PII b = {1, 0}; VPII A, B; for(int i = 0;i < n;i++) { A.pb({0, i}); B.pb({i, 0}); } for(int i = n - 1;i > 0;i--) { A.pb({1, i}); B.pb({i, 1}); } A.pb(A[1]); B.pb(B[1]); int wska = 1; int wskb = 1; while(goldsum < 1800) { if(tab[a.fi][a.se] == 0) { move(a.fi, a.se, A[wska + 1].fi, A[wska + 1].se); a = A[wska + 1]; wska++; } if(tab[b.fi][b.se] == 0) { move(b.fi, b.se, B[wskb + 1].fi, B[wskb + 1].se); b = B[wskb + 1]; wskb++; } endturn(); } while(a.fi > 0 || b.se > 0) { if(a.fi > 0) { move(a.fi, a.se, a.fi - 1, a.se); a.fi--; } if(b.se > 0) { move(b.fi, b.se, b.fi, b.se - 1); b.se--; } endturn(); } while(farmer[0][0] == 0) { if(a.se > 0) { if(wolne(a.fi, a.se - 1)) { move(a.fi, a.se, a.fi, a.se - 1); a.se--; } } if(b.fi > 0) { if(wolne(b.fi - 1, b.se)) { move(b.fi, b.se, b.fi - 1, b.se); b.fi--; } } endturn(); } if(a.fi == 0 && a.se == 0) { move(0, 0, 0, 1); while(b.fi != 0) { move(b.fi, b.se, b.fi - 1, b.se); b.fi--; endturn(); } } else { move(0, 0, 1, 0); while(a.se != 0) { move(a.fi, a.se, a.fi, a.se - 1); a.se--; endturn(); } } if(farmer[1][0]) { move(1, 0, 1, 1); endturn(); move(1, 1, 0, 1); endturn(); } do { for(int i = n - 2;i >= 0;i--) if(farmer[0][i]) move(0, i, 0, i + 1); buyfarmer(); endturn(); } while(farmer[0][n - 1] == 0); for(int i = 0 ;i < n;i++) { pos[i] = 0; czy[i] = 0; } int ile = 0; do { for(int i = 0 ;i < n;i++) if(czy[i]) { if(pos[i] > 0) { move(pos[i], i, pos[i] - 1, i); pos[i]--; if(pos[i] == 0) ile++; } } else { if(tab[pos[i]][i] == 0) { if(pos[i] == n - 1) { ile++; czy[i] = 1; move(pos[i], i, pos[i] - 1, i); pos[i]--; } else { move(pos[i], i, pos[i] + 1, i); pos[i]++; } } } } while(endturn() && ile < 2 * n); bool f = 1; for(int i = 0;i < n - 1;i++) { for(int j = i;j >= 0;j--) move(j, 0, j + 1, 0); for(int j = 1;j < n - i;j++) move(0, j, 0, j - 1); f = endturn(); } assert(f == 0); cout<<"===\n"; // for(int i = 0 ;i < n;i++) // { // for(int j = 0 ;j < n;j++) // cerr<<tab[i][j]<<" "; // cerr<<endl; // } // for(int i = 0 ;i < n;i++) // { // for(int j = 0 ;j < n;j++) // cerr<<farmer[i][j]<<" "; // cerr<<endl; // } dbg(tury, goldsum, gold); } int main() { _upgrade int t; cin>>t>>k; while(t--) solve(); 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 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") //~ #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include<bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define pb push_back #define SZ(x) ((int)(x).size()) #define ALL(x) x.begin(),x.end() #define all(x) x.begin(),x.end() #define fi first #define se second #define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define erase_duplicates(x) sort(all(x)); (x).resize(distance((x).begin(), unique(all(x)))); using namespace std; using namespace __gnu_pbds; template<typename T> using ordered_set = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //X.find_by_order(k); - zwraca iterator na k-ty element (numeracja od zerowego) //X.order_of_key(k); - zwraca liczbę elementów ostro mniejszych niż k typedef long long LL; typedef pair<int,int> PII; typedef pair<LL,LL> PLL; typedef vector<PII> VPII; typedef vector<PLL> VPLL; typedef vector<LL> VLL; typedef vector<int> VI; typedef vector<string> VS; typedef vector<char> VC; typedef long double LD; typedef pair<LD,LD> PLD; typedef vector<LD> VLD; typedef vector<PLD> VPLD; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while(*sdbg!=',')cerr<<*sdbg++; cerr<<" = "<<h<<", "; _dbg(sdbg+1, a...); } #ifdef LOCAL #define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__) #else #define dbg(...) #define cerr if(0)cout #endif const int maxn = (22)+7; const int maxk = 20; const int inf = (1e9)+7; const LL LLinf = ((LL)1e18)+7LL; const LD eps = 1e-9; const LL mod = 1e9+7; // ***************************** CODE ***************************** // int plecak[maxn][maxn]; int farmer[maxn][maxn]; int czolg[maxn][maxn]; int tab[maxn][maxn]; bool used[maxn][maxn]; int n; int gold; int goldsum; int k; int tury; int farmerzy; bool czy[maxn]; int pos[maxn]; VPII mov = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}}; void move(int a, int b, int c, int d) { assert(abs(a - c) + abs(b - d) == 1); assert(farmer[a][b] == 1 || czolg[a][b] == 1); assert(farmer[c][d] == 0 && czolg[c][d] == 0); assert(used[a][b] == 0); assert(tab[c][d] >= 0); assert(b < n && a < n && c < n && d < n); assert(a >= 0 && b >= 0 && c >= 0 && d >= 0); cout<<"M "<<a<<" "<<b<<" "<<c<<" "<<d<<"\n"; farmer[c][d] = farmer[a][b]; czolg[c][d] = czolg[a][b]; czolg[a][b] = farmer[a][b] = 0; used[c][d] = 1; used[a][b] = 0; plecak[c][d] = plecak[a][b]; plecak[a][b] = 0; } void buyfarmer() { assert(gold >= 100); assert(farmer[0][0] == 0 && czolg[0][0] == 0); farmerzy++; plecak[0][0] = 0; farmer[0][0] = 1; used[0][0] = 0; gold -= 100; goldsum -= 100; cout<<"R FARMER\n"; } void buytank() { assert(gold >= 100); assert(farmer[0][0] == 0 && czolg[0][0] == 0); plecak[0][0] = 0; czolg[0][0] = 1; used[0][0] = 0; gold -= 100; goldsum -= 100; cout<<"R TANK\n"; } bool endturn() { bool f = 0; for(int i = 0 ;i < n;i++) for(int j = 0;j < n;j++) { if(farmer[i][j]) { int val = min(10, tab[i][j]); tab[i][j] -= val; plecak[i][j] += val; goldsum += val; } if(czolg[i][j]) { int val = min(10, -tab[i][j]); tab[i][j] += val; } if((plecak[i][j] > 0 && (i > 0 || j > 0)) || tab[i][j] > 0) f = 1; } gold += plecak[0][0]; plecak[0][0] = 0; for(int i = 0 ;i < n;i++) for(int j = 0;j < n;j++) used[i][j] = 0; cout<<"=\n"; tury++; return f; } bool pole(int x, int y) { return (0 <= x && x < n) && (0 <= y && y < n); } bool wolne(int x, int y) { return pole(x, y) && czolg[x][y] == 0 && farmer[x][y] == 0; } void solve() { cin>>n; for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) { cin>>tab[i][j]; plecak[i][j] = 0; farmer[i][j] = 0; czolg[i][j] = 0; used[i][j] = 0; } gold = 200; goldsum = 200; tury = 0; farmerzy = 0; buyfarmer(); move(0, 0, 0, 1); buyfarmer(); move(0, 0, 1, 0); PII a = {0, 1}; PII b = {1, 0}; VPII A, B; for(int i = 0;i < n;i++) { A.pb({0, i}); B.pb({i, 0}); } for(int i = n - 1;i > 0;i--) { A.pb({1, i}); B.pb({i, 1}); } A.pb(A[1]); B.pb(B[1]); int wska = 1; int wskb = 1; while(goldsum < 1800) { if(tab[a.fi][a.se] == 0) { move(a.fi, a.se, A[wska + 1].fi, A[wska + 1].se); a = A[wska + 1]; wska++; } if(tab[b.fi][b.se] == 0) { move(b.fi, b.se, B[wskb + 1].fi, B[wskb + 1].se); b = B[wskb + 1]; wskb++; } endturn(); } while(a.fi > 0 || b.se > 0) { if(a.fi > 0) { move(a.fi, a.se, a.fi - 1, a.se); a.fi--; } if(b.se > 0) { move(b.fi, b.se, b.fi, b.se - 1); b.se--; } endturn(); } while(farmer[0][0] == 0) { if(a.se > 0) { if(wolne(a.fi, a.se - 1)) { move(a.fi, a.se, a.fi, a.se - 1); a.se--; } } if(b.fi > 0) { if(wolne(b.fi - 1, b.se)) { move(b.fi, b.se, b.fi - 1, b.se); b.fi--; } } endturn(); } if(a.fi == 0 && a.se == 0) { move(0, 0, 0, 1); while(b.fi != 0) { move(b.fi, b.se, b.fi - 1, b.se); b.fi--; endturn(); } } else { move(0, 0, 1, 0); while(a.se != 0) { move(a.fi, a.se, a.fi, a.se - 1); a.se--; endturn(); } } if(farmer[1][0]) { move(1, 0, 1, 1); endturn(); move(1, 1, 0, 1); endturn(); } do { for(int i = n - 2;i >= 0;i--) if(farmer[0][i]) move(0, i, 0, i + 1); buyfarmer(); endturn(); } while(farmer[0][n - 1] == 0); for(int i = 0 ;i < n;i++) { pos[i] = 0; czy[i] = 0; } int ile = 0; do { for(int i = 0 ;i < n;i++) if(czy[i]) { if(pos[i] > 0) { move(pos[i], i, pos[i] - 1, i); pos[i]--; if(pos[i] == 0) ile++; } } else { if(tab[pos[i]][i] == 0) { if(pos[i] == n - 1) { ile++; czy[i] = 1; move(pos[i], i, pos[i] - 1, i); pos[i]--; } else { move(pos[i], i, pos[i] + 1, i); pos[i]++; } } } } while(endturn() && ile < 2 * n); bool f = 1; for(int i = 0;i < n - 1;i++) { for(int j = i;j >= 0;j--) move(j, 0, j + 1, 0); for(int j = 1;j < n - i;j++) move(0, j, 0, j - 1); f = endturn(); } assert(f == 0); cout<<"===\n"; // for(int i = 0 ;i < n;i++) // { // for(int j = 0 ;j < n;j++) // cerr<<tab[i][j]<<" "; // cerr<<endl; // } // for(int i = 0 ;i < n;i++) // { // for(int j = 0 ;j < n;j++) // cerr<<farmer[i][j]<<" "; // cerr<<endl; // } dbg(tury, goldsum, gold); } int main() { _upgrade int t; cin>>t>>k; while(t--) solve(); return 0; } |