rt,扒拉了官方数据出来之后发现是判断将军的地方错了好几个,求一下将军判断的样例
(讨论区之前的帖子的试过了)
顺带求调一下
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
map<int,string>mp;
int a[50][50];//棋盘 0为空 1-7是红方 8-14是蓝方
bool ckck(){
//红被蓝将
int xx,yy;
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(a[i][j]==1){
xx=i,yy=j;
break;
}
}
}
//对面车
for(int i=yy-1;i>0;i--){
if(a[xx][i]==12)return 1;
if(a[xx][i]!=0)break;
}
for(int i=yy+1;i<=10;i++){
if(a[xx][i]==12)return 1;
if(a[xx][i]!=0)break;
}
for(int i=xx-1;i>0;i--){
if(a[i][yy]==12)return 1;
if(a[i][yy]!=0)break;
}
for(int i=xx+1;i<=10;i++){
if(a[i][yy]==12)return 1;
if(a[i][yy]!=0)break;
}
//对面王
if(a[xx+1][yy]==8 || a[xx-1][yy]==8 || a[xx][yy+1]==8 || a[xx][yy-1]==8)return 1;
//对面士
if(a[xx+1][yy+1]==9 || a[xx+1][yy-1]==9 || a[xx-1][yy+1]==9 || a[xx-1][yy-1]==9)return 1;
//对面象
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+2*sx>=1 && yy+2*sy>=1){
if(!a[xx+sx][yy+sy] && a[xx+2*sx][yy+2*sy]==10)return 1;
}
}
}
//对面马
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+2*sx>=1 && yy+sy>=1){
if(!a[xx+sx][yy] && a[xx+2*sx][yy+sy]==11)return 1;
}
if(xx+sx>=1 && yy+2*sy>=1){
if(!a[xx][yy+sy] && a[xx+sx][yy+sy*2]==11)return 1;
}
}
}
//对面鸭
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+3*sx>=1 && yy+2*sy>=1){
if(!a[xx+2*sx][yy+sy] && a[xx+3*sx][yy+2*sy]==13)return 1;
}
if(xx+2*sx>=1 && yy+sy*3>=1){
if(!a[xx+sx][yy+2*sy] && a[xx+2*sx][yy+sy*3]==13)return 1;
}
}
}
//对面兵
for(int sx=-1;sx<=1;sx++){
for(int sy=-1;sy<=1;sy++){
if(a[xx+sx][yy+sy]==14)return 1;
}
}
//蓝被红将
for(int i=1;i<=10;i++){
for(int j=1;j<=9;j++){
if(a[i][j]==8){
xx=i,yy=j;
break;
}
}
}
//对面车
for(int i=yy-1;i>=1;i--){
if(a[xx][i]==5)return 1;
if(a[xx][i]!=0)break;
}
for(int i=yy+1;i<=9;i++){
if(a[xx][i]==5)return 1;
if(a[xx][i]!=0)break;
}
for(int i=xx-1;i>0;i--){
if(a[i][yy]==5)return 1;
if(a[i][yy]!=0)break;
}
for(int i=xx+1;i<=10;i++){
if(a[i][yy]==5)return 1;
if(a[i][yy]!=0)break;
}
//对面王
if(a[xx+1][yy]==1 || a[xx-1][yy]==1 || a[xx][yy+1]==1 || a[xx][yy-1]==1)return 1;
//对面士
if(a[xx+1][yy+1]==2 || a[xx+1][yy-1]==2 || a[xx-1][yy+1]==2 || a[xx-1][yy-1]==2)return 1;
//对面象
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+2*sx>=1 && yy+2*sy>=1){
if(!a[xx+sx][yy+sy] && a[xx+2*sx][yy+2*sy]==3)return 1;
}
}
}
//对面马
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+2*sx>=1){
if(!a[xx+sx][yy] && a[xx+2*sx][yy+sy]==4)return 1;
}
if(yy+sy*2>=1){
if(!a[xx][yy+sy] && a[xx+sx][yy+sy*2]==4)return 1;
}
}
}
//对面鸭
for(int sx=-1;sx<=1;sx+=2){
for(int sy=-1;sy<=1;sy+=2){
if(xx+3*sx>=1 && yy+2*sy>=1){
if(!a[xx+2*sx][yy+sy] && a[xx+3*sx][yy+2*sy]==6)return 1;
}
if(xx+2*sx>=1 && yy+sy*3>=1){
if(!a[xx+sx][yy+2*sy] && a[xx+2*sx][yy+sy*3]==6)return 1;
}
}
}
//对面兵
for(int sx=-1;sx<=1;sx++){
for(int sy=-1;sy<=1;sy++){
if(a[xx+sx][yy+sy]==7)return 1;
}
}
return 0;
};
void sol(){
//棋子对应名字
mp[1]="captain";mp[2]="guard";mp[3]="elephant";mp[4]="horse";
mp[5]="car";mp[6]="duck";mp[7]="soldier";
//初始化棋盘
for(int i=1;i<=9;i+=2)a[7][i]=14,a[4][i]=7;
a[8][1]=a[8][9]=13;a[3][1]=a[3][9]=6;
for(int i=1;i<=4;i++){
a[10][i]=a[10][10-i]=13-i;
a[1][i]=a[1][10-i]=6-i;
}
a[10][5]=8,a[1][5]=1;
//check
// for(int i=1;i<=10;i++){
// for(int j=1;j<=9;j++){
// cout<<a[i][j]<<" \n"[j==9];
// }
// }
// for(int i=1;i<=7;i++){
// cout<<mp[i]<<endl;
// }
int q,x1,y1,x2,y2,f=1,ed=0;//f=1红色移动 ed=1已经结束
cin>>q;
int cnt=0;
while(q--){
cin>>x1>>y1>>x2>>y2;
x1++,x2++,y1++,y2++;//题目按照左下角为00给出坐标,所以进行转换
// cerr<<++cnt<<endl;
//cerr<<++cnt<<' '<<f<<' '<<x1<<' '<<y1<<' '<<a[x1][y1]<<endl;
if(ed){//游戏已经结束
cout<<"Invalid command"<<endl;
continue;
}
else if(!a[x1][y1]){//没有棋子
cout<<"Invalid command"<<endl;
continue;
}
else if(f && a[x1][y1]>7){//红方但是这里是对面的棋子
cout<<"Invalid command"<<endl;
continue;
}
else if(!f && a[x1][y1]>0 && a[x1][y1]<=7){//蓝方但是这里是对面的棋子
cout<<"Invalid command"<<endl;
continue;
}
else if(f && a[x2][y2]>0 && a[x2][y2]<=7){//结束位置有己方棋子
cout<<"Invalid command"<<endl;
continue;
}
else if(!f && a[x2][y2]>7){//结束位置有己方棋子
cout<<"Invalid command"<<endl;
continue;
}
int k=a[x1][y1];
// cerr<<f<<' '<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<' '<<k<<endl;
if(k>=8)k-=7;
if(k==1){//王
if(abs(x2-x1)+abs(y2-y1)!=1){cout<<"Invalid command"<<endl;continue;}
}
else if(k==2){//士
if(abs(x2-x1)!=1 || abs(y2-y1)!=1){cout<<"Invalid command"<<endl;continue;}
}
else if(k==3){//象
if(abs(x2-x1)!=2 || abs(y2-y1)!=2){cout<<"Invalid command"<<endl;continue;}
int sx=(x2-x1)/2,sy=(y2-y1)/2;
if(a[x1+sx][y1+sy]){cout<<"Invalid command"<<endl;continue;}
}
else if(k==4){//马
if(abs(x2-x1)+abs(y2-y1)!=3){cout<<"Invalid command"<<endl;continue;}
else if(abs(x2-x1)==3 || abs(y2-y1)==3){cout<<"Invalid command"<<endl;continue;}
int sx=x2-x1,sy=y2-y1;
if(abs(sx)==2){
if(sx==2)if(a[x1+1][y1]){cout<<"Invalid command"<<endl;continue;}
else if(sx==-1)if(a[x1-1][y1]){cout<<"Invalid command"<<endl;continue;}
}
else{
if(sy==2)if(a[x1][y1+1]){cout<<"Invalid command"<<endl;continue;}
else if(sy==-1)if(a[x1][y1-1]){cout<<"Invalid command"<<endl;continue;}
}
}
else if(k==5){//车
if(x1!=x2 && y1!=y2){cout<<"Invalid command"<<endl;continue;}
if(x1==x2){
int ff=1;
if(y1<y2){
for(int i=y1+1;i<y2;i++){
if(a[x1][i]){
cout<<"Invalid command"<<endl;
ff=0;
break;
}
}
}
else{
for(int i=y1-1;i>y2;i--){
if(a[x1][i]){
cout<<"Invalid command"<<endl;
ff=0;
break;
}
}
}
if(!ff)continue;
}
else{
int ff=1;
if(x1<x2){
for(int i=x1+1;i<x2;i++){
if(a[i][y1]){
cout<<"Invalid command"<<endl;
ff=0;
break;
}
}
}
else{
for(int i=x1-1;i>x2;i--){
if(a[i][y1]){
cout<<"Invalid command"<<endl;
ff=0;
break;
}
}
}
if(!ff)continue;
}
}
else if(k==6){//鸭
if(!((abs(x2-x1)==3 && abs(y2-y1)==2)||(abs(x2-x1)==2 && abs(y2-y1)==3))){
cout<<"Invalid command"<<endl;continue;
}
if(abs(x2-x1)==3){
int sx=(x2-x1)/3,sy=(y2-y1)/2;
if(a[x1+2*sx][y1+sy]){
cout<<"Invalid command"<<endl;continue;
}
else if(a[x1+sx][y1]){
cout<<"Invalid command"<<endl;continue;
}
}
else{
int sx=(x2-x1)/2,sy=(y2-y1)/3;
if(a[x1+sx][y1+2*sy]){cout<<"Invalid command"<<endl;continue;}
else if(a[x1][y1+sy]){cout<<"Invalid command"<<endl;continue;}
}
}
else{//兵
if(abs(x2-x1)>1 || abs(y2-y1)>1){cout<<"Invalid command"<<endl;continue;}
}
// cerr<<q<<' '<<x1<<' '<<y1<<' '<<x2<<' '<<y2<<' '<<k<<endl;
//移动的棋子
if(f)cout<<"red ";
else cout<<"blue ";
cout<<mp[k]<<';';
//被移除的棋子
if(a[x2][y2]){
if(f)cout<<"blue ";
else cout<<"red ";
int pp=a[x2][y2];
if(pp>=8)pp-=7;
cout<<mp[pp]<<';';
}
else{
cout<<"NA;";
}
//移走
if(a[x2][y2]==1 || a[x2][y2]==8)ed=1;
a[x2][y2]=a[x1][y1];
a[x1][y1]=0;
//交替红蓝方
f=!f;
//将军
if(ckck() && !ed)cout<<"yes;";
else cout<<"no;";
//游戏结束
if(ed)cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
sol();
}