#include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 22; const int T = 5555; const string farm = "R FARMER"; int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {1, 0, -1, 0, 0}; string pattern[20] = { "0bbbkkkjjiihhggffedc", "aabbllkkjjiihhggfedc", "aabbmllkkjjiihhgfedc", "aammmmllkkjjiihgfedc", "klmmmmmllkkjjihgfedc", "kllmmmmmllkkjihgfedc", "kkllmmmmmllkjihgfedc", "jkkllmmmmllkjihgfedc", "jjkkllmmmllkjihgfedc", "ijjkkllllllkjihgfedc", "iijjkklllllkjihgfedc", "hiijjkkkkkkkjihgfedc", "hhiijjjjjjjjjihgfedc", "ghhiiiiiiiiiiihgfedc", "gghhhhhhhhhhhhhgfedc", "fgggggggggggggggfedc", "fffffffffffffffffedc", "eeeeeeeeeeeeeeeeeedc", "dddddddddddddddddddc", "cccccccccccccccccccc" }; /* 0 1 2 3 4 5 6 7 8 9 1 2 4 8 16 32 64 128 256 512 */ int k, n; vector<string> cmds; int field[N][N], inp[N][N]; int have; // gold vi pathx[N], pathy[N], pathg[N]; int siz[N]; vi ttpathx[N], ttpathy[N], ttpathg[N]; int ttsiz[N]; int pos[N]; int cx[N], cy[N], gold[N]; int tot_f, lo_f; int positions[N][N]; void submit_move(int id, int x, int y) { cmds.pb("M " + to_string(cx[id]) + " " + to_string(cy[id]) + " " + to_string(x) + " " + to_string(y)); positions[x][y] = id; positions[cx[id]][cy[id]] = -1; cx[id] = x; cy[id] = y; } void choose_move(int id) { if (pathg[id][pos[id]] == 1) { if (field[cx[id]][cy[id]] > 0) return; // nie zebral jeszcze } if (pathg[id][pos[id]] == 2 && id>1) { // skonczyl gre if (cx[id]<n && positions[cx[id]+1][cy[id]] == -1) { // odsun sie submit_move(id, cx[id]+1, cy[id]); } return; } if (cx[id]==0 && cy[id]==0 && pos[id]==0) { pos[id] = -1; } int nx = pathx[id][pos[id]+1], ny = pathy[id][pos[id]+1]; // kolejny krok if (positions[nx][ny] != -1) return; // zajete if (id==1 && nx==0 && ny==0 && lo_f==0) return; // pierwszy jeszcze nie wrocil, poczekaj submit_move(id, nx, ny); pos[id]++; } int turnn; bool endturn() { turnn++; cmds.pb("="); for (int i=lo_f; i<tot_f; i++) { int gather = min(field[cx[i]][cy[i]], 10); // zbierz gold[i] += gather; field[cx[i]][cy[i]] -= gather; if (cx[i] == 0 && cy[i] == 0) { // oddaj if (gold[i]>0) { //fprintf(stderr, "%d: tura %d\n", i, turnn); } have += gold[i]; gold[i] = 0; } } FOR(i,n) FOR(j,n) if (field[i][j] > 0) return false; for (int i=lo_f; i<tot_f; i++) if (gold[i] > 0) return false; return true; } int cmd_iter; void print_cmds() { for (; cmd_iter < SZ(cmds); cmd_iter++) printf("%s\n", cmds[cmd_iter].c_str()); } void test() { turnn=0; //fprintf(stderr, "test\n"); fflush(stderr); FOR(j,13) { pathx[j] = ttpathx[j]; pathy[j] = ttpathy[j]; pathg[j] = ttpathg[j]; siz[j] = ttsiz[j]; } //n=20; scanf("%d", &n); //FOR(i,n) FOR(j,n) inp[i][j] = 1<<(rand()%10); //inp[0][0]=0; FOR(i,n) FOR(j,n) scanf("%d", &inp[i][j]); FOR(i,n) FOR(j,n) field[i][j] = inp[i][j]; int g0=0,g1=0; while (true) { g0=g1=0; FOR(i,siz[0]) if (pathg[0][i] == 1) g0 += inp[pathx[0][i]][pathy[0][i]]; FOR(i,siz[1]) if (pathg[1][i] == 1) g1 += inp[pathx[1][i]][pathy[1][i]]; //fprintf(stderr, "g = %d %d\n", g0, g1); if (g0+g1 >= 900) break; FOR(i,siz[0]) if (pathx[0][i] == pathx[0][i+1] && pathy[0][i]==0) { pathx[0].pb(pathx[0][siz[0]-2]); pathy[0].pb(pathy[0][siz[0]-2]); pathg[0].pb(pathg[0][siz[0]-2]); pathx[0].pb(pathx[0][siz[0]-1]); pathy[0].pb(pathy[0][siz[0]-1]); pathg[0].pb(pathg[0][siz[0]-1]); for (int j = siz[0]-1; j >= i+3; j--) { pathx[0][j] = pathx[0][j-2]; pathy[0][j] = pathy[0][j-2]; pathg[0][j] = pathg[0][j-2]; } pathx[0][i+1] = pathx[0][i+2] = pathx[0][i]+1; pathy[0][i+1] = 0; pathy[0][i+2] = 1; pathg[0][i+1] = pathg[0][i+2] = 1; siz[0]+=2; break; } FOR(i,siz[1]) if (pathx[1][i] == pathx[1][i+1] && pathy[1][i]==3) { pathx[1].pb(pathx[1][siz[1]-2]); pathy[1].pb(pathy[1][siz[1]-2]); pathg[1].pb(pathg[1][siz[1]-2]); pathx[1].pb(pathx[1][siz[1]-1]); pathy[1].pb(pathy[1][siz[1]-1]); pathg[1].pb(pathg[1][siz[1]-1]); for (int j = siz[1]-1; j >= i+3; j--) { pathx[1][j] = pathx[1][j-2]; pathy[1][j] = pathy[1][j-2]; pathg[1][j] = pathg[1][j-2]; } pathx[1][i+1] = pathx[1][i+2] = pathx[1][i]+1; pathy[1][i+1] = 3; pathy[1][i+2] = 2; pathg[1][i+1] = pathg[1][i+2] = 1; siz[1]+=2; break; } } /*if (g0>g1) { swap(pathx[0], pathx[1]); swap(pathy[0], pathy[1]); swap(pathg[0], pathg[1]); swap(siz[0], siz[1]); }*/ /*FOR(j,13) { fprintf(stderr, "%d : ", j); FOR(i,siz[j]) fprintf(stderr, "%d %d %d; ", pathx[j][i], pathy[j][i], pathg[j][i]); fprintf(stderr, "\n"); }*/ FOR(i,13) pos[i] = 0; FOR(i,n) FOR(j,n) positions[i][j] = -1; FOR(i,n) cx[i] = cy[i] = gold[i] = 0; have = 0; cmds.clear(); cmd_iter=0; cmds.pb(farm); choose_move(0); cmds.pb(farm); choose_move(1); endturn(); print_cmds(); tot_f = 2; lo_f = 0; int iter = 0; while (true) { iter++; /*if (iter%100==0) { printf("turn, %d farm, %d gold\n", tot_f, have); for (int i=lo_f; i<tot_f; i++) printf("i = %d xy = %d %d gold = %d goal = %d, pos = %d/%d -> %d\n", i, cx[i], cy[i], gold[i], pathg[i][pos[i]], pos[i], siz[i], positions[pathx[i][pos[i]]+1][pathy[i][pos[i]]+1]); FOR(i,n) { FOR(j,n) printf("%d ", field[i][j]); printf("\n"); } fflush(stdout); } if (iter>1000) exit(0);*/ //if (iter>5000) exit(0); //printf("turn, %d farm, %d gold\n", tot_f, have); //for (int i=lo_f; i<tot_f; i++) printf("i = %d xy = %d %d gold = %d goal = %d, pos = %d/%d\n", i, cx[i], cy[i], gold[i], pathg[i][pos[i]], pos[i], siz[i]);fflush(stdout); if (have >= 100 && tot_f-lo_f < 11 && positions[0][0] == -1) { have -= 100; cmds.pb(farm); choose_move(tot_f); positions[0][0] = tot_f; tot_f++; } for (int i=lo_f; i<tot_f; i++) { if (i==lo_f && i<2 && cx[i]==0 && cy[i]==0) { tot_f++; lo_f++; } else { choose_move(i); } } bool fin = endturn(); if (fin) break; //print_cmds(); } cmds.pb("==="); print_cmds(); //fprintf(stderr, "iters=%d\n", iter); } bool occ[N][N]; void dfs(int x, int y, int pos) { bool fnd = 1; while (fnd) { fnd = 0; occ[x][y] = 1; FOR(i,4) { int xx = x+dx[i], yy = y+dy[i]; if (xx<0 || yy<0 || xx>=20 || yy>=20) continue; if (pattern[xx][yy] != 'a'+pos) continue; if (occ[xx][yy]) continue; x = xx; y = yy; pathx[pos].pb(x); pathy[pos].pb(y); pathg[pos].pb(1); fnd = 1; break; } assert(SZ(pathx[pos]) < 80); } } void init() { pathx[0] = {1, 2, 3, 3, 2, 1, 1, 0}; pathy[0] = {0, 0, 0, 1, 1, 1, 0, 0}; pathg[0] = {1, 1, 1, 1, 1, 1, 0, 2}; pathx[1] = {0, 0, 0, 1, 2, 2, 1, 0, 0, 0}; pathy[1] = {1, 2, 3, 3, 3, 2, 2, 2, 1, 0}; pathg[1] = {1, 1, 1, 1, 1, 1, 1, 0, 0, 2}; for (int j = 2; 'a'+j <= 'k'; j++) { pathx[j] = {1}; pathy[j] = {0}; pathg[j] = {0}; int m = 0; while (pattern[pathx[j][m]][pathy[j][m]] != 'a'+j) { pathx[j].pb(pathx[j][m]+1); pathy[j].pb(0); pathg[j].pb(0); m++; assert(m<90); } pathg[j].back() = 1; dfs(pathx[j][m], pathy[j][m], j); m = SZ(pathx[j])-1; while (pathy[j][m] > 1) { pathx[j].pb(0); pathy[j].pb(pathy[j][m]-1); pathg[j].pb(0); m++; assert(m<90); } pathx[j].pb(0); pathy[j].pb(0); pathg[j].pb(2); } pathx[11] = {1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 9, 9,10,10,10, 9, 9, 8, 8, 7, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0}; pathy[11] = {0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9,10,10, 9, 9,10,10, 9,10,10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 3, 3, 2, 1, 0}; pathg[11] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2}; pathx[12] = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 0, 0}; pathy[12] = {0, 0, 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 7, 7, 8, 7, 6, 6, 7, 6, 5, 5, 6, 5, 4, 4, 5, 4, 3, 4, 4, 3, 2, 1, 1, 1, 0}; pathg[12] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2}; FOR(j,13) siz[j]=SZ(pathx[j]); FOR(j,13) { ttpathx[j] = pathx[j]; ttpathy[j] = pathy[j]; ttpathg[j] = pathg[j]; ttsiz[j] = siz[j]; } /*FOR(j,13) { printf("%d : ", j); FOR(i,siz[j]) printf("%d %d %d; ", pathx[j][i], pathy[j][i], pathg[j][i]); printf("\n"); }*/ /*FOR(j,13) { FOR(i,SZ(pathx[j])) if (pathg[j][i]==1) { assert(pattern[pathx[j][i]][pathy[j][i]] == 'a'+j); pattern[pathx[j][i]][pathy[j][i]] = ' '; } } FOR(i,20) printf("%s.\n", pattern[i].c_str());*/ } int main() { init(); int te; scanf("%d%d", &te, &k); while (te--) test(); //fprintf(stderr, "all ok\n"); 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 | #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = (a); i <= (b); ++i) #define REPD(i,a,b) for (int i = (a); i >= (b); --i) #define FORI(i,n) REP(i,1,n) #define FOR(i,n) REP(i,0,int(n)-1) #define mp make_pair #define pb push_back #define pii pair<int,int> #define vi vector<int> #define ll long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++) #define fi first #define se second const int N = 22; const int T = 5555; const string farm = "R FARMER"; int dx[5] = {0, 1, 0, -1, 0}, dy[5] = {1, 0, -1, 0, 0}; string pattern[20] = { "0bbbkkkjjiihhggffedc", "aabbllkkjjiihhggfedc", "aabbmllkkjjiihhgfedc", "aammmmllkkjjiihgfedc", "klmmmmmllkkjjihgfedc", "kllmmmmmllkkjihgfedc", "kkllmmmmmllkjihgfedc", "jkkllmmmmllkjihgfedc", "jjkkllmmmllkjihgfedc", "ijjkkllllllkjihgfedc", "iijjkklllllkjihgfedc", "hiijjkkkkkkkjihgfedc", "hhiijjjjjjjjjihgfedc", "ghhiiiiiiiiiiihgfedc", "gghhhhhhhhhhhhhgfedc", "fgggggggggggggggfedc", "fffffffffffffffffedc", "eeeeeeeeeeeeeeeeeedc", "dddddddddddddddddddc", "cccccccccccccccccccc" }; /* 0 1 2 3 4 5 6 7 8 9 1 2 4 8 16 32 64 128 256 512 */ int k, n; vector<string> cmds; int field[N][N], inp[N][N]; int have; // gold vi pathx[N], pathy[N], pathg[N]; int siz[N]; vi ttpathx[N], ttpathy[N], ttpathg[N]; int ttsiz[N]; int pos[N]; int cx[N], cy[N], gold[N]; int tot_f, lo_f; int positions[N][N]; void submit_move(int id, int x, int y) { cmds.pb("M " + to_string(cx[id]) + " " + to_string(cy[id]) + " " + to_string(x) + " " + to_string(y)); positions[x][y] = id; positions[cx[id]][cy[id]] = -1; cx[id] = x; cy[id] = y; } void choose_move(int id) { if (pathg[id][pos[id]] == 1) { if (field[cx[id]][cy[id]] > 0) return; // nie zebral jeszcze } if (pathg[id][pos[id]] == 2 && id>1) { // skonczyl gre if (cx[id]<n && positions[cx[id]+1][cy[id]] == -1) { // odsun sie submit_move(id, cx[id]+1, cy[id]); } return; } if (cx[id]==0 && cy[id]==0 && pos[id]==0) { pos[id] = -1; } int nx = pathx[id][pos[id]+1], ny = pathy[id][pos[id]+1]; // kolejny krok if (positions[nx][ny] != -1) return; // zajete if (id==1 && nx==0 && ny==0 && lo_f==0) return; // pierwszy jeszcze nie wrocil, poczekaj submit_move(id, nx, ny); pos[id]++; } int turnn; bool endturn() { turnn++; cmds.pb("="); for (int i=lo_f; i<tot_f; i++) { int gather = min(field[cx[i]][cy[i]], 10); // zbierz gold[i] += gather; field[cx[i]][cy[i]] -= gather; if (cx[i] == 0 && cy[i] == 0) { // oddaj if (gold[i]>0) { //fprintf(stderr, "%d: tura %d\n", i, turnn); } have += gold[i]; gold[i] = 0; } } FOR(i,n) FOR(j,n) if (field[i][j] > 0) return false; for (int i=lo_f; i<tot_f; i++) if (gold[i] > 0) return false; return true; } int cmd_iter; void print_cmds() { for (; cmd_iter < SZ(cmds); cmd_iter++) printf("%s\n", cmds[cmd_iter].c_str()); } void test() { turnn=0; //fprintf(stderr, "test\n"); fflush(stderr); FOR(j,13) { pathx[j] = ttpathx[j]; pathy[j] = ttpathy[j]; pathg[j] = ttpathg[j]; siz[j] = ttsiz[j]; } //n=20; scanf("%d", &n); //FOR(i,n) FOR(j,n) inp[i][j] = 1<<(rand()%10); //inp[0][0]=0; FOR(i,n) FOR(j,n) scanf("%d", &inp[i][j]); FOR(i,n) FOR(j,n) field[i][j] = inp[i][j]; int g0=0,g1=0; while (true) { g0=g1=0; FOR(i,siz[0]) if (pathg[0][i] == 1) g0 += inp[pathx[0][i]][pathy[0][i]]; FOR(i,siz[1]) if (pathg[1][i] == 1) g1 += inp[pathx[1][i]][pathy[1][i]]; //fprintf(stderr, "g = %d %d\n", g0, g1); if (g0+g1 >= 900) break; FOR(i,siz[0]) if (pathx[0][i] == pathx[0][i+1] && pathy[0][i]==0) { pathx[0].pb(pathx[0][siz[0]-2]); pathy[0].pb(pathy[0][siz[0]-2]); pathg[0].pb(pathg[0][siz[0]-2]); pathx[0].pb(pathx[0][siz[0]-1]); pathy[0].pb(pathy[0][siz[0]-1]); pathg[0].pb(pathg[0][siz[0]-1]); for (int j = siz[0]-1; j >= i+3; j--) { pathx[0][j] = pathx[0][j-2]; pathy[0][j] = pathy[0][j-2]; pathg[0][j] = pathg[0][j-2]; } pathx[0][i+1] = pathx[0][i+2] = pathx[0][i]+1; pathy[0][i+1] = 0; pathy[0][i+2] = 1; pathg[0][i+1] = pathg[0][i+2] = 1; siz[0]+=2; break; } FOR(i,siz[1]) if (pathx[1][i] == pathx[1][i+1] && pathy[1][i]==3) { pathx[1].pb(pathx[1][siz[1]-2]); pathy[1].pb(pathy[1][siz[1]-2]); pathg[1].pb(pathg[1][siz[1]-2]); pathx[1].pb(pathx[1][siz[1]-1]); pathy[1].pb(pathy[1][siz[1]-1]); pathg[1].pb(pathg[1][siz[1]-1]); for (int j = siz[1]-1; j >= i+3; j--) { pathx[1][j] = pathx[1][j-2]; pathy[1][j] = pathy[1][j-2]; pathg[1][j] = pathg[1][j-2]; } pathx[1][i+1] = pathx[1][i+2] = pathx[1][i]+1; pathy[1][i+1] = 3; pathy[1][i+2] = 2; pathg[1][i+1] = pathg[1][i+2] = 1; siz[1]+=2; break; } } /*if (g0>g1) { swap(pathx[0], pathx[1]); swap(pathy[0], pathy[1]); swap(pathg[0], pathg[1]); swap(siz[0], siz[1]); }*/ /*FOR(j,13) { fprintf(stderr, "%d : ", j); FOR(i,siz[j]) fprintf(stderr, "%d %d %d; ", pathx[j][i], pathy[j][i], pathg[j][i]); fprintf(stderr, "\n"); }*/ FOR(i,13) pos[i] = 0; FOR(i,n) FOR(j,n) positions[i][j] = -1; FOR(i,n) cx[i] = cy[i] = gold[i] = 0; have = 0; cmds.clear(); cmd_iter=0; cmds.pb(farm); choose_move(0); cmds.pb(farm); choose_move(1); endturn(); print_cmds(); tot_f = 2; lo_f = 0; int iter = 0; while (true) { iter++; /*if (iter%100==0) { printf("turn, %d farm, %d gold\n", tot_f, have); for (int i=lo_f; i<tot_f; i++) printf("i = %d xy = %d %d gold = %d goal = %d, pos = %d/%d -> %d\n", i, cx[i], cy[i], gold[i], pathg[i][pos[i]], pos[i], siz[i], positions[pathx[i][pos[i]]+1][pathy[i][pos[i]]+1]); FOR(i,n) { FOR(j,n) printf("%d ", field[i][j]); printf("\n"); } fflush(stdout); } if (iter>1000) exit(0);*/ //if (iter>5000) exit(0); //printf("turn, %d farm, %d gold\n", tot_f, have); //for (int i=lo_f; i<tot_f; i++) printf("i = %d xy = %d %d gold = %d goal = %d, pos = %d/%d\n", i, cx[i], cy[i], gold[i], pathg[i][pos[i]], pos[i], siz[i]);fflush(stdout); if (have >= 100 && tot_f-lo_f < 11 && positions[0][0] == -1) { have -= 100; cmds.pb(farm); choose_move(tot_f); positions[0][0] = tot_f; tot_f++; } for (int i=lo_f; i<tot_f; i++) { if (i==lo_f && i<2 && cx[i]==0 && cy[i]==0) { tot_f++; lo_f++; } else { choose_move(i); } } bool fin = endturn(); if (fin) break; //print_cmds(); } cmds.pb("==="); print_cmds(); //fprintf(stderr, "iters=%d\n", iter); } bool occ[N][N]; void dfs(int x, int y, int pos) { bool fnd = 1; while (fnd) { fnd = 0; occ[x][y] = 1; FOR(i,4) { int xx = x+dx[i], yy = y+dy[i]; if (xx<0 || yy<0 || xx>=20 || yy>=20) continue; if (pattern[xx][yy] != 'a'+pos) continue; if (occ[xx][yy]) continue; x = xx; y = yy; pathx[pos].pb(x); pathy[pos].pb(y); pathg[pos].pb(1); fnd = 1; break; } assert(SZ(pathx[pos]) < 80); } } void init() { pathx[0] = {1, 2, 3, 3, 2, 1, 1, 0}; pathy[0] = {0, 0, 0, 1, 1, 1, 0, 0}; pathg[0] = {1, 1, 1, 1, 1, 1, 0, 2}; pathx[1] = {0, 0, 0, 1, 2, 2, 1, 0, 0, 0}; pathy[1] = {1, 2, 3, 3, 3, 2, 2, 2, 1, 0}; pathg[1] = {1, 1, 1, 1, 1, 1, 1, 0, 0, 2}; for (int j = 2; 'a'+j <= 'k'; j++) { pathx[j] = {1}; pathy[j] = {0}; pathg[j] = {0}; int m = 0; while (pattern[pathx[j][m]][pathy[j][m]] != 'a'+j) { pathx[j].pb(pathx[j][m]+1); pathy[j].pb(0); pathg[j].pb(0); m++; assert(m<90); } pathg[j].back() = 1; dfs(pathx[j][m], pathy[j][m], j); m = SZ(pathx[j])-1; while (pathy[j][m] > 1) { pathx[j].pb(0); pathy[j].pb(pathy[j][m]-1); pathg[j].pb(0); m++; assert(m<90); } pathx[j].pb(0); pathy[j].pb(0); pathg[j].pb(2); } pathx[11] = {1, 2, 3, 3, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,10,10, 9, 9,10,10,10, 9, 9, 8, 8, 7, 7, 7, 6, 6, 5, 5, 4, 4, 3, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0}; pathy[11] = {0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9,10,10, 9, 9,10,10, 9,10,10, 9, 9, 8, 8, 7, 7, 6, 6, 5, 5, 4, 3, 3, 2, 1, 0}; pathg[11] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2}; pathx[12] = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 8, 7, 7, 6, 6, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 1, 0, 0}; pathy[12] = {0, 0, 0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 8, 8, 7, 7, 8, 7, 6, 6, 7, 6, 5, 5, 6, 5, 4, 4, 5, 4, 3, 4, 4, 3, 2, 1, 1, 1, 0}; pathg[12] = {0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 2}; FOR(j,13) siz[j]=SZ(pathx[j]); FOR(j,13) { ttpathx[j] = pathx[j]; ttpathy[j] = pathy[j]; ttpathg[j] = pathg[j]; ttsiz[j] = siz[j]; } /*FOR(j,13) { printf("%d : ", j); FOR(i,siz[j]) printf("%d %d %d; ", pathx[j][i], pathy[j][i], pathg[j][i]); printf("\n"); }*/ /*FOR(j,13) { FOR(i,SZ(pathx[j])) if (pathg[j][i]==1) { assert(pattern[pathx[j][i]][pathy[j][i]] == 'a'+j); pattern[pathx[j][i]][pathy[j][i]] = ' '; } } FOR(i,20) printf("%s.\n", pattern[i].c_str());*/ } int main() { init(); int te; scanf("%d%d", &te, &k); while (te--) test(); //fprintf(stderr, "all ok\n"); return 0; } |