QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#198201#6534. Peg SolitaireEason_cyx#AC ✓1ms3948kbC++141.8kb2023-10-03 09:32:312023-10-03 09:32:31

Judging History

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

  • [2023-10-03 09:32:31]
  • 评测
  • 测评结果:AC
  • 用时:1ms
  • 内存:3948kb
  • [2023-10-03 09:32:31]
  • 提交

answer

#include <bits/stdc++.h>
#define MAXN 6
#define MAXM 6
using namespace std;

int n, m, K, ans;
int MAP[MAXN + 5][MAXM + 5];

void dfs(int now) {
    ans = min(ans, now);
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (MAP[i][j]) {
        if (i > 1 && i < n) {
            // 向下跳
            if (MAP[i - 1][j] && !MAP[i + 1][j]) {
                MAP[i - 1][j] = MAP[i][j] = 0;
                MAP[i + 1][j] = 1;
                dfs(now - 1);
                MAP[i - 1][j] = MAP[i][j] = 1;
                MAP[i + 1][j] = 0;
            }

            // 向上跳
            if (!MAP[i - 1][j] && MAP[i + 1][j]) {
                MAP[i + 1][j] = MAP[i][j] = 0;
                MAP[i - 1][j] = 1;
                dfs(now - 1);
                MAP[i + 1][j] = MAP[i][j] = 1;
                MAP[i - 1][j] = 0;
            }
        }

        if (j > 1 && j < m) {
            // 向右跳
            if (MAP[i][j - 1] && !MAP[i][j + 1]) {
                MAP[i][j - 1] = MAP[i][j] = 0;
                MAP[i][j + 1] = 1;
                dfs(now - 1);
                MAP[i][j - 1] = MAP[i][j] = 1;
                MAP[i][j + 1] = 0;
            }

            // 向左跳
            if (!MAP[i][j - 1] && MAP[i][j + 1]) {
                MAP[i][j + 1] = MAP[i][j] = 0;
                MAP[i][j - 1] = 1;
                dfs(now - 1);
                MAP[i][j + 1] = MAP[i][j] = 1;
                MAP[i][j - 1] = 0;
            }
        }
    }
}

void solve() {
    scanf("%d%d%d", &n, &m, &K);
    memset(MAP, 0, sizeof(MAP));
    for (int i = 1; i <= K; i++) {
        int x, y; scanf("%d%d", &x, &y);
        MAP[x][y] = 1;
    }
    ans = K; dfs(K);
    printf("%d\n", ans);
}

int main() {
    int tcase; scanf("%d", &tcase);
    while (tcase--) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3 4 5
2 2
1 2
1 4
3 4
1 1
1 3 3
1 1
1 2
1 3
2 1 1
2 1

output:

2
3
1

result:

ok 3 number(s): "2 3 1"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3912kb

input:

20
2 1 2
1 1
2 1
5 1 3
3 1
2 1
4 1
3 3 6
1 2
2 2
1 1
2 3
3 1
3 2
4 4 4
2 3
3 1
3 2
1 2
1 1 1
1 1
5 2 6
3 2
4 1
2 1
5 2
2 2
5 1
1 3 1
1 2
1 5 1
1 5
4 6 5
4 6
4 4
2 3
4 3
1 6
6 6 3
2 4
1 3
2 1
2 2 2
2 1
1 1
5 3 4
2 2
5 1
4 3
3 2
6 5 6
5 5
6 5
2 4
2 1
3 4
1 4
2 6 5
1 6
2 1
1 4
2 3
1 3
3 5 6
2 1
3 3
1 5...

output:

2
2
3
1
1
2
1
1
3
3
2
1
3
3
2
1
3
1
2
2

result:

ok 20 numbers

Test #3:

score: 0
Accepted
time: 0ms
memory: 3772kb

input:

20
2 1 1
2 1
4 3 2
4 3
1 1
6 4 6
4 3
5 4
4 2
3 2
3 4
2 3
3 6 4
3 1
2 6
2 4
3 5
6 6 6
3 3
3 6
4 2
3 2
4 1
1 4
3 3 1
1 1
2 3 2
2 2
2 1
3 2 2
1 2
2 2
3 4 5
2 1
3 4
3 1
2 2
3 2
2 5 5
1 5
2 3
2 5
1 4
2 2
5 6 6
5 1
2 2
1 4
5 3
4 4
1 1
2 3 4
1 1
1 2
2 2
2 1
2 3 4
1 1
2 1
1 2
2 2
6 3 6
4 2
4 1
1 3
6 2
3 3
2...

output:

1
2
2
4
4
1
1
1
2
2
6
2
2
3
4
1
1
1
3
2

result:

ok 20 numbers

Test #4:

score: 0
Accepted
time: 0ms
memory: 3904kb

input:

20
1 5 3
1 2
1 3
1 1
5 6 6
5 4
2 4
4 4
4 6
2 5
3 3
5 2 4
4 1
3 1
4 2
5 2
6 3 6
3 1
5 2
3 2
4 1
4 3
3 3
5 3 5
5 2
2 2
3 1
4 3
3 2
6 1 4
3 1
5 1
1 1
4 1
3 5 6
1 1
2 1
3 4
1 5
2 2
3 3
2 5 2
2 5
2 1
5 4 6
1 3
4 3
2 2
1 2
3 3
2 3
2 3 3
2 3
1 1
1 3
1 1 1
1 1
1 1 1
1 1
1 1 1
1 1
6 4 5
2 1
6 2
3 2
1 2
4 2
5...

output:

2
1
2
2
1
2
3
2
2
3
1
1
1
3
2
2
2
3
2
1

result:

ok 20 numbers

Test #5:

score: 0
Accepted
time: 1ms
memory: 3908kb

input:

20
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5 2
6 6 6
2 4
3 4
3 3
4 3
4 2
5...

output:

2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2

result:

ok 20 numbers