QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#339394#7740. Puzzle: Question MarkgoodierAC ✓233ms19688kbC++148.3kb2024-02-27 10:51:282024-02-27 10:51:28

Judging History

你现在查看的是最新测评结果

  • [2024-02-27 10:51:28]
  • 评测
  • 测评结果:AC
  • 用时:233ms
  • 内存:19688kb
  • [2024-02-27 10:51:28]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
const int N = 2e3 + 10,
dx[8][4] =
{ {0, 0, 1, 1},
{0, 0, 1, 2},
{0, 0, 1, 1},
{0, 0, 1, 2},
{0, 0, -1, -1},
{0, 0, -1, -2},
{0, 0, -1, -1},
{0, 0, -1, -2} },
dy[8][4] =
{ {0, 1, 0, 2},
{0, 1, 0, 1},
{0, -1, 0, -2},
{0, -1, 0, -1},
{0, -1, 0, -2},
{0, -1, 0, -1},
{0, 1, 0, 2},
{0, 1, 0, 1} },
M = 10;

int ansc[M][M][M] = {
{
{
0 }
},
{
{
0, 0 },
{
0, 0 }
},
{
{
1, 1, 2 },
{
1, 2, 1 },
{
0, 2, 2 }
},
{
{
1, 1, 2, 2 },
{
1, 2, 1, 2 },
{
3, 3, 4, 4 },
{
3, 4, 3, 4 }
},
{
{
1, 1, 0, 2, 2 },
{
1, 0, 1, 2, 4 },
{
3, 3, 5, 4, 2 },
{
3, 5, 3, 4, 4 },
{
0, 5, 5, 0, 0 }
},
{
{
1, 1, 0, 2, 2, 0 },
{
1, 0, 1, 2, 0, 2 },
{
3, 3, 4, 4, 5, 5 },
{
3, 4, 3, 4, 5, 8 },
{
6, 6, 7, 7, 8, 5 },
{
6, 7, 6, 7, 8, 8 }
},
{
{
1, 1, 0, 2, 2, 3, 3 },
{
1, 0, 1, 2, 3, 2, 3 },
{
4, 4, 7, 5, 5, 6, 6 },
{
4, 7, 4, 5, 6, 5, 6 },
{
0, 7, 7, 8, 8, 9, 9 },
{
10, 10, 11, 8, 11, 8, 9 },
{
10, 0, 10, 11, 11, 9, 0 }
},
{
{
1, 1, 2, 2, 3, 3, 4, 4 },
{
1, 2, 1, 2, 3, 4, 3, 4 },
{
5, 5, 6, 6, 7, 7, 8, 8 },
{
5, 6, 5, 6, 7, 8, 7, 8 },
{
9, 9, 10, 10, 11, 11, 12, 12 },
{
9, 10, 9, 10, 11, 12, 11, 12 },
{
13, 13, 14, 14, 15, 15, 16, 16 },
{
13, 14, 13, 14, 15, 16, 15, 16 }
},
{
{
1, 1, 2, 2, 3, 3, 7, 4, 4 },
{
1, 2, 1, 2, 3, 7, 3, 4, 10 },
{
5, 5, 6, 6, 0, 7, 7, 10, 4 },
{
5, 12, 6, 8, 8, 9, 9, 10, 10 },
{
12, 5, 8, 6, 8, 15, 9, 11, 11 },
{
12, 12, 13, 13, 15, 9, 16, 11, 16 },
{
14, 14, 13, 20, 15, 15, 16, 16, 11 },
{
14, 17, 17, 13, 20, 18, 18, 19, 19 },
{
17, 14, 17, 20, 20, 18, 19, 18, 19 }
}
}, ans[M] = {0, 0, 2, 4, 5, 8, 11, 16, 20};

int idx, T, a[N][N], c[M][M], idc[M];

int valid(int n, int x, int y, int k)
{
    for(int j = 0; j < 4; j++)
    {
        int nx = x + dx[k][j], ny = y + dy[k][j];
        if(nx <= 0 || ny <= 0 || nx > n || ny > n || c[nx][ny]) return 0;
    }
    return 1;
}

void ins0(int n, int x, int y, int k)
{
    ++idc[n];
    for(int j = 0; j < 4; j++)
    {
        int nx = x + dx[k][j], ny = y + dy[k][j];
        c[nx][ny] = idc[n];
    }
}

void del0(int n, int x, int y, int k)
{
    for(int j = 0; j < 4; j++)
    {
        int nx = x + dx[k][j], ny = y + dy[k][j];
        c[nx][ny] = 0;
    }
    --idc[n];
}


