Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int arr[1010][1010];
- pair<int, int> pp;
- set<pair<int, int>> spots;
- void remover(int i, int j) {
- pp = make_pair(i, j);
- spots.erase(pp);
- arr[i][j] = 0;
- if(arr[i - 1][j] == 1 and i - 1 >= 0) {
- remover(i - 1, j);
- }
- if(arr[i + 1][j] == 1) {
- remover(i + 1, j);
- }
- if(arr[i][j + 1] == 1) {
- remover(i, j + 1);
- }
- if(arr[i][j - 1] == 1 and j - 1 >= 0) {
- remover(i, j - 1);
- }
- return;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- int n, m;
- cin >> n >> m;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> arr[i][j];
- if(arr[i][j] == 1) {
- pp = make_pair(i, j);
- spots.insert(pp);
- }
- }
- }
- long long ans = 0;
- //cout << spots.size() << "\n";
- while(spots.size() != 0) {
- auto itr = spots.begin();
- pp = *itr;
- remover(pp.first, pp.second);
- //cout << pp.first << " " << pp.second << "\n";
- ans++;
- }
- cout << ans << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement