社区讨论
为什么WA
P1506拯救oibh总部参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @mi7d3tu9
- 此快照首次捕获于
- 2025/11/20 19:42 4 个月前
- 此快照最后确认于
- 2025/11/20 19:42 4 个月前
绝望ing...
CPP#include <bits/stdc++.h>
#define re register
using namespace std;
const int N = 505;
char graph[N][N];
int n, m, cnt, dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
inline int read(){
int f = 0, x = 0; char ch;
do {ch = getchar(); f |= ch == '-';} while (!isdigit(ch));
do {x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} while (isdigit(ch));
return f ? -x : x;
}
inline bool check(int x, int y){
return x > 0 && x <= n && y > 0 && y <= m;
}
inline void dfs1(int x, int y){
for (re int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if (check(nx, ny) && graph[nx][ny] != '*'){
graph[nx][ny] = '*';
dfs1(nx, ny);
}
}
}
inline void dfs2(int x, int y){
for (re int i = 0; i < 4; ++i){
int nx = x + dx[i], ny = y + dy[i];
if (check(nx, ny) && graph[nx][ny] != '*'){
graph[nx][ny] = '*';
dfs2(nx, ny);
}
}
}
int main(){
//freopen("data.txt", "r", stdin);
n = read(), m = read();
for (re int i = 1; i <= n; ++i)scanf("%s", graph[i] + 1);
for (re int i = 1; i <= n; ++i){
if (graph[i][1] == '0'){
graph[i][1] = '*';
dfs1(i, 1);
}
if (graph[1][i] == '0'){
graph[1][i] = '*';
dfs1(1, i);
}
if (graph[n][i] == '0'){
graph[n][i] = '*';
dfs1(n, i);
}
if (graph[i][n] == '0'){
graph[i][n] = '*';
dfs1(i, n);
}
}
for (re int i = 1; i <= n; ++i){
for (re int j = 1; j <= m; ++j){
if (graph[i][j] == '0'){
++cnt;
dfs2(i, j);
}
}
}
printf("%d", cnt);
return 0;
}
回复
共 0 条回复,欢迎继续交流。
正在加载回复...