qz
查看原帖
qz
746790
space_sea楼主2024/8/5 07:32

昨天调了一下午,真的要崩溃了... 哪位好心人帮帮我

#include <bits/stdc++.h>
using namespace std;
bool game_over;
int rkx=5, rky=1, bkx=5, bky=10;
int read(){
    char c=getchar();
    int sum=0, flag=1;
    while (c<'0'||c>'9'){
        if (c=='-'){
            flag=-1;
        }
        c=getchar();
    }
    while (c>='0'&&c<='9'){
        sum=(sum<<1)+(sum<<3)+(c^48);
        c=getchar();
    }
    return sum*flag;
}
struct node{
    int id=0;//棋子种类(1王,2士,3象,4马,5车,6鸭,7兵)
    bool flag=0;//所属阵营(0红,1蓝)
}e[11][10];
void set__(int y, int x, int id, bool flag){
    e[x][y].id=id;
    e[x][y].flag=flag;
}
void init(){//棋子初始化
	int k=0;//辅助,进行y坐标翻转 
    for (int i=1; i>=0; i--){
    	if (!i){
    		k=11;
		}
        set__(5, abs(k-1), 1, i);//中王初始化
        set__(4, abs(k-1), 2, i);//左士初始化
        set__(6, abs(k-1), 2, i);//右士初始化
        set__(3, abs(k-1), 3, i);//左象初始化
        set__(7, abs(k-1), 3, i);//右象初始化
        set__(2, abs(k-1), 4, i);//左马初始化
        set__(8, abs(k-1), 4, i);//右马初始化
        set__(1, abs(k-1), 5, i);//左车初始化
        set__(9, abs(k-1), 5, i);//右车初始化
        set__(1, abs(k-3), 6, i);//左鸭初始化
        set__(9, abs(k-3), 6, i);//右鸭初始化
        for (int j=1; j<=9; j+=2){//五兵初始化
            set__(j, abs(k-4), 7, i);
        }
    }
}
bool go(int x, int y, int flag){//坐标之间的距离判定
    return abs(x-y)==flag;
}
bool move_check(int sx, int sy, int fx, int fy){//移动合理性判定(1合理,0不合理)
    if (fx>10||fx<=0||fy>9||fy<=0){//判越界
        return 1;
    }
    if (e[sx][sy].id==1){//王判定
        if (abs(sx-fx)+abs(sy-fy)!=1){
            return 1;
        }
        if (e[sx][sy].flag){
        	bkx=fx;
        	bky=fy;
		}else{
			rkx=fx;
        	rky=fy;
		}
    }
    if (e[sx][sy].id==2){//士判定
        if (!(go(sx, fx, 1)&&go(sy, fy, 1))){
            return 1;
        }
    }
    if (e[sx][sy].id==3){//象判定
        if (!(go(sx, fx, 2)&&go(sy, fy, 2)&&!e[sx+(fx-sx)/2][sy+(fy-sy)/2].id)){
            return 1;
        }
    }
    if (e[sx][sy].id==4){//马判定
        if (!(go(sx, fx, 1)&&go(sy, fy, 2)&&!e[sx][sy+(fy-sy)/2].id||go(sx, fx, 2)&&go(sy, fy, 1)&&!e[sx+(fx-sx)/2][sy].id)){
            return 1;
        }
    }
    if (e[sx][sy].id==5){//车判定
        if (!(sx==fx||sy==fy)){
            return 1;
        }
        if (sx>fx){
        	swap(sx, fx);
		}
        for (int i=sx+1; i<fx; i++){
            if (e[i][sy].id){
                return 1;
            }
        }
        if (sy>fy){
        	swap(sy, fy);
		}
        for (int i=sy+1; i<fy; i++){
            if (e[sx][i].id){
                return 1;
            }
        }
    }
    if (e[sx][sy].id==6){//鸭判定
        if(!(go(sx, fx, 3)&&go(sy, fy, 2)&&
		!e[int(sx+1.0*(fx-sx)/1.5)][sy+(fy-sy)/2].id&&
		!e[sx+(fx-sx)/3][sy].id
		||
		go(sx, fx, 2)&&go(sy, fy, 3)&&
		!e[sx+(fx-sx)/2][int(sy+1.0*(fy-sy)/1.5)].id&&
		!e[sx][sy+(fy-sy)/3].id
		)){
            return 1;
        }
    }
    /*
    0 0 0 b
	0 0 c 0
	a c 0 0
	
	0 0 b
	0 c 0
	c 0 0
	a 0 0
	*/ 
    if (e[sx][sy].id==7){//兵判定
        if (!(abs(sx-fx)+abs(sy-fy)==1||go(sx, fx, 1)&&go(sy, fy, 1))){
            return 1;
        }
    }
    return 0;
}
void move(int sx, int sy, int fx, int fy){
	if (e[fx][fy].id==1){
		game_over=1;
		if (e[fx][fy].flag){
			bkx=bky=-1;
		}else{
			rkx=rky=-1;
		}
	}
	e[fx][fy].id=e[sx][sy].id;
	e[sx][sy].id=0;
	e[fx][fy].flag=e[sx][sy].flag;
} 
bool check_mate(int x, int y){//将军判断(0没将军, 1将军)
	for (int i=1; i<=10; i++){
		for (int j=1; j<=9; j++){
			if (e[i][j].id&&e[i][j].flag!=e[x][y].flag){
				if (move_check(i, j, x, y)){
//					printf("Debug:(%d,%d)->(%d,%d) check\n",i,j,x,y); 
					return 1;
				}
			}
		}
	}
	return 0;
}
string name[8]={"", "captain", "guard", "elephant", "horse", "car", "duck", "soldier"};
int main(){
    // cout << "Debug:-1\n";
    init();
//    for (int i=1; i<=10; i++){
//    	for (int j=1; j<=9; j++){
//    		cout << e[i][j].id << " ";
//		}
//		cout << endl;
//	}
//	for (int i=1; i<=10; i++){
//    	for (int j=1; j<=9; j++){
//    		cout << e[i][j].flag << " ";
//		}
//		cout << endl;
//	}
    int n=read();
    bool flag=0;
    for (int i=1; i<=n; i++){
        int sx=read(), sy=read(), fx=read(), fy=read();
//        for (int i=1; i<=10; i++){
//    		for (int j=1; j<=9; j++){
//    			cout << e[i][j].id << " ";
//			}
//			cout << endl;
//		}
        sx=10-sx, fx=10-fx, sy++, fy++;
//        cout << sx << " " << sy << " " << fx << " " << fy << endl;
		if (e[sx][sy].flag!=flag){//棋子错误判定 
			cout << "Invalid command\n";
//			cout << "Debug1\n";
            continue;
		}
        if (!e[sx][sy].id){//无棋子判定
            cout << "Invalid command\n";
//            cout << "Debug2\n";
            continue;
        }
        if (e[fx][fy].id&&e[fx][fy].flag==flag){//落点友方判定
            cout << "Invalid command\n";
//            cout << "Debug3\n";
            continue;
        }
        if (move_check(sx, sy, fx, fy)){//移动错误判定
            cout << "Invalid command\n";
//            cout << "Debug4\n";
            continue;
        }
        if (game_over){//游戏结束判定
            cout << "Invalid command\n";
//            cout << "Debug5\n";
            continue;
        }
//        printf("Debug:%d\n",e[sx][sy].id);
        string first=(flag?"blue ":"red ")+name[e[sx][sy].id];
        string second=e[fx][fy].id?(flag?"red ":"blue ")+name[e[fx][fy].id]:"NA";
        move(sx, sy, fx, fy);
        string mate=!game_over&&check_mate(rkx, rky)||check_mate(bkx, bky)?"yes":"no";
        string over=game_over?"yes":"no";
        cout << first << ";" << second << ";" << mate << ";" << over << endl;
		flag=!flag;
	}

    return 0;
}//10 4       9 3
/*
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;no
red duck;blue captain;no;yes
Invalid command
*/
2024/8/5 07:32
加载中...