Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- using namespace std;
- int dx[]={1,0,-1,0};
- int dy[]={0,1,0,-1};
- int main()
- {
- int n,m;
- cin>>n>>m;
- int x[n][m];
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- cin>>x[i][j];
- }
- }
- bool visited[n][m];
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- visited[i][j]=false;
- }
- }
- for(int k = 0;k<n*m;k++){
- int najmal=100000;
- int si=0;
- int sj=0;
- for(int i = 0;i<n;i++){
- for(int j = 0;j<m;j++){
- if(x[i][j]<najmal && visited[i][j]==false){
- najmal=x[i][j];
- si=i;
- sj=j;
- }
- }
- }
- if(najmal==100000){
- cout<<k;
- break;
- }
- visited[si][sj] = true;
- queue<int>q;
- q.push(si);
- q.push(sj);
- while(!q.empty()){
- int ci=q.front();
- q.pop();
- int cj=q.front();
- q.pop();
- for(int i = 0;i<4;i++){
- int ti=ci+dx[i];
- int tj=cj+dy[i];
- if(ti<n && tj<m && ti>=0 && tj>=0 && visited[ti][tj]==false && x[ti][tj]>=x[ci][cj]){
- visited[ti][tj]=true;
- q.push(ti);
- q.push(tj);
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement