QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#24119#2955. Stable Tablemaze#AC ✓6ms5176kbC++142.8kb2022-03-26 16:14:042022-04-30 04:58:40

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-04-30 04:58:40]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:5176kb
  • [2022-03-26 16:14:04]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define N 110
#define pb push_back

int in[N][N];

vector<int> e[N * N];
vector<int> e2[N * N];

bool vis[N * N];

int dist1[N * N];
int dist2[N * N];
int dist3[N * N];

int main()
{
    int h, w;
    scanf("%d%d", &h, &w);
    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            scanf("%d", &in[i][j]);
        }
    }
    for (int j = 0; j < w; j++)
        in[h][j] = 0;

    int id1 = in[0][0], id2 = in[0][0];

    for (int j = 0; j < w; j++)
        if (in[0][j] != id2)
        {
            id2 = in[0][j];
            break;
        }

    for (int i = 0; i < h; i++)
    {
        for (int j = 0; j < w; j++)
        {
            if (in[i][j] != in[i + 1][j])
            {
                e[in[i][j]].pb(in[i + 1][j]);
                e2[in[i + 1][j]].pb(in[i][j]);
            }
        }
    }

    for (int i = 0; i < N * N; i++)
    {
        dist1[i] = N;
        dist2[i] = N;
        dist3[i] = N;
    }

    queue<int> q;
    q.push(id1);
    dist1[id1] = 1;
    vis[id1] = 1;
    while (q.size())
    {
        int x = q.front();
        q.pop();

        for (int i = 0; i < e[x].size(); i++)
        {
            if (!vis[e[x][i]])
            {
                q.push(e[x][i]);
                dist1[e[x][i]] = dist1[x] + 1;
                vis[e[x][i]] = 1;
            }
        }
    }
    memset(vis, 0, sizeof vis);

    q.push(id2);
    dist2[id2] = 1;
    vis[id2] = 1;
    while (q.size())
    {
        int x = q.front();
        q.pop();

        for (int i = 0; i < e[x].size(); i++)
        {
            if (!vis[e[x][i]])
            {
                q.push(e[x][i]);
                dist2[e[x][i]] = dist2[x] + 1;
                vis[e[x][i]] = 1;
            }
        }
    }
    memset(vis, 0, sizeof vis);

    q.push(0);
    dist3[0] = 1;
    vis[0] = 1;
    while (q.size())
    {
        int x = q.front();
        q.pop();

        for (int i = 0; i < e2[x].size(); i++)
        {
            if (!vis[e2[x][i]])
            {
                q.push(e2[x][i]);
                dist3[e2[x][i]] = dist3[x] + 1;
                vis[e2[x][i]] = 1;
            }
        }
    }

    // for (int i = 0; i < 14; i++)
    // {
    //     printf("%d ", dist1[i]);
    // }
    // puts("");

    // for (int i = 0; i < 14; i++)
    // {
    //     printf("%d ", dist2[i]);
    // }
    // puts("");

    // for (int i = 0; i < 14; i++)
    // {
    //     printf("%d ", dist3[i]);
    // }
    // puts("");

    int ans = 3 * N;
    for (int i = 0; i < N * N; i++)
    {
        ans = min(ans, dist1[i] + dist2[i] + dist3[i] - 3);
    }
    printf("%d\n", ans);

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 4404kb

input:

7 8
3 3 3 3 10 10 10 10
2 14 3 7 7 10 4 11
2 14 3 1 1 10 4 11
2 14 8 8 8 8 4 11
9 14 5 5 5 5 4 13
9 14 12 12 12 12 4 13
9 6 6 6 6 6 6 13

output:

5

result:

ok single line: '5'

Test #2:

score: 0
Accepted
time: 3ms
memory: 4468kb

input:

8 8
1 1 1 1 1 1 1 1
2 3 3 4 5 6 6 7
2 2 3 4 5 6 7 7
2 3 3 3 6 6 6 7
2 2 3 8 8 8 6 7
9 2 9 10 10 11 11 12
9 9 9 9 10 11 12 12
13 9 14 14 10 10 15 15

output:

3

result:

ok single line: '3'

Test #3:

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

input:

10 10
1 1 1 1 1 1 1 1 1 1
1 2 2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1 2
3 4 5 6 7 8 9 10 11 2
12 13 5 6 7 7 7 7 2 2
14 15 5 6 6 6 6 7 16 16
14 15 15 15 6 16 16 16 16 16
14 14 14 15 16 16 17 16 18 19
15 15 15 15 20 20 20 20 18 19
20 20 20 20 20 21 21 21 21 19

output:

4

result:

ok single line: '4'

Test #4:

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

input:

10 8
20 20 20 20 16 16 16 16
18 19 1 3 3 21 17 2
18 19 1 4 4 21 17 2
18 19 1 5 5 21 17 2
18 19 6 6 6 6 17 2
18 7 7 7 7 7 7 2
18 8 8 8 8 8 8 8
9 9 9 9 9 9 11 11
14 14 9 9 9 9 12 12
15 15 10 10 10 10 13 13

output:

7

result:

ok single line: '7'

Test #5:

score: 0
Accepted
time: 3ms
memory: 4468kb

input:

10 5
4 4 4 2 2
4 4 4 8 8
15 4 1 1 8
3 4 9 9 9
3 6 6 6 6
3 13 13 12 12
3 10 10 12 12
3 14 14 7 7
11 11 11 7 7
5 5 5 7 7

output:

7

result:

ok single line: '7'

Test #6:

score: 0
Accepted
time: 3ms
memory: 4556kb

input:

10 5
4 4 4 2 2
4 4 4 8 8
15 4 1 1 8
3 4 9 9 9
3 4 6 6 6
3 13 13 12 12
3 10 10 12 12
3 14 14 7 7
11 11 11 7 7
5 5 5 7 7

output:

8

result:

ok single line: '8'

Test #7:

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

input:

10 10
1 1 1 1 1 1 1 1 1 1
2 2 3 1 1 1 1 1 4 4
2 2 5 6 7 1 1 8 9 9
2 2 10 11 12 13 14 8 9 9
15 15 16 17 18 13 14 14 19 19
20 20 16 21 22 23 23 23 24 24
25 26 21 21 27 28 28 29 30 30
31 32 21 33 34 34 35 36 37 38
39 40 21 41 41 42 42 43 43 43
44 40 45 46 47 48 49 50 51 43

output:

6

result:

ok single line: '6'

Test #8:

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

input:

50 50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 ...

output:

100

result:

ok single line: '100'

Test #9:

score: 0
Accepted
time: 6ms
memory: 5096kb

input:

100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 ...

output:

200

result:

ok single line: '200'

Test #10:

score: 0
Accepted
time: 2ms
memory: 4552kb

input:

8 12
13 13 13 13 13 13 13 13 3 3 3 3
13 13 13 13 13 13 13 13 3 2 2 3
13 13 13 1 1 13 13 13 3 3 3 3
13 13 13 1 1 13 13 13 8 11 11 11
13 13 13 13 13 7 8 8 8 11 11 11
13 13 13 13 13 7 9 9 9 11 11 11
4 4 4 4 6 6 10 10 10 11 12 12
5 5 5 5 6 6 10 12 12 12 12 12

output:

5

result:

ok single line: '5'

Test #11:

score: 0
Accepted
time: 4ms
memory: 4608kb

input:

50 50
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 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 ...

output:

75

result:

ok single line: '75'

Test #12:

score: 0
Accepted
time: 3ms
memory: 4552kb

input:

11 11
3 3 3 3 3 2 2 2 2 2 2
3 5 3 14 3 2 8 2 2 2 6
3 5 3 14 14 2 2 7 2 6 6
13 5 5 14 14 2 7 7 9 10 10
13 5 15 15 14 2 7 1 1 1 1
12 12 15 15 14 2 2 2 2 2 1
12 15 15 15 15 1 1 2 2 16 1
12 12 4 20 20 1 19 1 1 1 1
12 12 4 20 20 1 19 1 18 18 17
11 12 4 4 20 1 19 1 18 18 17
11 11 11 4 4 1 1 1 18 18 17

output:

6

result:

ok single line: '6'

Test #13:

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

input:

25 25
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2
1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 5 6 7 7 8
1 1 1 1 1 1 1 9 1 1 1 1 1 1 1 10 10 4 4 4 11 6 7 8 8
1 1 1 1 1 1 1 1 1 1 1 1 1 1 10 10 12 13 14 4 15 8 8 8 8
1 1 16 1 1 1 1 1 1 1 17 1 1 18 10 10 12 12 12 19 19 8 8 8 8
1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

4

result:

ok single line: '4'

Test #14:

score: 0
Accepted
time: 5ms
memory: 4620kb

input:

100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 1 1 1 3 1 1 1 1 4 4 5 6 1 1 7 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 9 10 1 1 1 11 12 12 13 1 1...

output:

17

result:

ok single line: '17'

Test #15:

score: 0
Accepted
time: 3ms
memory: 4408kb

input:

10 10
1 1 1 1 1 1 1 1 1 2
3 4 4 1 5 5 6 1 7 8
9 4 10 10 10 11 11 1 12 13
9 14 10 15 15 16 17 18 18 19
20 14 21 15 22 23 24 25 18 26
14 14 27 27 22 28 29 30 18 31
14 32 27 33 28 28 29 34 34 31
14 27 27 35 36 37 37 38 39 40
41 27 42 42 43 44 38 38 45 40
46 47 42 42 42 48 49 38 50 51

output:

12

result:

ok single line: '12'

Test #16:

score: 0
Accepted
time: 2ms
memory: 4452kb

input:

20 20
1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 4 4 5 6 6 7 8 9 9 10 2 11 12 2 13 2 2 14 14
3 15 16 17 18 18 19 8 20 9 10 21 22 22 13 13 23 23 14 24
3 25 25 18 18 18 26 27 20 28 10 21 22 29 13 30 31 32 14 24
3 3 25 18 18 33 34 35 36 28 10 37 38 39 40 30 30 14 14 41
42 43 44 45 46 33 34 47 48 49 50 5...

output:

14

result:

ok single line: '14'

Test #17:

score: 0
Accepted
time: 3ms
memory: 4408kb

input:

8 10
1 1 1 1 1 2 2 2 2 2
21 24 1 1 5 5 6 6 7 8
3 4 1 1 9 9 6 6 17 18
3 4 1 1 10 10 6 6 17 18
22 25 1 1 1 1 6 6 19 20
23 26 11 12 13 14 15 16 19 20
23 26 11 12 13 27 27 16 19 20
23 26 11 12 13 28 28 16 19 20

output:

5

result:

ok single line: '5'

Test #18:

score: 0
Accepted
time: 3ms
memory: 4400kb

input:

12 11
8 8 8 8 8 8 8 8 11 11 11
14 14 8 10 10 10 10 8 2 8 11
14 8 8 8 10 8 8 8 2 8 11
14 8 10 8 10 8 11 8 8 8 11
14 14 10 10 10 6 11 11 11 11 11
5 14 14 10 6 6 6 6 11 6 11
5 5 14 14 14 6 6 11 11 6 6
5 13 13 13 17 17 6 6 6 6 1
5 3 16 17 17 6 6 1 1 1 1
7 3 4 4 4 4 4 4 4 1 15
7 3 3 3 9 9 9 9 4 1 15
12 1...

output:

5

result:

ok single line: '5'

Test #19:

score: 0
Accepted
time: 3ms
memory: 4532kb

input:

12 11
8 8 8 8 8 8 8 8 11 11 11
14 14 8 10 10 10 10 8 2 8 11
14 8 8 8 10 8 8 8 2 8 11
14 8 10 8 10 8 11 8 8 8 11
14 14 10 10 10 6 11 11 11 11 11
5 14 14 10 6 6 6 6 11 6 11
5 5 14 14 14 6 6 11 11 6 6
5 13 13 13 14 14 6 6 6 6 1
5 3 13 14 14 6 6 1 1 1 1
7 3 4 4 4 4 4 4 4 1 15
7 3 3 3 9 9 9 9 4 1 15
12 1...

output:

5

result:

ok single line: '5'

Test #20:

score: 0
Accepted
time: 3ms
memory: 4696kb

input:

100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...

output:

101

result:

ok single line: '101'

Test #21:

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

input:

3 4
8 8 8 8
5 6 7 8
1 2 3 4

output:

2

result:

ok single line: '2'

Test #22:

score: 0
Accepted
time: 2ms
memory: 5172kb

input:

100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 3...

output:

100

result:

ok single line: '100'

Test #23:

score: 0
Accepted
time: 4ms
memory: 5176kb

input:

100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 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 3...

output:

99

result:

ok single line: '99'

Test #24:

score: 0
Accepted
time: 3ms
memory: 4552kb

input:

8 3
1 1 1
2 2 3
4 4 3
5 5 3
6 6 3
6 3 3
6 7 7
6 8 8

output:

4

result:

ok single line: '4'

Test #25:

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

input:

8 3
1 1 1
2 2 3
4 4 3
5 5 3
5 3 3
5 6 6
5 7 7
5 8 8

output:

4

result:

ok single line: '4'

Test #26:

score: 0
Accepted
time: 3ms
memory: 4468kb

input:

20 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 1 3 1 1 1 1 1 1 4 5 6 1 1 1 7 7 8 1
2 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 7 7 7 7
9 3 10 3 11 11 12 12 12 1 1 1 1 1 1 7 7 7 7 7
13 13 13 13 14 14 14 14 15 14 1 1 1 1 16 7 7 7 7 7
13 13 13 17 14 14 14 14 14 14 18 19 20 20 21 21 22 7 7 7
23 24 24 14 14 14 14...

output:

5

result:

ok single line: '5'

Test #27:

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

input:

18 4
6 6 12 12
13 3 3 5
13 21 21 5
13 4 4 5
13 32 32 5
7 7 14 14
8 15 15 16
8 2 2 16
8 27 27 16
1 1 9 9
17 11 11 10
17 35 35 10
18 19 26 22
20 19 26 25
28 19 26 31
30 19 26 34
33 19 26 24
29 19 26 23

output:

12

result:

ok single line: '12'

Test #28:

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

input:

50 50
1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 4 1 1 1 1 1 1 1 2 2 2 2 5 2 6 6 2 2 2 2 2 2 2 7 7 8 2 2 2 9 2 2 2 10 2 2 2 2 2 2 2 2 2 2 11 12 2 2 2
13 13 1 1 1 1 1 1 1 1 2 2 14 2 2 2 15 2 2 16 2 17 17 2 7 7 8 18 18 19 20 20 21 22 10 10 2 2...

output:

7

result:

ok single line: '7'

Test #29:

score: 0
Accepted
time: 4ms
memory: 4660kb

input:

75 75
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
1 3 4 5 1 1 1 1 1 1 1 1 1 1 1 1 6 6 6 7 8 8 9 9 9 10 10 10 2 11 12 13 14 2 2 2 2 2 15 2 2 2 2 2 2 2 16 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 17 ...

output:

13

result:

ok single line: '13'

Test #30:

score: 0
Accepted
time: 3ms
memory: 4588kb

input:

60 20
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 3 1 1 1 1 1 1 1 4 1 1 1 5 1 1
6 1 1 7 7 8 8 9 1 1 1 10 10 4 1 1 1 1 11 11
12 12 1 13 7 8 8 1 1 1 1 1 14 4 1 1 1 1 15 15
16 17 1 13 7 1 1 1 1 1 18 1 1 19 20 21 21 22 22 22
16 17 23 24 24 1 25 26 1 1 1 1 1 1 1 27 27 22 22 22
28 28 28 28 29 30 25 ...

output:

9

result:

ok single line: '9'

Test #31:

score: 0
Accepted
time: 3ms
memory: 4572kb

input:

100 3
1 1 1
1 1 1
2 2 1
3 2 2
3 4 4
5 4 4
5 5 4
6 5 7
8 9 7
8 8 8
8 8 8
10 11 12
10 10 13
10 10 13
10 10 14
10 10 14
10 10 10
10 10 10
10 10 10
10 10 15
10 10 15
10 10 10
16 16 16
16 16 16
16 16 16
16 16 16
16 17 18
19 20 20
19 20 20
19 19 19
21 22 22
21 21 21
21 21 21
21 21 21
23 21 21
24 21 25
26 ...

output:

28

result:

ok single line: '28'

Test #32:

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

input:

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

output:

5

result:

ok single line: '5'

Test #33:

score: 0
Accepted
time: 3ms
memory: 4580kb

input:

30 75
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 3 3 1 1 4 1 5 1 1 1 6 7 1 1 1 1 1 8 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 9 1 1 10 10 11 1 1 1 1 1 1 1 1 1 12 12 12 13 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1...

output:

5

result:

ok single line: '5'