QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#328038#833. Cells BlockingHunsterAC ✓496ms30976kbC++143.8kb2024-02-15 16:25:392024-02-15 16:25:40

Judging History

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

  • [2024-02-15 16:25:40]
  • 评测
  • 测评结果:AC
  • 用时:496ms
  • 内存:30976kb
  • [2024-02-15 16:25:39]
  • 提交

answer

#include <bits/stdc++.h>

using LL = long long;

constexpr int N = 3003;

int n, m;
char map[N][N];
bool vis1[N][N], vis2[N][N];
bool tag[N][N];

void dfs1(int x, int y) {
    vis1[x][y] = 1;
    if (int tx = x + 1, ty = y; tx <= n && map[tx][ty] == '.' && !vis1[tx][ty]) dfs1(tx, ty);
    if (int tx = x, ty = y + 1; ty <= m && map[tx][ty] == '.' && !vis1[tx][ty]) dfs1(tx, ty);
}
void dfs2(int x, int y) {
    vis2[x][y] = 1;
    if (int tx = x - 1, ty = y; tx >= 1 && map[tx][ty] == '.' && !vis2[tx][ty]) dfs2(tx, ty);
    if (int tx = x, ty = y - 1; ty >= 1 && map[tx][ty] == '.' && !vis2[tx][ty]) dfs2(tx, ty);
}

using Path = std::vector<std::pair<int, int>>;
int count(Path a, Path b) {
    int cnt = 0;
    for (int i = 0; i < a.size(); i++) if (a[i] == b[i])
        cnt += !tag[a[i].first][a[i].second];
    return cnt;
}
Path merge(Path a, Path b) {
    for (auto t : b) a.push_back(t);
    return a;
}

void to_s(int x, int y, bool tag, Path &path) {
    if (x == 1 && y == 1) return;
    if (tag) {
        if (int tx = x - 1, ty = y; tx >= 1 && vis1[tx][ty]) {
            to_s(tx, ty, tag, path);
            path.push_back({tx, ty});
            return;
        }
        if (int tx = x, ty = y - 1; ty >= 1 && vis1[tx][ty]) {
            to_s(tx, ty, tag, path);
            path.push_back({tx, ty});
            return;
        }
    }
    else {
        if (int tx = x, ty = y - 1; ty >= 1 && vis1[tx][ty]) {
            to_s(tx, ty, tag, path);
            path.push_back({tx, ty});
            return;
        }
        if (int tx = x - 1, ty = y; tx >= 1 && vis1[tx][ty]) {
            to_s(tx, ty, tag, path);
            path.push_back({tx, ty});
            return;
        }
    }
    assert(false);
}
void to_e(int x, int y, bool tag, Path &path) {
    if (x == n && y == m) return;
    if (tag) {
        if (int tx = x + 1, ty = y; tx <= n && vis2[tx][ty]) {
            path.push_back({tx, ty});
            to_e(tx, ty, tag, path);
            return;
        }
        if (int tx = x, ty = y + 1; ty <= m && vis2[tx][ty]) {
            path.push_back({tx, ty});
            to_e(tx, ty, tag, path);
            return;
        }
    }
    else {
        if (int tx = x, ty = y + 1; ty <= m && vis2[tx][ty]) {
            path.push_back({tx, ty});
            to_e(tx, ty, tag, path);
            return;
        }
        if (int tx = x + 1, ty = y; tx <= n && vis2[tx][ty]) {
            path.push_back({tx, ty});
            to_e(tx, ty, tag, path);
            return;
        }
    }
    assert(false);
}

int main() {
    scanf("%d%d", &n, &m);
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        scanf("%s", map[i] + 1);
        for (int j = 1; j <= m; j++) cnt += map[i][j] == '.';
    }
    if ([&] {
        if (map[1][1] == '*' || map[n][m] == '*') return true;      
        dfs1(1, 1);
        dfs2(n, m);
        return !vis1[n][m];
    }()) {
        printf("%lld\n", 1ll * cnt * (cnt - 1) / 2);
        return 0;
    }
    Path fir, sec;
    to_s(n, m, 0, fir);
    fir.push_back({n, m});
    to_s(n, m, 1, sec);
    sec.push_back({n, m});
    int t = 0;
    for (int i = 0; i < fir.size(); i++) if (fir[i] == sec[i]) {
        tag[fir[i].first][fir[i].second] = 1;
        t++;
    }
    LL ans = 1ll * t * (cnt - t) + 1ll * t * (t - 1) / 2;
    for (auto [px, py] : fir) {
        if (tag[px][py]) continue;
        while (true) {
            px--;
            py++;
            if (px < 1 || py > m) break;
            if (!vis1[px][py] || !vis2[px][py]) continue;
            Path cur;
            to_s(px, py, 0, cur);
            cur.push_back({px, py});
            to_e(px, py, 1, cur);
            ans += count(cur, sec);
            break;
        }
    }
    printf("%lld\n", ans);
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 10168kb

input:

3 3
...
...
...

output:

17

result:

ok 1 number(s): "17"