void dfs(int n, int x, int y)
{
    //cout << x << " " << y << endl;
    if((ans[n] - idc[n]) * 4 > n * n - ((x - 1) * n + y)) return;
    if(x > n)
    {
        if(idc[n] > ans[n])
        {
            ans[n] = idc[n];
            memcpy(ansc[n], c, sizeof(c));
        }
        return;
    }

    for(int k = 0; k < 8; k++)
    {
        if(valid(n, x, y, k))
        {
            ins0(n, x, y, k);
            dfs(n, x + (y == n), (y == n ? 1 : y + 1));
            del0(n, x, y, k);
        }
    }
    dfs(n, x + (y == n), (y == n ? 1 : y + 1));
}

void ins10(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y] = a[x + 1][y + 2] = idx;
    ++idx;
    a[x][y + 2] = a[x][y + 3] = a[x + 1][y + 1] = a[x + 1][y + 3] = idx;
}

void ins11(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y] = a[x + 2][y + 1] = idx;
    ++idx;
    a[x + 1][y + 1] = a[x + 2][y] = a[x + 3][y] = a[x + 3][y + 1] = idx;
}

void ins21(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y] = a[x + 1][y + 2] = idx;
    ++idx;
    a[x][y + 2] = a[x + 1][y + 1] = a[x + 2][y + 1] = a[x + 2][y + 2] = idx;
}

void ins22(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y + 1] = a[x + 2][y] = idx;
    ++idx;
    a[x + 1][y - 1] = a[x + 1][y] = a[x + 2][y - 1] = a[x + 2][y + 1] = idx;
}

void ins30(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y] = a[x + 2][y + 1] = idx;
    ++idx;
    a[x + 1][y + 1] = a[x + 3][y + 1] = a[x + 2][y + 2] = a[x + 3][y + 2] = idx;
}

void ins31(int x, int y)
{
    ++idx;
    a[x][y] = a[x][y + 1] = a[x + 1][y + 1] = a[x + 1][y - 1] = idx;
    ++idx;
    a[x + 1][y] = a[x + 1][y - 2] = a[x + 2][y - 2] = a[x + 2][y - 1] = idx;
}

void init()
{
    puts("{");
    for(int n = 1; n <= 9; n++)
    {
        memset(ansc[n], 0, sizeof(ansc[n])); ans[n] = 0;
        dfs(n, 1, 1);
        puts("{");
        for(int i = 1; i <= n; i++)
        {
            puts("{");
            for(int j = 1; j <= n; j++)
            {
                if(j < n) printf("%d, ", ansc[n][i][j]);
                else printf("%d ", ansc[n][i][j]);
            }
            if(i < n) puts("},");
            else puts("}");
        }
        if(n < 9) puts("},");
        else puts("}");
    }
    puts("}");
}

void solve1(int n, int x, int y)
{
    if(n == 9)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(ansc[n - 1][i - 1][j - 1]) a[x + i - 1][y + j - 1] = ansc[n - 1][i - 1][j - 1] + idx;
            }
        }
        idx += ans[n - 1];
        return;
    }
    for(int i = 1; i < n / 4; i++)
    {
        ins10(x, y + (i - 1) * 4);
        ins10(x + n - 2, y + (i - 1) * 4);
    }
    ins30(x, y + (n / 4 - 1) * 4);
    ins21(x, y + (n / 4 - 1) * 4 + 2);
    int m = n - 4, i;
    for(i = 4; i < n - 8; i += 4)
    {
        ins11(x + i, y + m);
        ins11(x + i - 1, y + m + 2);
    }
    //cout << endl << x + i - 1 << " " << y + m + 2 << endl;
    ins31(x + i - 1, y + m + 2);
    ins11(x + m, y + m + 2);
    ins22(x + n - 3, y + m);
    solve1(n - 4, x + 2, y);
}

void solve2(int n)
{
    if(n == 6)
    {
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                if(ansc[n - 1][i - 1][j - 1]) a[i][j] = ansc[n - 1][i - 1][j - 1] + idx;
            }
        }
        idx += ans[n - 1];
        return;
    }
    int m = n - 4;
    for(int i = 1; i <= n; i += 2)
    {
        ins10(i, m + 1);
    }
    for(int j = 1; j <= m; j += 2)
    {
        ins11(n - 3, j);
    }
    solve2(n - 4);
}

