其他分享
首页 > 其他分享> > Codeforces Round #798 (Div. 2) D

Codeforces Round #798 (Div. 2) D

作者:互联网

D. Lena and Matrix

首先我们能想到的就是暴力枚举 但是肯定是不行的
我们可以减少枚举个数 那么哪些是可以砍掉的呢 首先在黑色圈内的黑色块 肯定是可以被砍掉的
我们考虑外圈的 好像还是很多 那我们考虑四个角的
我们可以考虑到切比雪夫距离好像维护的就是四个角(左上左下右上右下)
原式就被转化为了 max(max(|a-xi|,|b-yi|))我们显然要维护的距离就是xi yi的最值即可

#include <bits/stdc++.h>
using namespace std;
const int N = 3e5+10;
const int M = 998244353;
const int mod = 1000000007;
#define int long long
#define endl '\n'
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define inf 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);

void solve() {
    int n,m;cin>>n>>m;
    int mx[5];
    memset(mx,-127,sizeof mx);
    char g[n+1][m+1];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>g[i][j];
            if(g[i][j]=='B'){
                mx[1] = max(mx[1], i + j);
                mx[2] = max(mx[2], i - j);
                mx[3] = max(mx[3], -i + j);
                mx[4] = max(mx[4], -i - j);
            }
        }
    }
    int ans=inf,x,y;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            int res=max({mx[1]-i-j,mx[2]-i+j,mx[3]+i-j,mx[4]+i+j});
            if(res<ans){
                ans=res,x=i,y=j;
            }
        }
    }
    cout<<x<<' '<<y<<endl;
}
signed main(){
    fast
    int T;cin>>T;
    while(T--) {
        solve();
    }
    return ~~(0^_^0);
}

标签:xi,const,int,max,Codeforces,798,Div,mx,define
来源: https://www.cnblogs.com/ycllz/p/16684805.html