Test #2:

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

input:

3 3
.**
.*.
...

output:

15

result:

ok 1 number(s): "15"

Test #3:

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

input:

3 4
****
....
****

output:

6

result:

ok 1 number(s): "6"

Test #4:

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

input:

5 5
*....
.*.*.
*****
*.***
..*..

output:

66

result:

ok 1 number(s): "66"

Test #5:

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

input:

10 10
...***.*..
**...*.***
...***.*..
.**...*.*.
.*****..*.
..*.****.*
.**...****
..*..*.*.*
*.*.**....
....**...*

output:

1378

result:

ok 1 number(s): "1378"

Test #6:

score: 0
Accepted
time: 434ms
memory: 30976kb

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

17999999

result:

ok 1 number(s): "17999999"

Test #7:

score: 0
Accepted
time: 444ms
memory: 30704kb

input:

3000 3000
...................................................................................................................*......................................................................................................................................................................*..........

output:

17981671

result:

ok 1 number(s): "17981671"

Test #8:

score: 0
Accepted
time: 446ms
memory: 30704kb

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

17963615

result:

ok 1 number(s): "17963615"

Test #9:

score: 0
Accepted
time: 445ms
memory: 30696kb

input:

3000 3000
.........................................................................................................*...........................................................................................................................................................................................

output:

17945165

result:

ok 1 number(s): "17945165"

Test #10:

score: 0
Accepted
time: 446ms
memory: 30652kb

input:

3000 3000
......................................................................................................................................*........................................................................................................................................*.....................

output:

17928211

result:

ok 1 number(s): "17928211"

Test #11:

score: 0
Accepted
time: 454ms
memory: 30964kb

input:

3000 3000
...........................................*.........................................................................................................................................................................................................................................................

output:

17911522

result:

ok 1 number(s): "17911522"

Test #12:

score: 0
Accepted
time: 455ms
memory: 30972kb

input:

3000 3000
..............................*................................................................................................................*.....................................................................................................................................................

output:

17892283

result:

ok 1 number(s): "17892283"

Test #13:

score: 0
Accepted
time: 462ms
memory: 30688kb

input:

3000 3000
................................................................*....*................................................................................................................................................................................*..............................................

output:

17873837

result:

ok 1 number(s): "17873837"

Test #14:

score: 0
Accepted
time: 464ms
memory: 30736kb

input:

3000 3000
............................................................................................*.............................................................................*.....................................................................................................*....................

output:

17856701

result:

ok 1 number(s): "17856701"

Test #15:

score: 0
Accepted
time: 450ms
memory: 30708kb

input:

3000 3000
......................................*..........................................................................................................................................................*...................................................................................................

output:

17837857

result:

ok 1 number(s): "17837857"

Test #16:

score: 0
Accepted
time: 433ms
memory: 30960kb

input:

3000 3000
.................................................................................................................................................................................................................................*...................................................................

output:

17819731

result:

ok 1 number(s): "17819731"

Test #17:

score: 0
Accepted
time: 488ms
memory: 30680kb

input:

3000 3000
......**.....*.......*.*..........*..*...............**.............*.......*......*........*...*.....*.*.................*......*....*.........*....................*.................*.......................*.......*..*.*.......*.......................*..........*..*......................*...

output:

16202000

result:

ok 1 number(s): "16202000"

Test #18:

score: 0
Accepted
time: 496ms
memory: 30920kb

input:

3000 3000
..................*....*....*...*.*.............*.............*....*.*..*...*...*...*....*.................*...*.*.***...*....*......*.......**...*.......*.*...**...*...*...**.........*..........*.....*.*....*..*.......*.........*..*.....*...............**.......*.....*.*..*.*.*........*.....

output:

21600132

result:

ok 1 number(s): "21600132"

Test #19:

score: 0
Accepted
time: 17ms
memory: 15252kb

input:

3000 3000
..*.**...*...............*........*.*..*.*.....*........*.*..........***..*..*..*..*.*....*...*.*.....***.*...*........*..*.****..*.*....**.......*......*....*..*......*......*..*..*.*..*....*..**.*.......**.*...*....**.....**..*......*...*....*..*.**.*..***...*.....*....***.*........*.......

output:

19862779430431

result:

ok 1 number(s): "19862779430431"

Test #20:

score: 0
Accepted
time: 20ms
memory: 15208kb

input:

3000 3000
.**.**..***....*.*....*..*...*.**.**.**.......*...*........*.**.*...*...**..*...*.*.**.*.*.*.*..*...*.....*.*.**.*.*....*.**.....*..**.**.*....**.**.**..*..**...*...***.**.*.*......**.**.*...****.....***.*..*.**.*......*..**.**.**.....**...*.*..***.******...**....****..***..**.*........*.....

output:

14601805246666

result:

ok 1 number(s): "14601805246666"

Test #21:

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

input:

1 1
*

output:

0

result:

ok 1 number(s): "0"

Test #22:

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

input:

1 1
.

output:

0

result:

ok 1 number(s): "0"

Test #23:

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

