Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <algorithm>
- using namespace std;
- int main()
- {
- int n, m;
- cin >> n >> m;
- int mat[n][m];
- vector<pair<int, pair<int, int>>> v;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- cin >> mat[i][j];
- v.push_back(make_pair(mat[i][j], make_pair(i, j)));
- }
- }
- sort(v.begin(), v.end());
- bool visited[n][m];
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- visited[i][j] = false;
- }
- }
- int di[] = {-1, 1, 0, 0};
- int dj[] = {0, 0, -1, 1};
- int robotcinja = 0;
- for(int i = 0; i < v.size(); i++) {
- int x = v[i].second.first;
- int y = v[i].second.second;
- if(!visited[x][y]) {
- queue<int> q;
- q.push(x);
- q.push(y);
- visited[x][y] = true;
- robotcinja++;
- while(!q.empty()) {
- int ci = q.front();
- q.pop();
- int cj = q.front();
- q.pop();
- for(int k = 0; k < 4; k++) {
- int ti = ci + di[k];
- int tj = cj + dj[k];
- if(ti >= 0 and ti < n and tj >= 0 and tj < m and !visited[ti][tj]) {
- if(mat[ci][cj] <= mat[ti][tj]) {
- q.push(ti);
- q.push(tj);
- visited[ti][tj] = true;
- }
- }
- }
- }
- }
- }
- cout << robotcinja << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement