minimario

203549J

Jun 25th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <utility>
  2. #define pb push_back
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <vector>
  7. #include <string>
  8. #include <fstream>
  9.  
  10. using namespace std;
  11.  
  12. int main() {
  13.     ifstream cin("input.txt");
  14.     ofstream cout("output.txt");
  15.     int n, m;
  16.     cin >> n >> m;
  17.     int trees;
  18.     cin >> trees;
  19.     queue<pair<int, int> > q;
  20.     bool visited[2005][2005];
  21.     for (int i=0; i < trees; i++) {
  22.         int x, y;
  23.         cin >> x >> y;
  24.         q.push(pair<int, int>(x, y));
  25.         visited[x][y] = 1;
  26.     }
  27.  
  28.     int a1[4] = {1, 0, -1, 0};
  29.     int a2[4] = {0, 1, 0, -1};
  30.     int x, y;
  31.     while (!q.empty()) {
  32.         pair<int, int> v = q.front();
  33.         x = v.first;
  34.         y = v.second;
  35.         q.pop();
  36.         for (int i=0; i<4; i++) {
  37.             int nx = a1[i] + x;
  38.             int ny = a2[i] + y;
  39.             if (nx >= 1 && nx <= n && ny >= 1 && ny <= m && !visited[nx][ny]) {
  40.                 visited[x][y] = 1;
  41.                 q.push(pair<int, int>(nx, ny));
  42.             }
  43.         }
  44.     }
  45.     cout << x << " " << y << endl;
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment