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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #include <iostream> #include <queue>
using namespace std;
const int N = 55;
int g[N][N], v[N][N]; int n, m; int x[] = {0, -1, 0, 1}; int y[] = {-1, 0, 1, 0};
int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { cin >> g[i][j]; } } queue<pair<int, int>> visited; int cnt = 0, mxar = 0; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) { int arr = 0; if (!v[i][j]) { cnt ++; visited.push({i, j}); } while(!visited.empty()) { int ox = visited.front().first, oy = visited.front().second; visited.pop(); if (v[ox][oy]) continue; v[ox][oy] = 1; arr ++; for (int s = 0; s < 4; ++s) { int nx = ox + x[s], ny = oy + y[s]; if (nx > n || ny > m || nx < 1 || ny < 1) continue; if (!(g[ox][oy] >> s & 1)) { visited.push({nx, ny}); } } } mxar = max(mxar, arr); } } cout << cnt << '\n' << mxar; }
|