Untitled

 avatar
unknown
c_cpp
2 years ago
1.2 kB
5
Indexable
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
using namespace std ;
queue<pair<int , int>> q;
int dx[4] = {0 , 1 , -1 , 0};
int dy[4] = {1 , 0 , 0 , -1};
const int maxn = 2e3+4;
bool mark[maxn][maxn];
int k , t , n, m , ans1 , ans2;

bool check(int  x , int y){
    if(x > n || y > m || x <= 0 || y <= 0 || mark[x][y])
        return false;
    return true;    
}

void bfs(int u , int v){
    mark[u][v] = true;
    q.push(make_pair(u , v));
    while(!q.empty()){
         k = q.front().first;
         t = q.front().second;
         q.pop();
        for(int i = 0 ; i < 4 ; i++){
            if(check(k + dx[i] , t + dy[i])){
                mark[k+dx[i]][t+dy[i]] = true;
                q.push(make_pair(k+ dx[i] , t + dy[i]));
            }
        }
    ans1 = k , ans2 = t;
    }

}
int main(){
    ios;
    int c , x , y;
    cin >> n >> m >> c;
    for(int i = 0 ; i < c ; i++){
        cin >> x >> y;
        q.push(make_pair(x , y));
        mark[x][y] = true;
    }
    bfs(q.front().first , q.front().second);
    cout << ans1 << ' ' << ans2;

    return 0;
}
Editor is loading...