POJ4001 Xiangqi

百分百的模拟题,要注意一些细节,如输入用cin(如果用scanf,数据坑爹的话,给你来几个空格就蛋疼了),代码略长,非常考验代码的功底,自己太挫,弹了N次才AC的 TAT

网赛快到了,要加油了!!!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100+10;
const int xx[4] = {0,0,1,-1};
const int yy[4] = {1,-1,0,0};
struct node{
	int x,y,type;
	node(int x=0,int y=0,int type=0):x(x),y(y),type(type){}
};
vector<node> xq;
int gx,gy;//黑将军初始位置 
int mat[maxn][maxn];//记录是否有子 
bool in_chess(int x,int y){
	return x >= 1 && x <= 10 && y >= 1 && y <= 9;
}
bool in_board(int x,int y){
	return x>=1 && x <=3 && y >=4 && y <= 6;
}
bool checkmate(node black,node red){
		int minx = min(black.x,red.x);
		int maxx = max(black.x,red.x);
		int miny = min(black.y,red.y);
		int maxy = max(black.y,red.y);
		if(red.type == 1){
		   if(red.y == black.y){
		    for(int k = minx+1; k < maxx; k++){
		   	  if(mat[k][red.y])
		   		return false;
		    }
		    return true;
	       }else  return false;
	
		}
		else if(red.type == 2){
		   if(black.x == red.x){
		   	for(int k = miny+1; k < maxy; k++)
		   		if(mat[red.x][k]) return false;
		   	return true;
		   }
		   else if(red.y == black.y){
		   	for(int k = minx+1; k < maxx; k++)
		   		if(mat[k][red.y])  return false;
		   	return true;
		   }else  return false;
		}	
		else if(red.type == 4){
		   int cnt = 0;
		   if(red.x == black.x){
		   	for(int k = miny+1; k < maxy; k++)
		   		if(mat[red.x][k]) cnt++;
			if(cnt == 1)  return true;
		    else  return false;
		   }
		   else if(red.y == black.y){
		   	for(int k = minx+1; k < maxx; k++)
		   		if(mat[k][red.y])  cnt++;
		   	if(cnt == 1)  return true;
		    else  return false;
		   	
		   }else return false;
		   
		    
		}
		else{
           for(int i = 0; i < 4; i++){
           	int tx = red.x+xx[i];
           	int ty = red.y+yy[i];
           	if(!in_chess(tx,ty))  continue;
           	if(!mat[tx][ty]){
           		switch (i){
           			case 0:
           				if(tx+1==black.x && ty+1 == black.y) return 1;
           				if(tx-1==black.x && ty+1 == black.y) return 1;
           				break;
           			case 1:
           				if(tx+1==black.x && ty-1 == black.y) return 1;
           				if(tx-1==black.x && ty-1 == black.y) return 1;
           				break;
           			case 2:
           				if(tx+1==black.x && ty-1 == black.y) return 1;
           				if(tx+1==black.x && ty+1 == black.y) return 1;
           				break;
           			case 3:
           				if(tx-1==black.x && ty-1 == black.y) return 1;
           				if(tx-1==black.x && ty+1 == black.y) return 1;
           			
           				
           		}
           	}
           }
		   return false;   
       }
		
	
}
void init(){
	memset(mat,0,sizeof(mat));
	xq.clear();
}
/*1 : 将军
  2 : 车
  3 :马
  4 :炮 
*/ 
int main(){
	int n;
	while(~scanf("%d%d%d",&n,&gx,&gy) && n|gx|gy){
		init();
		while(n--){
		   char type;
		   int a,b; 
		   cin >> type >> a >> b;
		   mat[a][b] = 1;
		   if(type == 'G'){
		   	 xq.push_back(node(a,b,1));
		   	 
		   }	
		   else if(type == 'R'){
		     xq.push_back(node(a,b,2));
		   }
		   else if(type == 'H'){
		   	 xq.push_back(node(a,b,3));
		   }else{
		   	 xq.push_back(node(a,b,4));
		   }
		  
		} 
		bool flag;
		for(int i = 0; i < 4; i++){
			 
		   	 int x = gx + xx[i];
		   	 int y = gy + yy[i];
		   	 if(in_board(x,y)){
				flag = 0;
		   	 	node tmp;
		   	 	tmp.x = x;
		   	 	tmp.y = y;
		   	 	for(int j = 0; j < xq.size(); j++){
		   	 		if(x == xq[j].x && y == xq[j].y)  continue;
		   	 		if(checkmate(tmp,xq[j])){
		   	 			flag = 1;
		   	 			break;
		   	 		}
		   	 	}
				if(!flag)  break;
		   	 
		   	 }
		   	 
		}
		if(!flag)  cout<<"NO"<<endl;
		else    cout<<"YES"<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值