【P5380】鸭棋

· · 个人记录

[THUPC2019]鸭棋 Soltion

这是一道比较基础的模拟题,比较考验代码能力和工程构建能力。鉴于大部分读者是基本没接触过工程编写的 OI 选手,所以我将从最基础的部分讲起,让读者在掌握此题解法的同时,也可以自己尝试编写一些简单的工程代码来解决其他题目。

首先我们看到,这道题中涉及了 7 种性质各有异同的棋子,它们有相同的性质(都能移动、被吃、将对方的军等等),也有不同的特性(移动规则不同等等)。自然你可以使用大量的 if 语句来为不同的棋子进行不同的操作,但是我们 C++ 选手还有一种更优美、更简易的方式来表现这个"异同"——继承类和虚函数。

由于我们还是 OIer,所以无需掌握过多的关于类的知识,所以这里只讲我们用得到的部分。

众所周知,一个较为完整的类大概长这样:

class NewClass{ //NewClass 是类名
public: //表示任何对象都可以使用下面的部分(类似的,还有 private 和 protected,但是我们这道题不使用)
    NewClass(int _myValue=0){ //构造函数——在你创造这个类的时候会调用
        myValue=_myValue;
    } 
    int myValue; //若干个成员变量
    void print(){ //若干个成员函数
        cout<<myValue<<endl;
    }
    ~NewClass(){} //析构函数——在你销毁这个类的时候会调用。可省略
};

在这个类里,我们可以定义一个虚函数:

class NewClass{
    //...省略了一些部分
    virtual void specialPrint(){
        cout<<myValue*2<<endl;
    }
    //...省略了一些部分
};

如果你不再写别的东西的话,specialPrint() 函数和一般的函数没有任何不同。

但现在我们再写一个继承于 NewClass 的类:

class NewClass2:public NewClass{ //表示继承了 NewClass 的所有 public 成员
public:
    NewClass2(int _myValue=0){
        myValue=_myValue; //NewClass 的所有成员变量都可以在这里使用
    }
    void specialPrint(){
        cout<<myValue*10<<endl;
    }
    ~NewClass2(){}
};

现在我们在主程序里干些事情:

int main(){
    NewClass* myClass=new NewClass(7); //定义一个 NewClass 指针。由于构造函数的存在,现在 myClass 中 myValue 为 7
    NewClass* myClass2=new NewClass2(9); //定义一个 NewClass2 指针。现在 myClass2 中 myValue 为 9
    myClass->print(); //输出 7
    myClass2->print(); //输出 9
    myClass->specialPrint(); //输出 14
    myClass2->specialPrint(); //输出 90
}

要注意的是,指针型变量需要用 -> 调用成员变量/函数。

是不是已经很明显了?由于作为 NewClass2 的 myClass2 继承于 NewClass,所以它的 print() 等价于 NewClass 的 print()。但由于 specialPrint() 是虚函数,在作为父类 NewClass 的 myClass 调用 specialPrint() 时,会使用 cout<<myValue*2<<endl 这个父类中定义的 specialPrint()。作为子类 NewClass2 的 myClass2 调用 specialPrint() 时,会首先看看子类中有没有新定义 specialPrint(),如果找到了就运行这个 specialPrint(),即 cout<<myValue*10<<endl;否则就运行父类中的 specialPrint()。

要注意的是,虚函数一般只适用于指针型变量。如果对上述这些还不能很好的理解,可以看看后面的题解部分。

好了,现在正式开始本题的题解。

首先我们定义一个叫做 Board 的类。

class Board{
public:
    Board(){}
    Chess* chess[10][9]; //Chess 类在后面。用指针的形式方便使用虚函数
    void init(); //初始化
    void run(int turn); //运行游戏
    void print(int turn); //调试输出。可忽略
    ~Board(){}
};

然后我们定义一个叫做 Chess 的父类。

class Chess{
public:
    int id,camp; //id 表示棋子的种类,camp 表示棋子的阵营
    string name; //name 表示棋子的名字
    int x,y; //x,y 表示棋子的坐标
    Chess(){}
    void init(int _id=0,int _camp=0,string _name="",int _x=0,int _y=0){
        id=_id;camp=_camp;name=_name;x=_x;y=_y;
    }
    virtual bool canGo(int tx,int ty,Board &nowBoard){return 0;} //虚函数,判断当前棋子是否能够从 (x,y) 走到 (tx,ty)。由于该父类不表示任何棋子,所以直接返回 0。由于我们需要获取棋盘中其它棋子的状态,所以要把当前的整个棋盘作为参数传入函数中
    virtual bool canKill(Board &nowBoard){return 0;} //虚函数,判断当前棋子是否将对面的军
    ~Chess(){} 
};

这时问题出现了,Board 调用了 Chess 类,Chess 又调用了 Board 类,这怎么办呢?

由于这是一道 OI 题,必须在一个程序内完成。所以我们必须使用下面这种类似"千层饼"的形式:

class Chess;
class Board{
    //...
};
class Chess{
    //...
};
void Board::init(){
    //...
}
void Board::run(int turn){
    //...
}
void Board::print(){
    //...
}

在实现中,显然 Chess 的任何成分只会调用 Board 中的其它 Chess,不会调用 init(),run(),print(),而这些函数会调用 Chess 中的很多内容,所以我们把 init(),run(),print() 的实现部分写在 Chess 下面,将剩下的 Board 主体(定义部分)写在 Chess 上面。Board 的主体部分只是定义了 chess 数组,所以只要在最上面提一句 class Chess; 即可。

现在我们具体实现每一个棋子,它们作为 Chess 的子类,都将继承 Chess 的多数成员,并特殊化地实现虚函数的内容。这些内容都应该放在 Board 的实现部分之上,父类 Chess 之下。

具体的实现完全按照题意模拟,没有什么好讲的。其中的 xRange() 和 yRange() 是判断横纵坐标是否在合法的范围内。

