首页 > 其他 > 详细

Fire Again CodeForces - 35C (BFS)

时间:2019-04-21 19:37:02      阅读:156      评论:0      收藏:0      [点我收藏+]

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=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,m;
int vis[2020][2020];
struct node
{
    int x;
    int y;
    int t;
    node(){}
    node(int xx,int yy,int tt)
    {
        x=xx;
        y=yy;
        t=tt;
    }
};
int xx[]={-1,1,0,0};
int yy[]={0,0,-1,1};
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=0;
    int ansx,ansy;
    int mnum=1;
    repd(i,1,k)
    {
        cin>>x>>y;
        vis[x][y]=1;
        ansx=x;
        ansy=y;
        q.push(node(x,y,1));
    }
    cnt=k;
    node temp;
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        repd(i,0,3)
        {
            x=temp.x+xx[i];
            y=temp.y+yy[i];
            if(x>=1&&x<=n&&y>=1&&y<=m&&vis[x][y]==0)
            {
                cnt++;
                if(temp.t+1>mnum)
                {
                    mnum=temp.t+1;
                    ansx=x;
                    ansy=y;
                }
                vis[x][y]=1;
                q.push(node(x,y,temp.t+1));
            }
            if(cnt==n*m)// 矩阵中的全部的树都着火过了。优化时间。
            {
                break;
            }
        }
    }
    
    cout<<ansx<<" "<<ansy<<endl;
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch ==   || ch == \n);
    if (ch == -) {
        *p = -(getchar() - 0);
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 - ch + 0;
        }
    }
    else {
        *p = ch - 0;
        while ((ch = getchar()) >= 0 && ch <= 9) {
            *p = *p * 10 + ch - 0;
        }
    }
}

 

 

Fire Again CodeForces - 35C (BFS)

原文:https://www.cnblogs.com/qieqiemin/p/10746376.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!