QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#642505 | #4638. Xiangqi | cjx | TL | 1ms | 3536kb | C++20 | 4.6kb | 2024-10-15 14:38:39 | 2024-10-15 14:38:39 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define d(x,y) x*(9)+y+1
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
}
void write(ll x){
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
//const int N=;
int T;
ll x,y,x_,y_;
int f[20][20],g[20][20],v[20][20];
int fx[8]={1,1,-1,-1,2,2,-2,-2};
int fy[8]={2,-2,2,-2,1,-1,1,-1};
ll solve1(){
queue<pii >q;
while(q.size())q.pop();
memset(f,0x3f,sizeof(f));
memset(v,0,sizeof(v));
int o=9;
f[x+o][y+o]=0;v[x+o][y+o]=1;
q.push(pii(x,y));
while(q.size()){
pii u=q.front();q.pop();
for(int i=0;i<8;i++){
int xx=fx[i]+u.fi;
int yy=fy[i]+u.se;
if(xx==x_&&yy==y_)continue;
if(i<4){
if(u.fi==x_&&u.se+fy[i]/abs(fy[i])==y_)continue;
}
else{
if(u.se==y_&&u.fi+fx[i]/abs(fx[i])==x_)continue;
}
if(xx<-o||xx>o||yy<-o||yy>o)continue;
if(v[xx+o][yy+o])continue;
v[xx+o][yy+o]=1;
f[xx+o][yy+o]=f[u.fi+o][u.se+o]+1;
q.push(pii(xx,yy));
}
}
memset(g,0x3f,sizeof(g));
memset(v,0,sizeof(v));
g[x+o][y+o]=0;v[x+o][y+o]=1;
q.push(pii(x,y));
while(q.size()){
pii u=q.front();q.pop();
for(int i=0;i<8;i++){
int xx=fx[i]+u.fi;
int yy=fy[i]+u.se;
if(xx<-o||xx>o||yy<-o||yy>o)continue;
if(v[xx+o][yy+o])continue;
v[xx+o][yy+o]=1;
g[xx+o][yy+o]=g[u.fi+o][u.se+o]+1;
q.push(pii(xx,yy));
}
}
return min(f[o][o],g[o][o]+1);
}
ll jump(ll ma,ll pa){
if(pa>0)return 2;
if(ma-pa>3)return 4;
if(ma-pa==3||ma-pa==2){
if(ma-4==0)return 2;
if(ma-4<0)return 4;
if(ma-4>0)return 3;
}
if(ma-pa==1){
if(ma-2==0)return 2;
if(ma-2<0)return 4;
if(ma-2>0)return 3;
}
return 2;
}
ll jump2(ll ma,ll pa){
if(ma-pa>3)return 4;
if(ma-pa==3||ma-pa==2){
if(ma-4==0)return 2;
if(ma-4<0)return 4;
if(ma-4>0)return 3;
}
if(ma-pa==1){
if(ma-2==0)return 2;
if(ma-2<0)return 4;
if(ma-2>0)return 3;
}
return 2;
}
ll work2(ll x,ll y,ll x_,ll y_){
if(x<0)x=-x,x_=-x_;
if(y<0)y=-y,y_=-y_;
if(x==0){
if(y_>y){
if(x_==0)return 1;
return 2;
}
if(x_==0){
return jump2(y,y_);
}
return 3;
}
if(y==0){
if(x_>x){
if(y_==0)return 1;
return 2;
}
if(y_==0)
return jump2(x,x_);
return 3;
}
return 5e9;
}
ll work(ll x,ll y,ll x_,ll y_){
if(x<0)x=-x,x_=-x_;
if(y<0)y=-y,y_=-y_;
if(x==0){
if(y_>y){
if(x_==0)return 1;
return 2;
}
if(x_==0){
return jump(y,y_);
}
return 3;
}
if(y==0){
if(x_>x){
if(y_==0)return 1;
return 2;
}
if(y_==0)
return jump(x,x_);
return 3;
}
return 5e9;
}
ll solve2(){//y +
ll d=(x+1)/2;
if(d==1){
if(x==1)return min(work(0,y-2,x_,y_),work(0,y+2,x_,y_))+d;
if(x==2)return min(work(0,y-1,x_,y_),work(0,y+1,x_,y_))+d;
}
ll yy=y-d-(x&1);
yy=max(1ll,yy);
ll ans=work(0,yy,x_,y_)+d;
yy=max(1ll,yy-3);
ans=min(ans,work(0,yy,x_,y_)+d+1);
//printf("solve%d = %lld\n",2,ans);
return ans;
}
ll solve3(){//y -
ll d=(x+1)/2;
if(d==1){
if(x==1)return min(work(0,y-2,x_,y_),work(0,y+2,x_,y_))+d;
if(x==2)return min(work(0,y-1,x_,y_),work(0,y+1,x_,y_))+d;
}
ll yy=y-d-(x&1);
if(yy>=0)return 5e9;
ll ans=work(0,-1,x_,y_)+d;
//printf("solve%d = %lld\n",3,ans);
return ans;
}
ll solve4(){//x +
ll d=(y+1)/2;
if(d==1){
if(y==1)return min(work(x-2,0,x_,y_),work(x+2,0,x_,y_))+d;
if(y==2)return min(work(x-1,0,x_,y_),work(x+1,0,x_,y_))+d;
}
ll xx=x-d-(y&1);
xx=max(1ll,xx);
ll ans=work(xx,0,x_,y_)+d;
xx=max(1ll,xx-3);
ans=min(ans,work(xx,0,x_,y_)+d+1);
//printf("solve%d = %lld\n",4,ans);
return ans;
}
ll solve5(){//x -
ll d=(y+1)/2;
if(d==1){
if(y==1)return min(work(x-2,0,x_,y_),work(x+2,0,x_,y_))+d;
if(y==2)return min(work(x-1,0,x_,y_),work(x+1,0,x_,y_))+d;
}
ll xx=x-d-(y&1);
if(xx>=0)return 5e9;
ll ans=work(-1,0,x_,y_)+d;
//printf("solve%d = %lld\n",5,ans);
return ans;
}
int main(){
//freopen("chess.in","r",stdin);
//freopen("chess.out","w",stdout);
T=read();
while(T--){
x=read();y=read();x_=read();y_=read();
if(x<0)x=-x,x_=-x_;
if(y<0)y=-y,y_=-y_;
if(x==0)x_=abs(x_);
if(y==0)y_=abs(y_);
ll ans=1e10;
if(x<=9&&y<=9)ans=min(ans,solve1());
if(x==0||y==0)ans=min(ans,work2(x,y,x_,y_));
if(x)ans=min(ans,min(solve2(),solve3()));
if(y)ans=min(ans,min(solve4(),solve5()));
write(ans);puts("");
}
return 0;
}
/*
5
-2 1 5 5
5 0 6 0
2 1 1 1
100 2 -1 0
4 2 1 1
1
8 -7 68 -41
5
1
82 9 65 0
7
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3536kb
input:
5 -2 1 5 5 5 0 6 0 2 1 1 1 100 2 -1 0 4 2 1 1
output:
1 1 2 5 3
result:
ok 5 lines
Test #2:
score: -100
Time Limit Exceeded
input:
388752 -12 -12 -12 -11 -12 -12 -12 -10 -12 -12 -12 -9 -12 -12 -12 -8 -12 -12 -12 -7 -12 -12 -12 -6 -12 -12 -12 -5 -12 -12 -12 -4 -12 -12 -12 -3 -12 -12 -12 -2 -12 -12 -12 -1 -12 -12 -12 0 -12 -12 -12 1 -12 -12 -12 2 -12 -12 -12 3 -12 -12 -12 4 -12 -12 -12 5 -12 -12 -12 6 -12 -12 -12 7 -12 -12 -12 8 ...
output:
8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 7 8 8 8 8 8 8 8 8 8 8 8 8 8 ...