void solve3(int n)
{
    int m = n - 2;
    for(int i = 1; i < m; i += 4)
    {
        ins11(i, m + 1);
    }
    for(int j = 1; j < m; j += 4)
    {
        ins10(m + 1, j);
    }
    ins22(m, m + 1);
    solve1(n - 2, 1, 1);
}

int main()
{
    //freopen("data.out", "w", stdout);
    //init();
    //return 0;
    scanf("%d", &T);
    while(T--)
    {
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                a[i][j] = 0;
            }
        }
        idx = 0;
        if(n % 4 == 0)
        {
            for(int i = 1; i <= n; i += 4)
            {
                for(int j = 1; j <= n; j += 4)
                {
                    ins10(i, j), ins10(i + 2, j);
                }
            }
        }
        else if(n % 4 == 1)
        {
            if(n >= 9) solve1(n, 1, 1);
            else
            {
                for(int i = 1; i <= n; i++)
                {
                    for(int j = 1; j <= n; j++)
                    {
                        a[i][j] = ansc[n - 1][i - 1][j - 1];
                    }
                }
                idx = ans[n - 1];
            }
        }
        else if(n % 4 == 2)
        {
            if(n >= 6) solve2(n);
            else
            {
                for(int i = 1; i <= n; i++)
                {
                    for(int j = 1; j <= n; j++)
                    {
                        a[i][j] = ansc[n - 1][i - 1][j - 1];
                    }
                }
                idx = ans[n - 1];
            }
        }
        else
        {
            if(n >= 11) solve3(n);
            else
            {
                for(int i = 1; i <= n; i++)
                {
                    for(int j = 1; j <= n; j++)
                    {
                        a[i][j] = ansc[n - 1][i - 1][j - 1];
                    }
                }
                idx = ans[n - 1];
            }
        }

        printf("%d\n", idx);
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                printf("%d ", a[i][j]);
            }
            putchar('\n');
        }
    }
    return 0;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3848kb

input:

2
3
4

output:

2
1 1 2 
1 2 1 
0 2 2 
4
1 1 2 2 
1 2 1 2 
3 3 4 4 
3 4 3 4 

result:

ok Correct. (2 test cases)

Test #2:

score: 0
Accepted
time: 217ms
memory: 7164kb

input:

246
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
...

output:

0
0 
0
0 0 
0 0 
2
1 1 2 
1 2 1 
0 2 2 
4
1 1 2 2 
1 2 1 2 
3 3 4 4 
3 4 3 4 
5
1 1 0 2 2 
1 0 1 2 4 
3 3 5 4 2 
3 5 3 4 4 
0 5 5 0 0 
8
1 1 0 2 2 0 
1 0 1 2 0 2 
3 3 4 4 5 5 
3 4 3 4 5 8 
6 6 7 7 8 5 
6 7 6 7 8 8 
11
1 1 0 2 2 3 3 
1 0 1 2 3 2 3 
4 4 7 5 5 6 6 
4 7 4 5 6 5 6 
0 7 7 8 8 9 9 
10 10 1...

result:

ok Correct. (246 test cases)

Test #3:

score: 0
Accepted
time: 207ms
memory: 8096kb

input:

64
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

output:

15252
247 247 248 248 251 251 252 252 255 255 256 256 259 259 260 260 263 263 264 264 267 267 268 268 271 271 272 272 275 275 276 276 279 279 280 280 283 283 284 284 287 287 288 288 291 291 292 292 295 295 296 296 299 299 300 300 303 303 304 304 307 307 308 308 311 311 312 312 315 315 316 316 319 31...

result:

ok Correct. (64 test cases)

Test #4:

score: 0
Accepted
time: 199ms
memory: 6552kb

input:

45
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

output:

24180
311 311 312 312 315 315 316 316 319 319 320 320 323 323 324 324 327 327 328 328 331 331 332 332 335 335 336 336 339 339 340 340 343 343 344 344 347 347 348 348 351 351 352 352 355 355 356 356 359 359 360 360 363 363 364 364 367 367 368 368 371 371 372 372 375 375 376 376 379 379 380 380 383 38...

result:

ok Correct. (45 test cases)

Test #5:

score: 0
Accepted
time: 198ms
memory: 8456kb

input:

35
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

output:

31684
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 101 ...

result:

ok Correct. (35 test cases)

Test #6:

score: 0
Accepted
time: 205ms
memory: 7456kb

input:

30
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420

output:

