1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include<bits/stdc++.h> using namespace std; const int maxm = 1e5 + 10;
int n,m; vector<char > v[maxm]; vector<bool> vis[maxm]; const int dirx[4] = {1, -1, 0, 0}; const int diry[4] = {0, 0, 1, -1}; int cnt, ans; bool ans_once;
void dfs(int x, int y){ vis[x][y] = true; if(v[x][y] >= '2' && v[x][y] <= '9') ans_once = true; for(int i = 0 ; i < 4 ; i ++){ int nx = x + dirx[i]; int ny = y + diry[i]; if(nx >= 1 && nx <= n && ny >= 1 && ny <= m && !vis[nx][ny] && v[nx][ny] != '0'){ dfs(nx, ny); } } }
int main(void){ cin>>n>>m; for(int i = 1 ; i <= n ; i ++){ v[i].resize(m+1); vis[i].resize(m+1); for(int j = 1 ; j <= m ; j ++){ cin>>v[i][j]; } } for(int i = 1 ; i <= n ; i ++){ for(int j = 1 ; j <= m ; j ++){ if(!vis[i][j] && v[i][j] != '0'){ ans_once = false; dfs(i, j); cnt++; if(ans_once) ans++; } } } cout<<cnt<<" "<<ans; return 0; }
|