uva1589 象棋

#include 
#include 
#include 
char a[15][15];
const int dr[4]= {-1,1,0,0},dc[4]= {0,0,-1,1};
const int Horser[8]= {-2,-2,-1,-1,1,1,2,2};
const int Horsec[8]= {-1,1,-2,2,-2,2,-1,1};
const int Stoner[8]= {-1,-1,-1,-1,1,1,1,1};
const int Stonec[8]= {-1,1,-1,1,-1,1,-1,1};
int check(int r,int c)//返回值为0表示安全,1表示不安全
{
    int i,j,t,patten;
    for(t=0; t<=3; t++)
    {
        patten=1;
        for(i=r+dr[t],j=c+dc[t]; i>=1&&i<=10&&j>=1&&j<=9; i+=dr[t],j+=dc[t])
        {
            if(patten==1)//找车将
            {
                if(a[i][j])
                {
                    if(a[i][j]=='G'||a[i][j]=='R')
                        return 1;
                    else
                        patten=2;
                }
            }
            else if(patten==2)//找炮
            {
                if(a[i][j])
                {
                    if(a[i][j]=='C')
                        return 1;
                    else
                        break;
                }
            }
        }
    }
    for(i=0; i<=7; i++)//找马
    {
        if(!(r+Horser[i]>=1&&r+Horser[i]<=10&&c+Horsec[i]>=1&&c+Horsec[i]<=9))
            continue;
        if(a[r+Horser[i]][c+Horsec[i]]=='H'&&!a[r+Stoner[i]][c+Stonec[i]])
            return 1;
    }
    return 0;
}
int main()
{
    int i,n,r,c,rr,cc,result;
    char ch;
    while(1)
    {
        memset(a,0,sizeof a);
        result=1;
        scanf("%d%d%d",&n,&r,&c);
        if(!n&&!r&&!c)
            break;
        for(i=0; i<=n-1; i++)
        {
            scanf(" %c%d%d",&ch,&rr,&cc);
            a[rr][cc]=ch;
        }
        for(i=r+1; i<=10; i++)
        {
            if(a[i][c])
            {
                if(a[i][c]=='G')
                    result=0;
                break;
            }
        }
        if(result)
        {
            for(i=0; i<=3; i++)
            {
                if(r+dr[i]<1||r+dr[i]>3||c+dc[i]<4||c+dc[i]>6)
                    continue;
                if(!check(r+dr[i],c+dc[i]))
                {
                    result=0;
                    break;
                }
            }
        }
        if(result)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

你可能感兴趣的:(uva)