제출 #1339834

#제출 시각아이디문제언어결과실행 시간메모리
1339834tudor_costinSuperpiece (EGOI22_superpiece)C++20
52 / 100
1 ms344 KiB
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int dx[]={1,1,-1,-1,2,2,-2,-2};
const int dy[]={2,-2,2,-2,1,-1,1,-1};
int distN(int a,int b,int c,int d)
{
    int p=abs(a-c),q=abs(b-d);
    int paritate=(p+q)%2;
    int l=0,r=1e9;
    int ans=r;
    while(l<=r)
    {
        int mid=(l+r)/2;
        int B=2*mid+paritate;
        pair<int,int> intervy={0,min(B,2*B-p)};
        pair<int,int> intervx={0,min(B,2*B-q)};
        if(intervy.first%2!=p%2) intervy.first++;
        if(intervy.second%2!=p%2) intervy.second--;
        if(intervx.first%2!=q%2) intervx.first++;
        if(intervx.second%2!=q%2) intervx.second--;
        bool ok=1;
        if(intervy.first>intervy.second) ok=0;
        if(intervx.first>intervx.second) ok=0;
        if(!ok)
        {
            l=mid+1;
            continue;
        }
        ///check possible cu x=0
        if(intervx.first==0)
        {
            if(p<=B && p%2==B%2 && q<=2*B && q%4==((2*B)%4))
            {
                ans=B;
                r=mid-1;
                continue;
            }
        }
        ///check possible cu y=0
        if(intervy.first==0)
        {
            if(q<=B && q%2==B%2 && p<=2*B && p%4==((2*B)%4))
            {
                ans=B;
                r=mid-1;
                continue;
            }
        }
        ///nu luam x=0 sau y=0
        if(intervx.first==0) intervx.first=2;
        if(intervy.first==0) intervy.first=2;
        if(intervy.first>intervy.second) ok=0;
        if(intervx.first>intervx.second) ok=0;
        int low=intervy.first+intervx.first,high=intervx.second+intervy.second;
        if(B<low || B>high) ok=0;
        if(ok)
        {
            ans=B;
            r=mid-1;
        }
        else l=mid+1;
    }
    return ans;
}
bool samediag(int a,int b,int c,int d)
{
    return((a+b)==(c+d) || (a-b)==(c-d));
}
bool samerowcol(int a,int b,int c,int d)
{
    return (a==c || b==d);
}
bool cangetN(int a,int b,int c,int d,int moves)
{
    queue<pair<pair<int,int>,int>> q;
    q.push({{a,b},0});
    while(!q.empty())
    {
        auto[pi,dist]=q.front();
        q.pop();
        if(pi.first==c && pi.second==d)
        {
            return true;
        }
        if(dist<moves)
        {
            for(int k=0;k<8;k++)
            {
                pair<int,int> nwpi=pi;
                nwpi.first+=dx[k];
                nwpi.second+=dy[k];
                q.push({nwpi,dist+1});
            }
        }
    }
    return false;
}
void solve()
{
    string s;
    cin>>s;
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    if(s[0]=='Q')
    {
        if(samediag(a,b,c,d) || samerowcol(a,b,c,d)) cout<<1<<'\n';
        else
        {
            bool hasN=0;
            for(auto ch:s) if(ch=='N') hasN=1;
            if(hasN && cangetN(a,b,c,d,1)) cout<<1<<'\n';
            else cout<<2<<'\n';
        }
    }
    else cout<<distN(a,b,c,d)<<'\n';
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...