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
3 3
1
2 2
Output
1 1
Input
3 3
1
1 1
Output
3 3
Input
3 3
2
1 1 3 3
Output
2 2

题意:
给你一个n*m的矩阵,每一个节点表示是一个树,
然后给你k个坐标,表示坐标位置的树着火了,
并且火在蔓延,每一秒火只能蔓延到矩阵内相邻4个位置的树上,
让求最后着火的坐标。
思路:
显然bfs嘛,
queue中加入的是结构体,结构体来维护每一个节点的信息,分别是坐标x,y和第几秒烧到这个树t,然后像四个方向bfs即可。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
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;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
int vis[][];
struct node
{
int x;
int y;
int t;
node(){}
node(int xx,int yy,int tt)
{
x=xx;
y=yy;
t=tt;
}
};
int xx[]={-,,,};
int yy[]={,,-,};
int main()
{
// 注意题目给定了读入和输出的路径。
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
gbtb;
cin>>n>>m;
int k;
cin>>k;
int x,y;
queue<node> q;
int cnt=;
int ansx,ansy;
int mnum=;
repd(i,,k)
{
cin>>x>>y;
vis[x][y]=;
ansx=x;
ansy=y;
q.push(node(x,y,));
}
cnt=k;
node temp;
while(!q.empty())
{
temp=q.front();
q.pop();
repd(i,,)
{
x=temp.x+xx[i];
y=temp.y+yy[i];
if(x>=&&x<=n&&y>=&&y<=m&&vis[x][y]==)
{
cnt++;
if(temp.t+>mnum)
{
mnum=temp.t+;
ansx=x;
ansy=y;
}
vis[x][y]=;
q.push(node(x,y,temp.t+));
}
if(cnt==n*m)// 矩阵中的全部的树都着火过了。优化时间。
{
break;
}
}
} cout<<ansx<<" "<<ansy<<endl; return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
 

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. Android开发:文本控件详解——TextView(一)基本属性

    一.简单实例: 新建的Android项目初始自带的Hello World!其实就是一个TextView. 在activity_main.xml中可以新建TextView,从左侧组件里拖拽到右侧预览界面 ...

  2. python --- 冒泡排序算法

    别想太多了,这个冒泡排序就是我们脑海中想到的那个冒泡,就好像是气泡一样,较小的元素比较轻,从而要往上浮出来, 冒泡排序算法. 要对‘气泡’序列处理若干遍.所谓一遍处理,就是自底向上检查一遍这个序列,并 ...

  3. git 建议使用

    1 登录github官网首页 创建一个项目 2 本地克隆下载git项目 git clone https://github.com/wangguoxingduanxuejing/branch-pract ...

  4. 《HelloGitHub》第 28 期

    <HelloGitHub>第 28 期 兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程. ...

  5. 运输问题的+Leapms模型

    运输问题的+Leapms模型 运输问题是本科教课书中的一个经典章节.运输问题的线性规划模型非常简单,而且求解难度极小. 问题 一个公司生产并销售一种产品.该公司有m个产地.n个销地.产地 i 的供给量 ...

  6. netstat命令总结

    nestat介绍 netstat是一款命令行工具,可用于列出系统上所有的网络套接字连接情况,包括 tcp, udp 以及 unix 套接字,另外它还能列出处于监听状态(即等待接入请求)的套接字. 使用 ...

  7. kubectl自动补全

    source <(kubectl completion bash) echo "source <(kubectl completion bash)" >> ...

  8. revit融合

    解决了嵌入部分也会布置砖胎膜或土方问题 1.需根据板往相应方向拉伸,创建拉伸体(非实例) 2.根据轮廓创建融合体 3.将两个物体融合 //创建平面 //创建草图平面,文档必须是族文档 Plane pl ...

  9. VsCode+Node的前端环境搭建及其理解并创建一个前端目录

    既然选择了远方,便只顾风雨兼程 __ HANS许 系列:零基础搭建前后端分离项目 系列:零基础搭建前后端分离项目 VsCode VsCode的安装 VSCode中文设置 VsCode 扩展 VsCod ...

  10. SQL Server 一列或多列重复数据的查询,删除(转载)

    转载来源:https://www.cnblogs.com/sunxi/p/4572332.html 业务需求 最近给公司做一个小工具,把某个数据库(数据源)的数据导进另一个数据(目标数据库).要求导入 ...