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.

Examples

Input
  1. 3 3
    1
    2 2
Output
  1. 1 1
Input
  1. 3 3
    1
    1 1
Output
  1. 3 3
Input
  1. 3 3
    2
    1 1 3 3
Output
  1. 2 2
  2.  
  3. 题意:
    给你一个n*m的矩阵,每一个节点表示是一个树,
    然后给你k个坐标,表示坐标位置的树着火了,
    并且火在蔓延,每一秒火只能蔓延到矩阵内相邻4个位置的树上,
    让求最后着火的坐标。
    思路:
    显然bfs嘛,
    queue中加入的是结构体,结构体来维护每一个节点的信息,分别是坐标xy和第几秒烧到这个树t,然后像四个方向bfs即可。
    细节见代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <queue>
  7. #include <stack>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <iomanip>
  12. #define ALL(x) (x).begin(), (x).end()
  13. #define rt return
  14. #define dll(x) scanf("%I64d",&x)
  15. #define xll(x) printf("%I64d\n",x)
  16. #define sz(a) int(a.size())
  17. #define all(a) a.begin(), a.end()
  18. #define rep(i,x,n) for(int i=x;i<n;i++)
  19. #define repd(i,x,n) for(int i=x;i<=n;i++)
  20. #define pii pair<int,int>
  21. #define pll pair<long long ,long long>
  22. #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
  23. #define MS0(X) memset((X), 0, sizeof((X)))
  24. #define MSC0(X) memset((X), '\0', sizeof((X)))
  25. #define pb push_back
  26. #define mp make_pair
  27. #define fi first
  28. #define se second
  29. #define eps 1e-6
  30. #define gg(x) getInt(&x)
  31. #define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
  32. using namespace std;
  33. typedef long long ll;
  34. ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
  35. ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
  36. ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
  37. inline void getInt(int* p);
  38. const int maxn=;
  39. const int inf=0x3f3f3f3f;
  40. /*** TEMPLATE CODE * * STARTS HERE ***/
  41. int n,m;
  42. int vis[][];
  43. struct node
  44. {
  45. int x;
  46. int y;
  47. int t;
  48. node(){}
  49. node(int xx,int yy,int tt)
  50. {
  51. x=xx;
  52. y=yy;
  53. t=tt;
  54. }
  55. };
  56. int xx[]={-,,,};
  57. int yy[]={,,-,};
  58. int main()
  59. {
  60. // 注意题目给定了读入和输出的路径。
  61. freopen("input.txt","r",stdin);
  62. freopen("output.txt","w",stdout);
  63. gbtb;
  64. cin>>n>>m;
  65. int k;
  66. cin>>k;
  67. int x,y;
  68. queue<node> q;
  69. int cnt=;
  70. int ansx,ansy;
  71. int mnum=;
  72. repd(i,,k)
  73. {
  74. cin>>x>>y;
  75. vis[x][y]=;
  76. ansx=x;
  77. ansy=y;
  78. q.push(node(x,y,));
  79. }
  80. cnt=k;
  81. node temp;
  82. while(!q.empty())
  83. {
  84. temp=q.front();
  85. q.pop();
  86. repd(i,,)
  87. {
  88. x=temp.x+xx[i];
  89. y=temp.y+yy[i];
  90. if(x>=&&x<=n&&y>=&&y<=m&&vis[x][y]==)
  91. {
  92. cnt++;
  93. if(temp.t+>mnum)
  94. {
  95. mnum=temp.t+;
  96. ansx=x;
  97. ansy=y;
  98. }
  99. vis[x][y]=;
  100. q.push(node(x,y,temp.t+));
  101. }
  102. if(cnt==n*m)// 矩阵中的全部的树都着火过了。优化时间。
  103. {
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. cout<<ansx<<" "<<ansy<<endl;
  110.  
  111. return ;
  112. }
  113.  
  114. inline void getInt(int* p) {
  115. char ch;
  116. do {
  117. ch = getchar();
  118. } while (ch == ' ' || ch == '\n');
  119. if (ch == '-') {
  120. *p = -(getchar() - '');
  121. while ((ch = getchar()) >= '' && ch <= '') {
  122. *p = *p * - ch + '';
  123. }
  124. }
  125. else {
  126. *p = ch - '';
  127. while ((ch = getchar()) >= '' && ch <= '') {
  128. *p = *p * + ch - '';
  129. }
  130. }
  131. }
  1.  

Fire Again CodeForces - 35C (BFS)的更多相关文章

  1. Amr and Chemistry CodeForces 558C(BFS)

    http://codeforces.com/problemset/problem/558/C 分析:将每一个数在给定范围内(10^5)可变成的数(*2或者/2)都按照广搜的方式生成访问一遍,标记上访问 ...

  2. Kilani and the Game CodeForces - 1105D (bfs)

    Kilani is playing a game with his friends. This game can be represented as a grid of size n×mn×m, wh ...

  3. Fire Game FZU - 2150 (bfs)

    Problem 2150 Fire Game Accept: 3772    Submit: 12868Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  4. codeforces #Round354-div2-D(BFS)

    题目链接:题目链接 题意:一个n*m的区域,每个格子都有上下左右四个门,相邻的两个格子A可以通向B当且仅当A对B的门和B对A的门都打开,问从起点S到终点T需要的最短时间 #include<bit ...

  5. Statues CodeForces - 129C(bfs)

    In this task Anna and Maria play a game with a very unpleasant rival. Anna and Maria are in the oppo ...

  6. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  7. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  8. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  9. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

随机推荐

  1. Python基础(zip方法)

    zip函数: 描述:将zip函数中的两个可迭代对象参数按对应索引值进行匹配组合,得到zip对象.(拉链式函数) zip函数简单应用如下: #-----------------zip函数-------- ...

  2. ASP.NET Core中使用GraphQL - 第四章 GraphiQL

    ASP.NET Core中使用GraphQL ASP.NET Core中使用GraphQL - 第一章 Hello World ASP.NET Core中使用GraphQL - 第二章 中间件 ASP ...

  3. ToastCustomUtil【简单的Toast封装类】【自定义Toast的显示风格】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 ToastUtil + ToastCustom结合.主要解决低版本机型上系统toast显示不好看的问题. 效果图 代码分析 在Toa ...

  4. ConfirmCancelBottomSheetDialog【确认取消底部对话框】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 继承BottomSheetDialog,实现简单的确认取消对话框样式. 效果图 代码分析 ConfirmCancelBottomSh ...

  5. 使用 EOLINKER 进行接口测试的最佳路径 (下)

    本文为 <使用 EOLINKER 进行接口测试的最佳路径> 下半部分文章,主要介绍测试脚本如何执行和报告生成,以及测试项目人员如何协作.还没看过上篇文章请戳 使用 EOLINKER 进行接 ...

  6. Jquer + Ajax 制作上传图片文件

    没什么 说的  直接 上代码 //选择图片并上传 function selectImg(node){ var f = node.value; var file = node.files[0]; if( ...

  7. C#保留2位小数几种场景总结

    场景1: C#保留2位小数,.ToString("f2")确实可以,但是如果这个数字本来就小数点后面三位比如1.253,那么转化之后就会变成1.25.可不可以刚好保留到最后一位不是 ...

  8. tablednd onDrap 方法不调用

    场景 使用 tablednd 插件时,onDrap 方法不调用 解决 给tr标签加 id 属性

  9. java集合(1)

    java集合类存放于java.util包里,只能存放对象,存放的是对象的引用,可以是不同类型,不限数量的数据类型. 顶层接口:Iterator(迭代器),Map Iterator:核心方法hasNex ...

  10. javascript排序算法-快速排序

    快速排序 概念: (1) 首先,从数组中选择中间一项作为主元. (2) 创建两个指针,左边一个指向数组第一个项,右边一个指向数组最后一个项.移动左指针直到我们找到一个比主元大的元素,接着,移动右指针直 ...