QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#85168 | #2821. 鸭棋 | zombie462 | AC ✓ | 2ms | 3576kb | C++23 | 10.1kb | 2023-03-07 02:14:11 | 2023-03-07 02:14:12 |
Judging History
answer
#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;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3560kb
input:
18 0 0 7 0 9 0 8 0 0 1 1 3 0 2 2 0 0 3 1 2 0 4 0 3 9 4 8 4 3 2 2 3 7 0 4 2 7 0 5 3 9 2 7 4 2 0 4 3 9 1 8 3 4 3 6 6 7 4 9 2 8 4 9 4 6 6 9 4 9 8 8 8
output:
Invalid command Invalid command Invalid command Invalid command red guard;NA;no;no Invalid command blue captain;NA;no;no red soldier;NA;no;no Invalid command Invalid command blue elephant;NA;no;no red duck;NA;no;no blue horse;NA;no;no red duck;blue soldier;no;no Invalid command blue captain;NA;yes;n...
result:
ok 18 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
1000 0 7 2 6 3 8 4 8 3 0 4 0 0 6 8 6 0 3 1 4 6 2 5 3 6 0 5 0 0 4 1 4 9 7 7 6 9 5 8 4 9 5 8 6 7 0 3 2 3 0 2 1 9 2 7 4 5 3 4 2 1 4 2 4 0 1 2 2 6 0 7 1 9 5 8 4 2 6 1 4 2 1 1 2 2 8 8 1 9 8 8 8 6 6 7 7 7 4 9 2 8 8 8 0 2 0 5 2 2 8 6 1 1 7 1 1 0 3 1 2 3 8 3 7 9 0 8 0 0 3 1 2 3 4 2 5 9 6 9 4 6 4 5 5 4 2 7 4...
output:
red horse;NA;no;no Invalid command Invalid command Invalid command Invalid command blue soldier;NA;no;no Invalid command red captain;NA;no;no blue horse;NA;no;no Invalid command Invalid command Invalid command red soldier;NA;no;no blue elephant;NA;no;no Invalid command red captain;NA;no;no Invalid c...
result:
ok 1000 lines
Test #3:
score: 0
Accepted
time: 2ms
memory: 3576kb
input:
1000 3 8 4 1 6 2 8 2 0 0 1 5 9 7 7 6 0 7 8 7 9 5 8 6 5 4 1 0 9 7 6 4 9 7 0 3 2 0 3 0 9 3 8 0 2 8 9 2 7 5 7 8 9 5 8 6 9 6 8 2 6 2 6 3 0 7 1 7 9 1 7 2 1 7 0 5 7 0 8 5 9 5 0 4 3 4 4 5 0 6 8 8 6 2 5 1 9 5 8 4 0 1 2 1 0 3 2 4 2 7 8 5 9 8 8 8 0 0 1 0 3 2 7 3 3 0 0 1 0 5 9 4 9 2 7 4 0 1 2 2 9 7 1 0 6 6 6 5...
output:
Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid comm...
result:
ok 1000 lines
Test #4:
score: 0
Accepted
time: 1ms
memory: 3532kb
input:
1000 6 4 5 5 9 1 4 5 9 7 5 8 9 4 8 4 0 6 5 1 0 3 5 5 0 3 5 5 9 7 6 2 2 8 9 6 9 5 5 8 0 4 1 4 0 0 5 6 0 7 3 5 2 8 7 8 0 5 6 8 7 8 9 8 9 2 9 3 0 3 1 2 3 2 5 5 0 5 1 6 3 6 5 6 3 2 1 1 0 5 6 7 9 5 3 4 3 0 2 4 3 4 6 3 9 7 8 7 0 6 9 1 6 8 8 4 6 8 6 3 6 0 5 7 2 0 8 7 9 8 4 0 0 7 3 8 0 3 5 2 2 0 1 7 3 2 0 3...
output:
Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command red captain;NA;no;no Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid command Invalid...
result:
ok 1000 lines