[](https://z3.ax1x.com/2021/07/07/RHbGUH.gif)
by shuhao @ 2024-02-19 10:37:19
@[shuhao](/user/1097884) xx,yy很有可能越界
by Hot_tear @ 2024-02-19 10:40:29
@[Hot_tear](/user/737112) 是肯定会越界,我还以为你95分呢。。。
by Hot_tear @ 2024-02-19 10:40:53
```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[110][110];
bool vis[110][110];
int dx[4] = {-1, 0, 0,-1};
int dy[4] = { 0,-1, 1, 0};
void dfs(int x, int y)
{
vis[x][y] = 1;
for(int i = 0; i < 4; i++)
{
int xx = x + dx[i];
int yy = y + dy[i];
if(xx > 0 && yy > 0 && xx <= n && yy <= m)
if(vis[xx][yy] == 0 && '1' <= a[xx][yy] && a[xx][yy] <= '9')
dfs(xx, yy);
}
}
int main()
{
int ans = 0;
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
{
a[i][j] = getchar();
if(vis[i][j] == 0 && '1' <= a[i][j] && a[i][j] <= '9')
dfs(i, j), ans++;
}
printf("%d", ans);
return 0;
}
```
![]("C:\Users\HH\Pictures\Screenshots\屏幕截图 2024-02-19 105908.png")
by shuhao @ 2024-02-19 11:00:14
~~![]("C:\Users\HH\Pictures\Screenshots\屏幕截图 2024-02-19 105908.png")~~
by shuhao @ 2024-02-19 11:03:06
**输出19**
by shuhao @ 2024-02-19 11:04:02
样例输出19
by shuhao @ 2024-02-19 11:04:41
@[shuhao](/user/1097884) 要输入完再 `dfs` 吧,不然有可能搜到了还没输入的地方。
by xudongyi1 @ 2024-02-19 13:29:03