1589-Xiangqi【模拟】

纯模拟题,判断走4个方向,判断是不是能够被将死

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#include<cmath>
using namespace std;
typedef long long LL;
#define ALL(x) x.begin(),x.end()
char Map[15][15];
int start_x,start_y,n;
const int range_x1 = 1 ,range_x2 = 3;
const int range_y1 = 4 ,range_y2 = 6;
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
void display(){
    printf("================\n");
    for(int i = 1; i <= 10 ; i++){
        for(int j = 1; j <= 9 ; j++)
        printf("%c",Map[i][j]);
        printf("\n");
    }
    printf("================\n");
}
int _G(int x,int y,int a,int b){
    if(y != b) return 1; /*返回1说明没被吃掉*/
    for(int i = x - 1; i >= 1 ; i --)
    if(i == a) return 0; /*返回0说明被吃掉了*/
    else
    if(Map[i][y] != '.')
    return 1;
}
int _C(int x,int y,int a,int b){
    if(x != a && y != b) return 1; /*不在一行一列肯定不行*/
    int _count = 0;
    if(x == a){       /*在一条横向上*/
        int pos_s = min(b,y);
        int pos_e = max(b,y);
        for(int i = pos_s + 1; i < pos_e ; i++)
        if(Map[a][i] != '.') _count ++;
    }
    else if(y == b){  /*在一条竖线上*/
        int pos_s = min(a,x);
        int pos_e = max(a,x);
        for(int i = pos_s + 1; i < pos_e ; i++)
        if(Map[i][b] != '.') _count ++;
    }
    if(_count == 1) return 0;
    else
    return 1;
}
int _H(int x,int y,int a,int b){
    /*上*/
    if(x - 1 >= 1){
        if(Map[x - 1][y] == '.'){
            if(x - 2 >= 1 && y - 1 >= 1){
                if(x - 2 == a && y - 1 == b)
                return 0;
            }
            if(x - 2 >= 1 && y + 1 <= 9){
                if(x - 2 == a && y + 1 == b)
                return 0;
            }
        }
    }
    /*下*/
    if(x + 1 <= 10){
        if(Map[x + 1][y] == '.'){
            if(x + 2 <= 10 && y - 1 >= 1){
                if(x + 2 == a && y - 1 == b)
                return 0;
            }
            if(x + 2 <= 10 && y + 1 <= 9){
                if(x + 2 == a && y + 1 == b)
                return 0;
            }
        }
    }
    /*左*/
    if(y - 1 >= 0){
        if(Map[x][y - 1] == '.'){
            if(x - 1 >= 1 &&  y - 2 >= 1){
                if(x - 1 == a && y - 2 == b)
                return 0;
            }
            if(x + 1 <= 10 && y - 2 >= 1){
                if(x + 1 == a && y - 2 == b)
                return 0;
            }
        }
    }
    /*右*/
    if(y + 1 <= 9){
        if(Map[x][y + 1] == '.'){
            if(Map[x][y - 1] == '.'){
            if(x - 1 >= 1 &&  y + 2 <= 9){
                if(x - 1 == a && y + 2 == b)
                return 0;
            }
            if(x + 1 <= 10 && y + 2 <= 9){
                if(x + 1 == a && y + 2 == b)
                return 0;
            }
            }
        }
    }
    return 1;
}
int _R(int x,int y,int a,int b){
    if(x != a && y != b) return 1; /*不在一行一列肯定不行*/
    int _count = 0;
    if(x == a){       /*在一条横向上*/
        int pos_s = min(b,y);
        int pos_e = max(b,y);
        for(int i = pos_s + 1; i < pos_e ; i++)
        if(Map[a][i] != '.') _count ++;
    }
    else if(y == b){  /*在一条竖线上*/
        int pos_s = min(a,x);
        int pos_e = max(a,x);
        for(int i = pos_s + 1; i < pos_e ; i++)
        if(Map[i][b] != '.') _count ++;
    }
    if(_count == 0) return 0;
    else
    return 1;
}
int _solve(int xx,int yy){
    int ok = 1;
    for(int i = 1 ; i <= 10 ; i++)
      for(int j = 1 ; j <= 9 ; j++){
          if(Map[i][j] == 'G'){
                ok = _G(i,j,xx,yy);
            }
          else if(Map[i][j] == 'C')ok = _C(i,j,xx,yy);
          else if(Map[i][j] == 'R')ok = _R(i,j,xx,yy);
          else if(Map[i][j] == 'H')ok = _H(i,j,xx,yy);
          if(ok == 0) /*所有棋子中,只要有一个棋子能吃到他就无法走*/
          return 0;
    }
    return 1;
}
int solve(){
    /*走4个方向*/
    int ok ;
    for(int i = 0 ; i < 4 ; i ++){
        int now_x =  start_x + dir[i][0];
        int now_y =  start_y + dir[i][1];
        if(now_x >= range_x1 && now_x <= range_x2 && now_y >= range_y1 && now_y <= range_y2){
            char temp = Map[now_x][now_y];
            Map[now_x][now_y] = '.';
            int ok = _solve(now_x,now_y); /*如果不能走为0,能走为1*/
            Map[now_x][now_y] = temp;
            if(ok)
            return 0; /*返回0代表没有被将军*/
        }
    }
    return 1;
}
int main(){
    while(scanf("%d%d%d",&n,&start_x,&start_y)){
        if(!start_x && !start_y && !n) break;
        memset(Map,'.',sizeof(Map));
        for(int i = 0 ; i < n ; i ++){
            char t[10];
            int x,y;
            scanf("%s%d%d",t,&x,&y);
            Map[x][y] = t[0];
        }
        int ok = solve();
        if(ok)  printf("YES\n"); /*被将军了*/
        else
        printf("NO\n"); /*没有被将军*/
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值