hdu 4121 Xiangqi

我写的时候主要有2个大错误

1:判断2点之间多少个子,没有保证起点<终点

2:马的方向是8个,我画图时搞了4个。。。

#include <iostream>
#include <vector>
#include <stack>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

/*  define */
#define sf(a) scanf("%d",&a)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define clr(a) memset(a,0,sizeof(a))
/*  define */

int dx[4]={0,0,1,-1};
int dy[4]={-1,1,0,0};
bool flag[50][50];

const int inf = (1<<29);
int n,newx,newy;

struct chess{
    int x,y;
    char kind[10];
}p[20];

int row(chess t){
    int st=min(newy,t.y),ed=max(newy,t.y),pos=t.x;
    int ans=0;
    for(int i=st+1;i<ed;i++)
        if(flag[pos][i])
            ans++;
    return ans;
}

int col(chess t){
    int st=min(newx,t.x),ed=max(newx,t.x),pos=t.y;
    int ans=0;
    for(int i=st+1;i<ed;i++)
        if(flag[i][pos])
            ans++;
    return ans;
}
/* if hob horse return true */
bool hobHorse(chess t){
    int x=t.x,y=t.y;
    if(y>newy && x>newx){
        if(flag[x][y-1])
            return true;
        if(flag[x-1][y])
            return true;
        return false;
    }
    else if(y>newy && x<newx){
        if(flag[x][y-1])
            return true;
        if(flag[x+1][y])
            return true;
        return false;
    }
    else if(y<newy && x>newx){
        if(flag[x][y+1])
            return true;
        if(flag[x-1][y])
            return true;
        return false;
    }
    else if(y<newy && x<newx){
        if(flag[x][y+1])
            return true;
        if(flag[x+1][y])
            return true;
        return false;
    }
    return false;
}
bool ok(int x,int y){
    for(int i=1;i<=n;i++){
        int _x=p[i].x,_y=p[i].y;
        if(flag[_x][_y]==0) continue;
        if(p[i].kind[0]=='G'){
            if(y==_y && col(p[i])==0)
                return true;
        }
        else if(p[i].kind[0]=='R'){
            if(x==_x && row(p[i])==0)
                return true;
            if(y==_y && col(p[i])==0)
                return true;
        }
        else if(p[i].kind[0]=='H'){
            if(abs(y-_y)==2 && abs(x-_x)==1){
                if(!hobHorse(p[i]))
                    return true;
            }
            if(abs(y-_y)==1 && abs(x-_x)==2){
                if(!hobHorse(p[i]))
                    return true;
            }
        }
        else if(p[i].kind[0]=='C'){
            if(x==_x && row(p[i])==1)
                return true;
            if(y==_y && col(p[i])==1)
                return true;
        }
    }
    return false;
}

int main(){
    int x,y;
    while(~scanf("%d%d%d",&n,&x,&y)&&(n+x+y)){
        bool dead=true;
        clr(flag);
        rep(i,1,n){
            scanf("%s",p[i].kind);
            scanf("%d%d",&p[i].x,&p[i].y);
            flag[p[i].x][p[i].y]=1;
        }
        for(int i=0;i<4;i++){
            newx=x+dx[i];
            newy=y+dy[i];
            if(newx<1 || newx>3 ||newy<4 ||newy>6)
                continue;
            if(flag[newx][newy]){
                flag[newx][newy]=0;
                if(!ok(newx,newy)){
                    dead=false;
                    goto A;
                }
                flag[newx][newy]=1;
            }
            else {
                if(!ok(newx,newy)){
                    dead=false;
                    goto A;
                }
            }
        }
        A:;
        if(dead)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值