caojiaming @ 2022-12-24 15:41:44
#include <bits/stdc++.h>
using namespace std;
char pic[100][100];
int a[100][100];
int n, m;
void dfs(int r, int c)
{
if(r < 0 || r >= m || c < 0 || c >= n) return;
if(a[r][c] != 0) return;
dfs(r + 1,c);
dfs(r - 1,c);
dfs(r,c + 1);
dfs(r,c - 1);
}
int main()
{
cin >> m;
n = m;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> pic[i][j];
if(pic[i][j] == '0')
{
a[i][j] = 0;
}
else
{
a[i][j] = 2;
}
}
}
dfs(0,0);
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(a[i][j] == 0)
{
cout << "2 ";
}
else
{
cout << pic[i][j] << " ";
}
}
cout << '\n';
}
return 0;
}
提交记录
by caojiaming @ 2022-12-24 15:44:09
第九行后面掉了一句
a[r][c] = 1;
by Jerrlee✅ @ 2022-12-24 15:59:23
@caojiaming 好像改成 1-base 就可以过
#include<bits/stdc++.h>
using namespace std;
char pic[100][100];
int a[100][100];
int n, m;
void dfs(int r, int c)
{
if(r<0||r>n+1||c<0||c>n+1||a[r][c]!=0) return;
a[r][c] = 1;
dfs(r + 1,c);
dfs(r - 1,c);
dfs(r,c + 1);
dfs(r,c - 1);
}
int main()
{
cin >> m;
n = m;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> pic[i][j];
if(pic[i][j] == '0')
{
a[i][j] = 0;
}
else
{
a[i][j] = 2;
}
}
}
dfs(0,0);
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
if(a[i][j] == 0)
{
cout << "2 ";
}
else
{
cout << pic[i][j] << " ";
}
}
cout << '\n';
}
}
原因应该是 dfs 扫描的起点问题。