class Empty:public Chess{ //空格子
public:
    Empty(int _camp=0,int _x=0,int _y=0){
        init(-1,_camp,"empty",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){return 0;}
    bool canKill(Board &nowBoard){return 0;}
    ~Empty(){}
};
class Car:public Chess{ //车
public:
    Car(int _camp=0,int _x=0,int _y=0){
        init(1,_camp,"car",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){ 
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (x!=tx && y==ty){
            for (int i=min(x,tx)+1;i<=max(x,tx)-1;++i){
                if (nowBoard.chess[i][y]->id!=-1) return false;
            }
        }else if (x==tx && y!=ty){
            for (int i=min(y,ty)+1;i<=max(y,ty)-1;++i){
                if (nowBoard.chess[x][i]->id!=-1) return false;
            }           
        }else return false;
        return true;
    } 
    bool canKill(Board &nowBoard){
        int tx,ty; 
        tx=x+1;ty=y;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx++;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x-1;ty=y;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx--;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x;ty=y+1;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty++;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x;ty=y-1;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty--;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        return false;       
    }
    ~Car(){}
};
class Horse:public Chess{ //马
public:
    Horse(int _camp=0,int _x=0,int _y=0){
        init(2,_camp,"horse",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*2 && ty==y+sy && nowBoard.chess[x+sx][y]->id==-1) return true;
                if (tx==x+sx && ty==y+sy*2 && nowBoard.chess[x][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*2) && yRange(y+sy) && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*2][y+sy]->id==0 && nowBoard.chess[x+sx*2][y+sy]->camp!=camp) return true;
                if (xRange(x+sx) && yRange(y+sy*2) && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx][y+sy*2]->id==0 && nowBoard.chess[x+sx][y+sy*2]->camp!=camp) return true;
            }
        }
        return false;
    }
    ~Horse(){}
}; 
class Elephant:public Chess{ //象
public:
    Elephant(int _camp=0,int _x=0,int _y=0){
        init(3,_camp,"elephant",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*2 && ty==y+sy*2 && nowBoard.chess[x+sx][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*2) && yRange(y+sy*2) && nowBoard.chess[x+sx][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*2]->id==0 && nowBoard.chess[x+sx*2][y+sy*2]->camp!=camp) return true;
            }
        }
        return false;
    } 
    ~Elephant(){}
}; 
class Guard:public Chess{ //士
public:
    Guard(int _camp=0,int _x=0,int _y=0){
        init(4,_camp,"guard",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(tx-x)==1 && abs(ty-y)==1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)==1 && abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Guard(){}
};
class Captain:public Chess{ //王
public:
    Captain(int _camp=0,int _x=0,int _y=0){
        init(0,_camp,"captain",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(tx-x)+abs(ty-y)==1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)+abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Captain(){}
};
class Duck:public Chess{ //鸭
public:
    Duck(int _camp=0,int _x=0,int _y=0){
        init(5,_camp,"duck",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*3 && ty==y+sy*2 && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1) return true;
                if (tx==x+sx*2 && ty==y+sy*3 && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*3) && yRange(y+sy*2) && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*3][y+sy*2]->id==0 && nowBoard.chess[x+sx*3][y+sy*2]->camp!=camp) return true;
                if (xRange(x+sx*2) && yRange(y+sy*3) && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*3]->id==0 && nowBoard.chess[x+sx*2][y+sy*3]->camp!=camp) return true;
            }
        }
        return false;
    }   
    ~Duck(){}
};  
class Soldier:public Chess{ //兵
public:
    Soldier(int _camp=0,int _x=0,int _y=0){
        init(6,_camp,"soldier",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(x-tx)<=1 && abs(y-ty)<=1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)<=1 && abs(sy)<=1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Soldier(){} 
};

这种写法较若干 if 的优点很明显:可更新性好 & 可读性好。我可以往棋盘中加入更多种类的棋子,而只需要多写一个子类,并不需要在除了该子类外的任何地方做任何修改。

在实现了每一个棋子后,Board 的实现也是比较容易的。

void Board::init(){ //初始化:把棋子放入格子内
    for (int i=0;i<10;++i){
        for (int j=0;j<9;++j){
            chess[i][j]=new Empty(-1,i,j);
        }
    }
    chess[0][0]=new Car(0,0,0);
    chess[0][1]=new Horse(0,0,1);
    chess[0][2]=new Elephant(0,0,2);
    chess[0][3]=new Guard(0,0,3);
    chess[0][4]=new Captain(0,0,4);
    chess[0][5]=new Guard(0,0,5);
    chess[0][6]=new Elephant(0,0,6);
    chess[0][7]=new Horse(0,0,7);
    chess[0][8]=new Car(0,0,8);
    chess[2][0]=new Duck(0,2,0);
    chess[2][8]=new Duck(0,2,8);
    for (int i=0;i<9;i+=2) chess[3][i]=new Soldier(0,3,i);
    for (int i=0;i<9;i+=2) chess[6][i]=new Soldier(1,6,i);
    chess[7][0]=new Duck(1,7,0);
    chess[7][8]=new Duck(1,7,8);
    chess[9][0]=new Car(1,9,0);
    chess[9][1]=new Horse(1,9,1);
    chess[9][2]=new Elephant(1,9,2);
    chess[9][3]=new Guard(1,9,3);
    chess[9][4]=new Captain(1,9,4);
    chess[9][5]=new Guard(1,9,5);
    chess[9][6]=new Elephant(1,9,6);
    chess[9][7]=new Horse(1,9,7);
    chess[9][8]=new Car(1,9,8);
}
void Board::run(int turn){
    int now=1;bool gameover=0; //now 表示当前是哪一方行动:1 为蓝方,0 为红方;gameover 表示游戏是否结束
    for (int t=1;t<=turn;++t){
        //print(t);
        int sx=9-read(),sy=read(),tx=9-read(),ty=read(); //将反人类的左下角数起变成左上角数起
        if (gameover || chess[sx][sy]->camp!=now || !chess[sx][sy]->canGo(tx,ty,*this)){ //如果游戏结束/当前的棋子不是当前玩家的/当前的棋子不能走到指定位置则非法
            puts("Invalid command");
        }else{
            cout<<(now==1?"red ":"blue ")<<chess[sx][sy]->name;putchar(';'); //输出当前移动的棋子
            cout<<(chess[tx][ty]->id==-1?"NA":(now==0?"red ":"blue ")+chess[tx][ty]->name);putchar(';'); //输出当前被吃掉的棋子
            if (chess[tx][ty]->id==0) gameover=1; //如果被吃的是王则游戏结束
            swap(chess[sx][sy],chess[tx][ty]); //把吃掉的那个棋子变成空格子(这里采用了比较奇妙的做法)
            chess[tx][ty]->x=tx;chess[tx][ty]->y=ty; //注意更新被移动棋子的坐标
            chess[sx][sy]=new Empty(-1,sx,sy);
            if (gameover || t==turn) printf("no;"); //如果游戏结束/本操作是最后一个行动则不会将军
            else{
                bool flag=false;
                for (int i=0;i<10;++i){
                    for (int j=0;j<9;++j){
                        if (chess[i][j]->canKill(*this)){ //给每一个棋子都判断一下是否将军
                            flag=true;break;
                        }
                    }
                    if (flag) break;
                }
                if (flag) printf("yes;");
                else printf("no;");
            }
            if (gameover) printf("yes\n"); //判断游戏是否结束
            else printf("no\n");
            now^=1; //换人
        }
    }
}

这道题就这样结束了。完整代码如下。

#include <bits/stdc++.h>
using namespace std;
namespace IO{
    int read(){
        int x=0,f=1;
        char ch=getchar();
        while (ch<'0' || ch>'9'){
            if (ch=='-') f=-f;
            ch=getchar();
        }
        while (ch>='0' && ch<='9'){
            x=x*10+ch-48;
            ch=getchar();
        }
        return x*f;
    }   
};
using IO::read;
bool xRange(int x){
    return 0<=x && x<=9;
}
bool yRange(int y){
    return 0<=y && y<=8;
}
class Chess;
class Board{
public:
    Board(){}
    Chess* chess[10][9];
    void init();
    void run(int turn);
    void print(int turn);
    ~Board(){}
};
class Chess{
public:
    int id,camp;
    string name;
    int x,y;
    Chess(){}
    void init(int _id=0,int _camp=0,string _name="",int _x=0,int _y=0){
        id=_id;camp=_camp;name=_name;x=_x;y=_y;
    }
    virtual bool canGo(int tx,int ty,Board &nowBoard){return 0;}
    virtual bool canKill(Board &nowBoard){return 0;}
    ~Chess(){} 
};
class Empty:public Chess{
public:
    Empty(int _camp=0,int _x=0,int _y=0){
        init(-1,_camp,"empty",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){return 0;}
    bool canKill(Board &nowBoard){return 0;}
    ~Empty(){}
};
class Car:public Chess{
public:
    Car(int _camp=0,int _x=0,int _y=0){
        init(1,_camp,"car",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (x!=tx && y==ty){
            for (int i=min(x,tx)+1;i<=max(x,tx)-1;++i){
                if (nowBoard.chess[i][y]->id!=-1) return false;
            }
        }else if (x==tx && y!=ty){
            for (int i=min(y,ty)+1;i<=max(y,ty)-1;++i){
                if (nowBoard.chess[x][i]->id!=-1) return false;
            }           
        }else return false;
        return true;
    } 
    bool canKill(Board &nowBoard){
        int tx,ty; 
        tx=x+1;ty=y;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx++;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x-1;ty=y;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) tx--;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x;ty=y+1;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty++;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        tx=x;ty=y-1;
        while (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==-1) ty--;
        if (xRange(tx) && yRange(ty) && nowBoard.chess[tx][ty]->id==0 && nowBoard.chess[tx][ty]->camp!=camp) return true;
        return false;       
    }
    ~Car(){}
};
class Horse:public Chess{
public:
    Horse(int _camp=0,int _x=0,int _y=0){
        init(2,_camp,"horse",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*2 && ty==y+sy && nowBoard.chess[x+sx][y]->id==-1) return true;
                if (tx==x+sx && ty==y+sy*2 && nowBoard.chess[x][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*2) && yRange(y+sy) && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*2][y+sy]->id==0 && nowBoard.chess[x+sx*2][y+sy]->camp!=camp) return true;
                if (xRange(x+sx) && yRange(y+sy*2) && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx][y+sy*2]->id==0 && nowBoard.chess[x+sx][y+sy*2]->camp!=camp) return true;
            }
        }
        return false;
    }
    ~Horse(){}
}; 
class Elephant:public Chess{
public:
    Elephant(int _camp=0,int _x=0,int _y=0){
        init(3,_camp,"elephant",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*2 && ty==y+sy*2 && nowBoard.chess[x+sx][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*2) && yRange(y+sy*2) && nowBoard.chess[x+sx][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*2]->id==0 && nowBoard.chess[x+sx*2][y+sy*2]->camp!=camp) return true;
            }
        }
        return false;
    } 
    ~Elephant(){}
}; 
class Guard:public Chess{
public:
    Guard(int _camp=0,int _x=0,int _y=0){
        init(4,_camp,"guard",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(tx-x)==1 && abs(ty-y)==1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)==1 && abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Guard(){}
};
class Captain:public Chess{
public:
    Captain(int _camp=0,int _x=0,int _y=0){
        init(0,_camp,"captain",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(tx-x)+abs(ty-y)==1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)+abs(sy)==1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Captain(){}
};
class Duck:public Chess{
public:
    Duck(int _camp=0,int _x=0,int _y=0){
        init(5,_camp,"duck",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (tx==x+sx*3 && ty==y+sy*2 && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1) return true;
                if (tx==x+sx*2 && ty==y+sy*3 && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1) return true;
            }
        }
        return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;sx+=2){
            for (int sy=-1;sy<=1;sy+=2){
                if (xRange(x+sx*3) && yRange(y+sy*2) && nowBoard.chess[x+sx*2][y+sy]->id==-1 && nowBoard.chess[x+sx][y]->id==-1 && nowBoard.chess[x+sx*3][y+sy*2]->id==0 && nowBoard.chess[x+sx*3][y+sy*2]->camp!=camp) return true;
                if (xRange(x+sx*2) && yRange(y+sy*3) && nowBoard.chess[x+sx][y+sy*2]->id==-1 && nowBoard.chess[x][y+sy]->id==-1 && nowBoard.chess[x+sx*2][y+sy*3]->id==0 && nowBoard.chess[x+sx*2][y+sy*3]->camp!=camp) return true;
            }
        }
        return false;
    }   
    ~Duck(){}
};  
class Soldier:public Chess{
public:
    Soldier(int _camp=0,int _x=0,int _y=0){
        init(6,_camp,"soldier",_x,_y);
    }
    bool canGo(int tx,int ty,Board &nowBoard){
        if (x==tx && y==ty) return false;
        if (nowBoard.chess[tx][ty]->camp==camp) return false;
        if (abs(x-tx)<=1 && abs(y-ty)<=1) return true;
        else return false;
    }   
    bool canKill(Board &nowBoard){
        for (int sx=-1;sx<=1;++sx){
            for (int sy=-1;sy<=1;++sy){
                if (abs(sx)<=1 && abs(sy)<=1 && xRange(x+sx) && yRange(y+sy) && nowBoard.chess[x+sx][y+sy]->id==0 && nowBoard.chess[x+sx][y+sy]->camp!=camp){
                    return true;
                }
            }
        }
        return false;
    }
    ~Soldier(){} 
};
void Board::init(){
    for (int i=0;i<10;++i){
        for (int j=0;j<9;++j){
            chess[i][j]=new Empty(-1,i,j);
        }
    }
    chess[0][0]=new Car(0,0,0);
    chess[0][1]=new Horse(0,0,1);
    chess[0][2]=new Elephant(0,0,2);
    chess[0][3]=new Guard(0,0,3);
    chess[0][4]=new Captain(0,0,4);
    chess[0][5]=new Guard(0,0,5);
    chess[0][6]=new Elephant(0,0,6);
    chess[0][7]=new Horse(0,0,7);
    chess[0][8]=new Car(0,0,8);
    chess[2][0]=new Duck(0,2,0);
    chess[2][8]=new Duck(0,2,8);
    for (int i=0;i<9;i+=2) chess[3][i]=new Soldier(0,3,i);
    for (int i=0;i<9;i+=2) chess[6][i]=new Soldier(1,6,i);
    chess[7][0]=new Duck(1,7,0);
    chess[7][8]=new Duck(1,7,8);
    chess[9][0]=new Car(1,9,0);
    chess[9][1]=new Horse(1,9,1);
    chess[9][2]=new Elephant(1,9,2);
    chess[9][3]=new Guard(1,9,3);
    chess[9][4]=new Captain(1,9,4);
    chess[9][5]=new Guard(1,9,5);
    chess[9][6]=new Elephant(1,9,6);
    chess[9][7]=new Horse(1,9,7);
    chess[9][8]=new Car(1,9,8);
}
void Board::run(int turn){
    int now=1;bool gameover=0;
    for (int t=1;t<=turn;++t){
        //print(t);
        int sx=9-read(),sy=read(),tx=9-read(),ty=read();
        if (gameover || chess[sx][sy]->camp!=now || !chess[sx][sy]->canGo(tx,ty,*this)){
            puts("Invalid command");
        }else{
            cout<<(now==1?"red ":"blue ")<<chess[sx][sy]->name;putchar(';');
            cout<<(chess[tx][ty]->id==-1?"NA":(now==0?"red ":"blue ")+chess[tx][ty]->name);putchar(';');
            if (chess[tx][ty]->id==0) gameover=1;
            swap(chess[sx][sy],chess[tx][ty]);
            chess[tx][ty]->x=tx;chess[tx][ty]->y=ty;
            chess[sx][sy]=new Empty(-1,sx,sy);
            if (gameover || t==turn) printf("no;");
            else{
                bool flag=false;
                for (int i=0;i<10;++i){
                    for (int j=0;j<9;++j){
                        if (chess[i][j]->canKill(*this)){
                            flag=true;break;
                        }
                    }
                    if (flag) break;
                }
                if (flag) printf("yes;");
                else printf("no;");
            }
            if (gameover) printf("yes\n");
            else printf("no\n");
            now^=1;
        }
    }
}
void Board::print(int turn){
    printf("turn %d:\n",turn);
    for (int i=0;i<10;++i){
        for (int j=0;j<9;++j){
            if (chess[i][j]->camp==0){
                switch (chess[i][j]->id){
                    case 0:printf("[王]");break;
                    case 1:printf("[车]");break;
                    case 2:printf("[马]");break;
                    case 3:printf("[象]");break;
                    case 4:printf("[士]");break;
                    case 5:printf("[鸭]");break;
                    case 6:printf("[兵]");break;
                    default:printf("    ");
                }               
            }else{
                switch (chess[i][j]->id){
                    case 0:printf("(王)");break;
                    case 1:printf("(车)");break;
                    case 2:printf("(马)");break;
                    case 3:printf("(象)");break;
                    case 4:printf("(士)");break;
                    case 5:printf("(鸭)");break;
                    case 6:printf("(兵)");break;
                    default:printf("    ");
                }               
            }
        }
        printf("\n");
    }
}
signed main(){
    int T=read();
    Board newGame;
    newGame.init();
    newGame.run(T);
    return 0;
}