1. 程式人生 > >Fire Again CodeForces - 35C (BFS)

Fire Again CodeForces - 35C (BFS)

it is getchar() truct char ios cat rst code coord

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)