codeForces 35C

来源:互联网 发布:catch it 编辑:程序博客网 时间:2024/05/21 19:38

Description

input
input.txt
output
output.txt

After a terrifying forest fire in Berland a forest rebirth program was carried out. Due to it N rows with M trees each were planted and the rows were so neat that one could map it on a system of coordinates so that the j-th tree in the i-th row would have the coordinates of(i, j). However a terrible thing happened and the young forest caught fire. Now we must find the coordinates of the tree that will catch fire last to plan evacuation.

The burning began in K points simultaneously, which means that initially K trees started to burn. Every minute the fire gets from the burning trees to the ones that aren’t burning and that the distance from them to the nearest burning tree equals to 1.

Find the tree that will be the last to start burning. If there are several such trees, output any.

Input

The first input line contains two integers N, M (1 ≤ N, M ≤ 2000) — the size of the forest. The trees were planted in all points of the (x, y) (1 ≤ x ≤ N, 1 ≤ y ≤ M) type, x and y are integers.

The second line contains an integer K (1 ≤ K ≤ 10) — amount of trees, burning in the beginning.

The third line contains K pairs of integers: x1, y1, x2, y2, ..., xk, yk (1 ≤ xi ≤ N, 1 ≤ yi ≤ M) — coordinates of the points from which the fire started. It is guaranteed that no two points coincide.

Output

Output a line with two space-separated integers x and y — coordinates of the tree that will be the last one to start burning. If there are several such trees, output any.

Sample Input

Input
3 312 2
Output
1 1
Input
3 311 1
Output
3 3
Input
3 321 1 3 3
Output
2 2
//广度优先搜索,分别从几个着火点开始搜索,初始化其他树木着火事件为无穷。如果遇到一棵可以更新(变少)着火时间的树木,那么就将这棵树推入队列。。。。此题也是文本读入文本读出
#include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <string>using namespace std;#define maxn 2008bool map[2008][2008];int dis[maxn][maxn];int xx[12];int yy[12];int heng[]={0,0,1,-1};int zong[]={1,-1,0,0};inline int max(int a,int b){return a>b?a:b;}int main(){freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);int n,m;while(scanf("%d%d",&n,&m)==2){memset(dis,0x3f,sizeof(dis));memset(map,0,sizeof(map));int k;scanf("%d",&k);for(int i=1;i<=k;i++){int x,y;scanf("%d%d",&x,&y);xx[i]=x;yy[i]=y;map[x][y]=1;dis[x][y]=0;}for(int i=1;i<=k;i++){queue <int> qx;queue <int> qy;qx.push(xx[i]);//xx[i]代表是行数qy.push(yy[i]);while(!qx.empty()){int xxx=qx.front();int yyy=qy.front();qx.pop();qy.pop();map[xxx][yyy]=1;for(int j=0;j<4;j++){int x_=xxx+heng[j];int y_=yyy+zong[j];if((x_>=1&&x_<=n)&&(y_>=1&&y_<=m)){if(dis[xxx][yyy]+1<dis[x_][y_]){dis[x_][y_]=dis[xxx][yyy]+1;qx.push(x_);qy.push(y_);}}}}}int las=-1;int lasx,lasy;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){if(dis[i][j]>las){las=dis[i][j];lasx=i;lasy=j;}}}cout<<lasx<<" "<<lasy<<endl;}return 0;}