input:

2 2
..
..

output:

6

result:

ok 1 number(s): "6"

Test #24:

score: 0
Accepted
time: 10ms
memory: 13156kb

input:

3000 3000
.***..**..*.*.*..*...**.*.**...***.....*..***.***.***.*..***.*......*.**.***.***.*...**.*.*..***.*..*.**..***.....*.*...***.*.***.*...*.*.....***.*..**...*.*..*.******.*.*...**.*..**.**.**.*.**..***.**.***..*......**.***.**.*....*..*.....*...*..*.*..*..*.*...**.**...*..**..***.**..*....*.....

output:

10151159625145

result:

ok 1 number(s): "10151159625145"

Test #25:

score: 0
Accepted
time: 12ms
memory: 14492kb

input:

3000 3000
*******************************************************************************************************************************************************************************************************************************************************.******************************************...

output:

39716328

result:

ok 1 number(s): "39716328"

Test #26:

score: 0
Accepted
time: 448ms
memory: 30684kb

input:

3000 3000
..*..................................................................................................................................................................................................................................................................................................

output:

35988321

result:

ok 1 number(s): "35988321"

Test #27:

score: 0
Accepted
time: 440ms
memory: 30632kb

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

35981866

result:

ok 1 number(s): "35981866"

Test #28:

score: 0
Accepted
time: 445ms
memory: 30692kb

input:

3000 3000
...**................................................................................................................................................................................................................................................................................................

output:

17988153

result:

ok 1 number(s): "17988153"

Test #29:

score: 0
Accepted
time: 452ms
memory: 30692kb

input:

3000 3000
...*.*...............................................................................................................................................................................................................................................................................................

output:

35969654

result:

ok 1 number(s): "35969654"

Test #30:

score: 0
Accepted
time: 443ms
memory: 30644kb

input:

3000 3000
..*..................................................................................................................................................................................................................................................................................................

output:

17982216

result:

ok 1 number(s): "17982216"

Test #31:

score: 0
Accepted
time: 454ms
memory: 30764kb

input:

3000 3000
....**.*.............................................................................................................................................................................................................................................................................................

output:

71916090

result:

ok 1 number(s): "71916090"

Test #32:

score: 0
Accepted
time: 432ms
memory: 30712kb

input:

3000 3000
....****.............................................................................................................................................................................................................................................................................................

output:

71903186

result:

ok 1 number(s): "71903186"

Test #33:

score: 0
Accepted
time: 453ms
memory: 30752kb

input:

3000 3000
.........*...........................................................................................................................................................................................................................................................................................

output:

17973051

result:

ok 1 number(s): "17973051"

Test #34:

score: 0
Accepted
time: 442ms
memory: 30928kb

input:

3000 3000
.*.......*.............................................................................................................*................................................................................*............................................................................................

output:

35630636

result:

ok 1 number(s): "35630636"

Test #35:

score: 0
Accepted
time: 462ms
memory: 30660kb

input:

3000 3000
.**...........................*..........................................................................................................................................*...........................................................................................................................

output:

44529907

result:

ok 1 number(s): "44529907"

Test #36:

score: 0
Accepted
time: 466ms
memory: 30700kb

input:

3000 3000
....*................................................................................................................................................................................................................................................................................................

output:

53426863

result:

ok 1 number(s): "53426863"

Test #37:

score: 0
Accepted
time: 437ms
memory: 30716kb

input:

3000 3000
..*.*........................................................................*............................*...............*.............................................................................................................................................*........*...................

output:

53419301

result:

ok 1 number(s): "53419301"

Test #38:

score: 0
Accepted
time: 436ms
memory: 30692kb

input:

3000 3000
......*.........*...................................................................................*.....................................................................................................................*................................................*.........................

output:

26705269

result:

ok 1 number(s): "26705269"

Test #39:

score: 0
Accepted
time: 441ms
memory: 30692kb

input:

3000 3000
....*.**....*................*............................*..........*...............................................................................................................................................................................................................................

output:

17799069

result:

ok 1 number(s): "17799069"

Test #40:

score: 0
Accepted
time: 447ms
memory: 30696kb

input:

3000 3000
...***.......................................*...........*.....................*........*...........................................................................................................................................................*................................................

output:

53393629

result:

ok 1 number(s): "53393629"

Test #41:

score: 0
Accepted
time: 449ms
memory: 30652kb

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

98852811

result:

ok 1 number(s): "98852811"

Test #42:

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

input:

1 3000
........................................................................................................................................................................................................................................................................................................

output:

4498500

result:

ok 1 number(s): "4498500"

Test #43:

score: 0
Accepted
time: 464ms
memory: 30928kb

input:

3000 3000
.*.....*...........................................................................................................*...................................*........................................................................................*............*.......................................

output:

88979547

result:

ok 1 number(s): "88979547"

Test #44:

score: 0
Accepted
time: 457ms
memory: 30692kb

input:

3000 3000
.....................................................................................................................................................................................................................................................................................................

output:

35964327

result:

ok 1 number(s): "35964327"