QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#328026 | #833. Cells Blocking | Hunster | TL | 1ms | 10012kb | C++14 | 3.2kb | 2024-02-15 16:18:48 | 2024-02-15 16:18:50 |
Judging History
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;
}
Path to_s(int x, int y, bool tag) {
if (x == 1 && y == 1) return {};
if (tag) {
if (int tx = x - 1, ty = y; tx >= 1 && vis1[tx][ty]) return merge(to_s(tx, ty, tag), {{tx, ty}});
if (int tx = x, ty = y - 1; ty >= 1 && vis1[tx][ty]) return merge(to_s(tx, ty, tag), {{tx, ty}});
}
else {
if (int tx = x, ty = y - 1; ty >= 1 && vis1[tx][ty]) return merge(to_s(tx, ty, tag), {{tx, ty}});
if (int tx = x - 1, ty = y; tx >= 1 && vis1[tx][ty]) return merge(to_s(tx, ty, tag), {{tx, ty}});
}
assert(false);
}
Path to_e(int x, int y, bool tag) {
if (x == n && y == m) return {};
if (tag) {
if (int tx = x + 1, ty = y; tx <= n && vis2[tx][ty]) return merge({{tx, ty}}, to_e(tx, ty, tag));
if (int tx = x, ty = y + 1; ty <= m && vis2[tx][ty]) return merge({{tx, ty}}, to_e(tx, ty, tag));
}
else {
if (int tx = x, ty = y + 1; ty <= m && vis2[tx][ty]) return merge({{tx, ty}}, to_e(tx, ty, tag));
if (int tx = x + 1, ty = y; tx <= n && vis2[tx][ty]) return merge({{tx, ty}}, to_e(tx, ty, tag));
}
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 = merge(to_s(n, m, 0), {{n, m}}), sec = merge(to_s(n, m, 1), {{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;
ans += count(merge(merge(to_s(px, py, 0), {{px, py}}), to_e(px, py, 1)), 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: 0ms
memory: 10012kb
input:
3 3 ... ... ...
output:
17
result:
ok 1 number(s): "17"
Test #2:
score: 0
Accepted
time: 0ms
memory: 9952kb
input:
3 3 .** .*. ...
output:
15
result:
ok 1 number(s): "15"
Test #3:
score: 0
Accepted
time: 1ms
memory: 5808kb
input:
3 4 **** .... ****
output:
6
result:
ok 1 number(s): "6"
Test #4:
score: 0
Accepted
time: 1ms
memory: 5796kb
input:
5 5 *.... .*.*. ***** *.*** ..*..
output:
66
result:
ok 1 number(s): "66"
Test #5:
score: 0
Accepted
time: 1ms
memory: 6124kb
input:
10 10 ...***.*.. **...*.*** ...***.*.. .**...*.*. .*****..*. ..*.****.* .**...**** ..*..*.*.* *.*.**.... ....**...*
output:
1378
result:
ok 1 number(s): "1378"
Test #6:
score: -100
Time Limit Exceeded
input:
3000 3000 .....................................................................................................................................................................................................................................................................................................