Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <cmath>
- #include <algorithm>
- using namespace std;
- int main() {
- int r, c;
- cin >> r >> c;
- int mat[55][55];
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- cin >> mat[i][j];
- }
- }
- bool visited[55][55];
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- visited[i][j] = false;
- }
- }
- int min1 = 10000000;
- queue<int> a;
- int si;
- int sj;
- int roboti = 0;
- for (int x = 0; x < r * c; x++) {
- si = -1;
- sj = -1;
- min1 = 10000000;
- for (int i = 0; i < r; i++) {
- for (int j = 0; j < c; j++) {
- if (min1 > mat[i][j] && !visited[i][j]) {
- min1 = min(mat[i][j], min1);
- si = i;
- sj = j;
- }
- }
- }
- int di[4] = {1, -1, 0, 0};
- int dj[4] = {0, 0, 1, -1};
- if (si != -1) {
- a.push(si);
- a.push(sj);
- roboti++;
- visited[si][sj] = true;
- while (!a.empty()) {
- int ci = a.front();
- a.pop();
- int cj = a.front();
- a.pop();
- for (int i = 0; i < 4; i++) {
- int tmpi = ci + di[i];
- int tmpj = cj + dj[i];
- if (tmpi >= 0 && tmpj >= 0 && tmpi < r && tmpj < c && !visited[tmpi][tmpj] &&
- mat[tmpi][tmpj] >= mat[ci][cj]) {
- a.push(tmpi);
- a.push(tmpj);
- visited[tmpi][tmpj] = true;
- }
- }
- }
- }
- }
- cout <<roboti;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement