fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int ans=0,n;
  4. vector <vector <int> > dis(1000,vector <int>(1000,0));
  5. vector <vector <int> > check(1000,vector <int>(1000,0));
  6. const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};
  7. bool checke(int x,int y){
  8. if (x<n && x>=0 && y<n && y>=0) return true;
  9. return false;
  10. }
  11. int main(){
  12. int s;cin >> n >> s;
  13. queue<pair<int,int> > q;
  14. while(s--){
  15. int x,y;cin >> x >> y;
  16. q.push(make_pair(x,y));
  17. check[x][y]=1;
  18. }
  19. while(!q.empty()){
  20. int x=q.front().first,y=q.front().second;q.pop();
  21. ans=max(ans,dis[x][y]);
  22. for(int i=0;i<4;i++){
  23. if(checke(x+dx[i],y+dy[i]) && !check[x+dx[i]][y+dy[i]]){
  24. q.push(make_pair(x+dx[i],y+dy[i]));
  25. check[x+dx[i]][y+dy[i]]=1;
  26. dis[x+dx[i]][y+dy[i]]=dis[x][y]+1;
  27. }
  28. }
  29. /*for(int j=0;j<n;j++){
  30.   for(int i=0;i<n;i++){
  31.   cout << dis[i][j] << " ";
  32.   }
  33.   cout << endl;
  34.   }
  35.   cout << endl;*/
  36. }
  37. cout << ans << endl;
  38. }
Success #stdin #stdout 0s 3280KB
stdin
7 5
0 5
6 3
4 1
5 5
1 0
stdout
4