WA #2 玄关
查看原帖
WA #2 玄关
945742
hexuchen楼主2024/6/17 13:42
#include <bits/stdc++.h>
using namespace std;
int q;
bool isgameover=false,issuccess=true;
char whochess='r';
string who,killed,checkmate,gameover="no";
string chess[10][9]=
{
	"ra","rh","re","rg","rc","rg","re","rh","ra",
	"nr","nr","nr","nr","nr","nr","nr","nr","nr",
	"rd","nr","nr","nr","nr","nr","nr","nr","rd",
	"rs","nr","rs","nr","rs","nr","rs","nr","rs",
	"nr","nr","nr","nr","nr","nr","nr","nr","nr",
	"nr","nr","nr","nr","nr","nr","nr","nr","nr",
	"bs","nr","bs","nr","bs","nr","bs","nr","bs",
	"bd","nr","nr","nr","nr","nr","nr","nr","bd",
	"nr","nr","nr","nr","nr","nr","nr","nr","nr",
	"ba","bh","be","bg","bc","bg","be","bh","ba"
};
void check(int x,int y,int h,int l,bool flag);
void move_chess(int fromx,int fromy,int endx,int endy);
string change(string x);
void ifcheckmate();
int main(){
	cin>>q;
	for(int i=1;i<=q;i++){
        int fx,fy,sx,sy;
        issuccess=true;
        cin>>fx>>fy>>sx>>sy;
        check(fx,fy,sx,sy,false);
        if(!issuccess){
            cout<<"Invalid command\n";
        }
        else{
            move_chess(fx,fy,sx,sy);
            ifcheckmate();
            whochess=(whochess=='r'?'b':'r');
            cout<<who<<';'<<killed<<';'<<checkmate<<';'<<gameover<<'\n';
        }
    }
	return 0;
}
string change(string x){
    string ans="";
    if(x[0]=='r'){
        ans+="red ";
    }
    else if(x[0]=='b'){
        ans+="blue ";
    }

    switch(x[1]){
        case 'c':ans+="captain";break;
        case 'g':ans+="guard";break;
        case 'e':ans+="elephant";break;
        case 'h':ans+="horse";break;
        case 'a':ans+="car";break;
        case 'd':ans+="duck";break;
        case 's':ans+="soldier";break;
    }
    return ans;
}
void move_chess(int fromx,int fromy,int endx,int endy){
    who=chess[fromx][fromy];
    if(chess[endx][endy]=="nr")
        killed="NA";
    else
        killed=chess[endx][endy];
    chess[fromx][fromy]="nr";
    chess[endx][endy]=who;
    if(killed[1]=='c' && killed.size()==2){
        gameover="yes";
        isgameover=true;
    }
    
    who=change(who);
    killed=(killed=="NA"?"NA":change(killed));
}
void ifcheckmate(){
    checkmate="no";
    if(isgameover)
        return ;
    int whx=-1,why=-1;
    for(int i=0;i<=9;i++){
        for(int j=0;j<=8;j++){
            if(chess[i][j][0]!=whochess && chess[i][j][1]=='c'){
                whx=i;
                why=j;
            }
        }
    }
    int whx2=-1,why2=-1;
    for(int i=0;i<=9;i++){
        for(int j=0;j<=8;j++){
            if(chess[i][j][0]==whochess && chess[i][j][1]=='c'){
                whx2=i;
                why2=j;
            }
        }
    }
    //cout<<whx2<<' '<<why2<<' '<<whochess<<endl; //whochess的一方出棋
    for(int ii=0;ii<=9;ii++){
        for(int jj=0;jj<=8;jj++){
            issuccess=true;
            if(chess[ii][jj][0]==whochess){
                check(ii,jj,whx,why,true);
                if(issuccess){
                    checkmate="yes";
                    return ;
                }
            }
            else if(chess[ii][jj]!="nr"){
                //cout<<chess[ii][jj]<<endl;
                issuccess=true;
                check(ii,jj,whx2,why2,true);
                if(issuccess){
                    checkmate="yes";
                    return ;
                }
            }
        }
    }
    //checkmate="no";
    /*for(int i=0;i<=9;i++){
        for(int j=0;j<=8;j++){
            cout<<chess[i][j]<<' ';
        }
        cout<<endl;
    }
    issuccess=true;
    check(6,6,9,4,true);
    cout<<issuccess<<endl;*/
}
/*
6
0 0 1 0
9 4 8 4
1 0 1 4
8 4 9 4
3 4 2 3
6 4 7 3
*/
void check(int x,int y,int h,int l,bool flag){
    if(!flag){
        if(x<0 || x>9 || y<0 || y>8 || h<0 || h>9 || l<0 || l>8){
            issuccess=false;
            return ;
        }
        if(chess[x][y]=="nr"){
            issuccess=false;
            return ;
        }
        if(chess[x][y][0]!=whochess || chess[h][l][0]==whochess){
            issuccess=false;
            return ;
        }
        if(isgameover){
            issuccess=false;
            return ;
        }
    }
    //cout<<"fuck you mother";
    char player=chess[x][y][1];
    if(player=='c'){ //王
        int dh[5]={-1,-1,0,1,0};
        int dl[5]={-1,0,1,0,-1};
        int nx=-1,ny=-1;
        bool flag=true;
        for(int i=1;i<=4;i++){
            nx=x+dh[i],ny=y+dl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8){
                if(nx==h && ny==l){ //符合条件
                    flag=false;
                    break;
                }
            }
        }
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
            
        }
        if(chess[nx][ny][0]==whochess){
            if(!flag){
                issuccess=false;
                return ;
            }
        }
        //move_chess(x,y,h,l);
    }
    else if(player=='g'){ //士
        int dh[5]={-1,-1,-1,1,1};
        int dl[5]={-1,-1,1,-1,1};
        int nx=-1,ny=-1;
        bool flag=true;
        for(int i=1;i<=4;i++){
            nx=x+dh[i],ny=y+dl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8){
                if(nx==h && ny==l){ //符合条件
                    flag=false;
                    break;
                }
            }
        }
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
            
        }
        if(chess[nx][ny][0]==whochess){
            if(!flag){
                issuccess=false;
                return ;
            }
        }
        //move_chess(x,y,h,l);
    }
    else if(player=='e'){ //象
        int dh[5]={-1,2,2,-2,-2};
        int dl[5]={-1,-2,2,-2,2};
        int ph[5]={-1,1,1,-1,-1};
        int pl[5]={-1,-1,1,-1,1};
        int nx=-1,ny=-1,px=-1,py=-1;
        bool flag=true;
        for(int i=1;i<=4;i++){
            nx=x+dh[i],ny=y+dl[i],px=x+ph[i],py=y+pl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8 && px>=0 && px<=9 && py>=0 && py<=8){
                if(chess[px][py]=="nr"){
                    //cout<<chess[px][py]<<' '<<px<<' '<<py<<endl;
                    if(nx==h && ny==l){ //符合条件
                        flag=false;
                        break;
                    }
                }
            }
        }
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
            
        }
        if(chess[nx][ny][0]==whochess){
            if(!flag){
                issuccess=false;
                return ;
            }
        }
        //move_chess(x,y,h,l);
    }
    else if(player=='h'){ //马
        int dh[9]={0,1,2,2,1,-1,-2,-2,-1};
        int dl[9]={0,-2,-1,1,2,2,1,-1,-2};
        int nx=-1,ny=-1,px=-1,py=-1;
        bool flag=true;
        for(int i=1;i<=8;i++){
            nx=x+dh[i],ny=y+dl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8){
                switch(i){
                    case 1:px=x;py=y-1;break;
                    case 2:px=x+1;py=y;break;

                    case 3:px=x+1;py=y;break;
                    case 4:px=x;py=y+1;break;

                    case 5:px=x;py=y+1;break;
                    case 6:px=x-1;py=y;break;
                    
                    case 7:px=x-1;py=y;break;
                    case 8:px=x;py=y-1;break;
                    /*
                    px=x-1;py=y;break;px=x;py=y+1;break;px=x+1;py=y;break;px=x;py=y-1;break;
                    */
                }//cout<<i<<' '<<px<<' '<<py<<endl;
                if(px>=0 && px<=9 && py>=0 && py<=8){
                    
                    if(nx==h && ny==l && chess[px][py]=="nr"){ //符合条件
                        flag=false;
                        break;
                    }
                }
                
            }
        }
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
            
        }
        if(chess[nx][ny][0]==whochess){
            if(!flag){
                issuccess=false;
                return ;
            }
        }
        //move_chess(x,y,h,l);
    }
    else if(player=='a'){ //车
        //cout<<x<<' '<<y<<endl;
        if(x!=h && y!=l){
            issuccess=false;
            return ;
        }
        //cout<<"Wrong?Mobving\n";
        int from=(x==h?y:x),end=(x==h?l:h);
        //cout<<from<<' '<<end<<endl;
        if(from>=end){
            swap(from,end);
        }
        for(int i=from+1;i<end;i++){
            if(x==h){
                if(chess[x][i]!="nr"){
                    issuccess=false;
                    return ;
                }
            }
            else{
                if(chess[i][y]!="nr"){
                    issuccess=false;
                    return ;
                }
            }
        }//cout<<"wtf\n";
        //move_chess(x,y,h,l);
    }
    else if(player=='d'){ //鸭
        //cout<<"tmd";
        int dh[9]={-1,2,3,3,2,-2,-3,-3,-2};
        int dl[9]={-1,-3,-2,2,3,3,2,-2,-3};
        int yh1[5]={-1,1,0,-1,0};
        int yl1[5]={-1,0,1,0,-1};
        int yh2[9]={0,1,2,2,1,-1,-2,-2,-1};
        int yl2[9]={0,-2,-1,1,2,2,1,-1,-2};
        int nx=-1,ny=-1,yx1=-1,yy1=-1,yx2=-1,yy2=-1;
        bool flag=true;
        for(int i=1;i<=8;i++){
            int a;
            switch(i){
                case 1:a=4;break;
                case 2:a=1;break;
                case 3:a=1;break;
                case 4:a=2;break;
                case 5:a=2;break;
                case 6:a=3;break;
                case 7:a=3;break;
                case 8:a=4;break;
            }
            yx1=x+yh1[a],yy1=y+yl1[a];
            yx2=x+yh2[i],yy2=y+yl2[i];
            nx=x+dh[i],ny=y+dl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8 && yx1>=0 && yx1<=9 && yy1>=0 && yy1<=8 && yx2>=0 && yx2<=9 && yy2>=0 && yy2<=8){
                if(nx==h && ny==l && chess[yx1][yy1]=="nr" && chess[yx2][yy2]=="nr"){ //符合条件
                    //cout<<i<<' '<<yx1<<' '<<yy1<<endl;
                    flag=false;
                    break;
                }
            }
        }
        //cout<<"fuck!why?????";
        //cout<<nx<<' '<<ny<<endl;
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
        }
        //cout<<"shift";
        //cout<<"found you!";
        if(chess[nx][ny][0]==whochess){
            //cout<<"ohhhh";
            if(flag){
                issuccess=false;
                return ;
            }
        }
        //cout<<"so?you will...";
        //move_chess(x,y,h,l);
    }
    else if(player=='s'){ //兵
        int dh[9]={-1,-1,-1,-1,0,0,1,1,1};
        int dl[9]={-1,-1,0,1,-1,1,-1,0,1};
        int nx=-1,ny=-1;
        bool flag=true;
        for(int i=1;i<=8;i++){
            nx=x+dh[i],ny=y+dl[i];
            if(nx>=0 && nx<=9 && ny>=0 && ny<=8){
                if(nx==h && ny==l){ //符合条件
                    flag=false;
                    break;
                }
            }
        }
        if(flag || nx!=h || ny!=l){
            issuccess=false;
            return ;
            
        }
        if(chess[nx][ny][0]==whochess){
            if(!flag){
                issuccess=false;
                return ;
            }
        }
        //move_chess(x,y,h,l);
    }
}
/*
10001
00000
00000
00000
10001
*/

不要直接给代码

2024/6/17 13:42
加载中...