https://qoj.ac/problem/7733 代码:
#include <bits/stdc++.h>
#define int long long
const int fx[] = {1, -1, 0, 0}, fy[] = {0, 0, -1, 1};
int t, n, m, vis[1002][1003], ans, pd, can[1002][1002], sz;
char mp[1002][1002], s[1002][1002];
int awa;
// O洞 .人 D空位
void dfs(int x, int y, int lst, int lstx, int lsty, char s[1002][1002]) {
if (x < 1 || x > n || y < 1 || y > m || mp[x][y] == 'O' || vis[x][y])
return;
if (lst != -1) {
// printf("%lld %lld %lld %lld\n", x, y, lstx, lsty);
if (lst == 2 || lst == 1) {
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (s[i][j] == '.') {
int dx = i + fx[lst], dy = j + fy[lst];
if (dx < 1 || dx > n || dy < 1 || dy > m ||
s[dx][dy] == 'O') {
int lx = x - lstx, ly = y - lsty;
int sx = dx - lx, sy = dy - ly;
// printf("AAA %lld %lld %lld %lld\n", i, j, lx,
// ly);
can[sx][sy] = 1;
s[i][j] = 'D';
} else
s[i][j] = 'D', s[dx][dy] = '.';
}
} else {
for (int i = n; i; i--)
for (int j = m; j; j--)
if (s[i][j] == '.') {
int dx = i + fx[lst], dy = j + fy[lst];
if (dx < 1 || dx > n || dy < 1 || dy > m ||
s[dx][dy] == 'O') {
int lx = x - lstx, ly = y - lsty;
int sx = dx - lx, sy = dy - ly;
// printf("AAA %lld %lld %lld %lld\n", i, j, lx,
// ly);
can[sx][sy] = 1;
s[i][j] = 'D';
} else
s[i][j] = 'D', s[dx][dy] = '.';
}
}
// for (int I = 1; I <= n; puts(""), I++)
// for (int J = 1; J <= m; J++)
// printf("%c", s[I][J]);
// puts("");
}
sz++, vis[x][y] = 1, dfs(x + 1, y, 0, lstx, lsty, s),
dfs(x, y + 1, 3, lstx, lsty, s), dfs(x - 1, y, 1, lstx, lsty, s),
dfs(x, y - 1, 2, lstx, lsty, s);
} // 0下 1上 2左 3右
signed main() {
// freopen("kangaroo.in", "r", stdin);
// freopen("kangaroo.out", "w", stdout);
scanf("%lld", &t);
for (; t--;) {
awa++;
scanf("%lld%lld", &n, &m), ans = 0;
for (int i = 1; i <= n; i++)
scanf("%s", mp[i] + 1);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
vis[i][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
if (mp[i][j] == 'O' || vis[i][j])
continue;
sz = 0;
for (int I = 1; I <= n; I++)
for (int J = 1; J <= m; J++)
s[I][J] = mp[I][J], can[I][J] = 0;
// printf("%lld %lld :\n", i, j);
dfs(i, j, -1, i, j, s);
// for (int I = 1; I <= n; I++, puts(""))
// for (int J = 1; J <= m; J++)
// printf("%lld ", can[I][J]);
for (int I = 1; I <= n; I++)
for (int J = 1; J <= m; J++)
if ((I != i || J != j) && !can[I][J] && mp[I][J] == '.')
goto NO;
ans += sz;
NO:;
}
printf("%lld\n", ans);
}
return 0;
}
wa的点:
1
2 5
.O...
.O.OO
不知道为啥输出 0 了
思路:每个联通块的能否胜利的情况是一样的