38220
391 391 392 392 395 395 396 396 399 399 400 400 403 403 404 404 407 407 408 408 411 411 412 412 415 415 416 416 419 419 420 420 423 423 424 424 427 427 428 428 431 431 432 432 435 435 436 436 439 439 440 440 443 443 444 444 447 447 448 448 451 451 452 452 455 455 456 456 459 459 460 460 463 46...

result:

ok Correct. (30 test cases)

Test #7:

score: 0
Accepted
time: 223ms
memory: 19592kb

input:

2
2000
1000

output:

1000000
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 10...

result:

ok Correct. (2 test cases)

Test #8:

score: 0
Accepted
time: 208ms
memory: 19688kb

input:

2
1999
999

output:

999000
1999 1999 2000 2000 2003 2003 2004 2004 2007 2007 2008 2008 2011 2011 2012 2012 2015 2015 2016 2016 2019 2019 2020 2020 2023 2023 2024 2024 2027 2027 2028 2028 2031 2031 2032 2032 2035 2035 2036 2036 2039 2039 2040 2040 2043 2043 2044 2044 2047 2047 2048 2048 2051 2051 2052 2052 2055 2055 205...

result:

ok Correct. (2 test cases)

Test #9:

score: 0
Accepted
time: 216ms
memory: 19636kb

input:

2
1998
998

output:

998000
997993 997993 0 997994 997994 0 997977 997977 997978 997978 997953 997953 997954 997954 997921 997921 997922 997922 997881 997881 997882 997882 997833 997833 997834 997834 997777 997777 997778 997778 997713 997713 997714 997714 997641 997641 997642 997642 997561 997561 997562 997562 997473 99...

result:

ok Correct. (2 test cases)

Test #10:

score: 0
Accepted
time: 232ms
memory: 19632kb

input:

2
1997
997

output:

997002
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 101...

result:

ok Correct. (2 test cases)

Test #11:

score: 0
Accepted
time: 217ms
memory: 19608kb

input:

2
1996
996

output:

996004
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 101...

result:

ok Correct. (2 test cases)

Test #12:

score: 0
Accepted
time: 201ms
memory: 19616kb

input:

2
1995
995

output:

995006
1995 1995 1996 1996 1999 1999 2000 2000 2003 2003 2004 2004 2007 2007 2008 2008 2011 2011 2012 2012 2015 2015 2016 2016 2019 2019 2020 2020 2023 2023 2024 2024 2027 2027 2028 2028 2031 2031 2032 2032 2035 2035 2036 2036 2039 2039 2040 2040 2043 2043 2044 2044 2047 2047 2048 2048 2051 2051 205...

result:

ok Correct. (2 test cases)

Test #13:

score: 0
Accepted
time: 233ms
memory: 19608kb

input:

2
1994
994

output:

994008
994001 994001 0 994002 994002 0 993985 993985 993986 993986 993961 993961 993962 993962 993929 993929 993930 993930 993889 993889 993890 993890 993841 993841 993842 993842 993785 993785 993786 993786 993721 993721 993722 993722 993649 993649 993650 993650 993569 993569 993570 993570 993481 99...

result:

ok Correct. (2 test cases)

Test #14:

score: 0
Accepted
time: 228ms
memory: 19600kb

input:

2
1993
993

output:

993012
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 101...

result:

ok Correct. (2 test cases)

Test #15:

score: 0
Accepted
time: 217ms
memory: 19456kb

input:

2
1992
992

output:

992016
1 1 2 2 5 5 6 6 9 9 10 10 13 13 14 14 17 17 18 18 21 21 22 22 25 25 26 26 29 29 30 30 33 33 34 34 37 37 38 38 41 41 42 42 45 45 46 46 49 49 50 50 53 53 54 54 57 57 58 58 61 61 62 62 65 65 66 66 69 69 70 70 73 73 74 74 77 77 78 78 81 81 82 82 85 85 86 86 89 89 90 90 93 93 94 94 97 97 98 98 101...

result:

ok Correct. (2 test cases)

Test #16:

score: 0
Accepted
time: 218ms
memory: 19440kb

input:

2
1991
991

output:

991020
1991 1991 1992 1992 1995 1995 1996 1996 1999 1999 2000 2000 2003 2003 2004 2004 2007 2007 2008 2008 2011 2011 2012 2012 2015 2015 2016 2016 2019 2019 2020 2020 2023 2023 2024 2024 2027 2027 2028 2028 2031 2031 2032 2032 2035 2035 2036 2036 2039 2039 2040 2040 2043 2043 2044 2044 2047 2047 204...

result:

ok Correct. (2 test cases)

Extra Test:

score: 0
Extra Test Passed