#include <iostream> #include <vector> #include <string> #include <utility> #include <stack> using namespace std; void print_vector_2d(const std::vector<std::vector<int>>& vec) { std::cout << "[\n"; for (size_t i = 0; i < vec.size(); ++i) { std::cout << "["; for (size_t j = 0; j < vec[i].size(); ++j) { std::cout << vec[i][j]; if (j < vec[i].size() - 1) { std::cout << ", "; } } std::cout << "]"; if (i < vec.size() - 1) { std::cout << ",\n"; } } std::cout << "\n]" << std::endl; } int main() { int n, m; cin >> n >> m; vector<string> boxes(n); for (int i = 0; i < n; i++) cin >> boxes[i]; vector<pair<int, int>> v; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (boxes[i].substr(j, 1) == "#") v.push_back(make_pair(i, j)); } } vector<vector<int>> component(n, vector<int>(m, -1)); int num = 0; stack<pair<int, int>> s; pair<int, int> now, i; for (pair<int, int> u : v) { if (component[u.first][u.second] != -1) continue; s = stack<pair<int, int>>(); s.push(u); component[u.first][u.second] = num; while (!s.empty()) { now = s.top(); s.pop(); i = make_pair(now.first+1, now.second); if ( i.first >= 0 && i.first < n && i.second >= 0 && i.second < m && boxes[i.first].substr(i.second, 1) == "#" && component[i.first][i.second] == -1 ) { s.push(i); component[i.first][i.second] = num; } i = make_pair(now.first-1, now.second); if ( i.first >= 0 && i.first < n && i.second >= 0 && i.second < m && boxes[i.first].substr(i.second, 1) == "#" && component[i.first][i.second] == -1 ) { s.push(i); component[i.first][i.second] = num; } i = make_pair(now.first, now.second+1); if ( i.first >= 0 && i.first < n && i.second >= 0 && i.second < m && boxes[i.first].substr(i.second, 1) == "#" && component[i.first][i.second] == -1 ) { s.push(i); component[i.first][i.second] = num; } i = make_pair(now.first, now.second-1); if ( i.first >= 0 && i.first < n && i.second >= 0 && i.second < m && boxes[i.first].substr(i.second, 1) == "#" && component[i.first][i.second] == -1 ) { s.push(i); component[i.first][i.second] = num; } cout << num << " "; num++; print_vector_2d(component); } } cout << num << "\n"; vector<int> cc_sizes(num); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (component[i][j] != -1) cc_sizes[component[i][j]]++; } } int max = 0; for (int i = 0; i < num; i++) { if (cc_sizes[i] > max) max = cc_sizes[i]; } cout << max << "\n"; }
5 6 ..##.. ..##.. .#.... ###..# .#..#.
0 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1] ] 1 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1] ] 2 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1] ] 3 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1] ] 4 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [-1, 4, -1, -1, -1, -1], [-1, -1, -1, -1, -1, -1] ] 5 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, -1], [-1, 5, -1, -1, -1, -1] ] 6 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, -1], [-1, 5, -1, -1, -1, -1] ] 7 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, -1], [-1, 5, -1, -1, -1, -1] ] 8 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, -1], [-1, 5, -1, -1, -1, -1] ] 9 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, 9], [-1, 5, -1, -1, -1, -1] ] 10 [ [-1, -1, 0, 0, -1, -1], [-1, -1, 0, 1, -1, -1], [-1, 4, -1, -1, -1, -1], [5, 4, 5, -1, -1, 9], [-1, 5, -1, -1, 10, -1] ] 11 3