Uva 1589 象棋


在temp数组中把红方所有棋子能到达的位置标‘1’,最后在黑将能走的地方判断一下有没有没被标‘1’的 没有则被将死。


#include <iostream>
#include <cstdio>
using namespace std;
char board[10][9]={},temp[10][9]={};
int G_G(int i,int j)
{

    for(int i=7;i<10;i++)
    {
        for(int j=3;j<6;j++)
            if(board[i][j]=='G')
            {
            for(int n=i-1;n>=0;n--)
        {
            if(board[n][j]=='0'||board[n][j]=='1')
            temp[n][j]='1';
            else
            {
                temp[n][j]='1';
                return 0;
            }

        }
            }
    }
}
void R(int i,int j)
{
    for(int m=i-1;m>=0;m--)
        if((board[m][j]=='0'||board[m][j]=='1')&&board[m][j]!='G')
            temp[m][j]='1';
        else
        {
            temp[m][j]='1';
            break;
        }
    for(int m=i+1;m<10;m++)
        if((board[m][j]=='0'||board[m][j]=='1')&&board[m][j]!='G')
            temp[m][j]='1';
        else
        {
            temp[m][j]='1';
            break;
        }

    for(int n=j-1;n>=0;n--)
        if((board[i][n]=='0'||board[i][n]=='1')&&board[i][n]!='G')
            temp[i][n]='1';
        else
        {
            temp[i][n]='1';
            break;
        }

    for(int n=j+1;n<9;n++)
        if((board[i][n]=='0'||board[i][n]=='1')&&board[i][n]!='G')
            temp[i][n]='1';
        else
        {
            temp[i][n]='1';
            break;
        }

}
void H(int i,int j)
{
    if(board[i-1][j]=='0'||board[i-1][j]=='1')
    {
        temp[i-2][j-1]='1';temp[i-2][j+1]='1';
    }
    if(board[i+1][j]=='0'||board[i+1][j]=='1')
    {
         temp[i+2][j-1]='1';temp[i+2][j+1]='1';
    }
    if(board[i][j-1]=='0'||board[i][j-1]=='1')
    {
         temp[i-1][j-2]='1';temp[i+1][j-2]='1';
    }
    if(board[i][j+1]=='0'||board[i][j+1]=='1')
    {
         temp[i-1][j+2]='1';temp[i+1][j+2]='1';
    }
}
void C(int i,int j)
{
    int flag=0;
    for(int m=i-1;m>=0;m--)
    {
        if(board[m][j]!='0')
        {
            flag=1;
            for(int p=m-1;p>=0;p--)
            {
                if((board[p][j]=='0'||board[p][j]=='1')&&board[p][j]!='G')
                {
                    temp[p][j]='1';
                }
                else
                {
                    temp[p][j]='1';
                    flag=1;
                    break;
                }
            }
        }
            if(flag==1)
            {
                 flag=0;
                 break;
            }

    }
    for(int m=i+1;m<10;m++)
    {
        if(board[m][j]!='0')
        {
            flag=1;
            for(int p=m+1;p<10;p++)
            {
                if((board[p][j]=='0'||board[p][j]=='1')&&board[p][j]!='G')
                {
                    temp[p][j]='1';
                }
                else
                {
                    temp[p][j]='1';
                    flag=1;
                    break;
                }
            }
        }
            if(flag==1)
            {
                flag=0;
                break;
            }

    }
    for(int n=j-1;n>=0;n--)
    {
        if(board[i][n]!='0')
        {
            flag=1;
            for(int q=n-1;q>=0;q--)
            {
                if((board[i][q]=='0'||board[i][q]=='1')&&board[i][q]!='G')
                {
                    temp[i][q]='1';
                }
                else
                {
                    temp[i][q]='1';
                    flag=1;
                    break;
                }
            }
        }
        if(flag==1)
        {
            flag=0;
            break;
        }

    }
    for(int n=j+1;n<9;n++)
    {
        if(board[i][n]!='0')
        {
            flag=1;
            for(int q=n+1;q<9;q++)
            {
                if((board[i][q]=='0'||board[i][q]=='1')&&board[i][q]!='G')
                {
                    temp[i][q]='1';
                }
                else
                {
                    temp[i][q]='1';
                    flag=1;
                    break;
                }
            }
        }
        if(flag==1)
        {
            flag=0;
            break;
        }
    }
}
void start()
{
    for(int i=0;i<10;i++)
        for(int j=0;j<9;j++)
        {
            board[i][j]='0';
            temp[i][j]='0';
        }

}
int main()
{
    //freopen("c:\\test.txt","r",stdin);
    //freopen("c:\\hu.txt","w",stdout);
    int T,x,y,m,n,flag=0,flag2=0;
    char t;
    while(cin>>T>>x>>y&&T&&x&&y)
    {
        start();
        x--;y--;
    while(T--)
    {
        cin>>t>>m>>n;
        board[m-1][n-1]=t;
    }
    for(int i=0;i<10;i++)
        {
            for(int j=0;j<9;j++)
                {
                    if(board[i][j]!='1'&&board[i][j]!='0')
                        {
                            if(board[i][j]=='R')
                                R(i,j);
                            else if(board[i][j]=='H')
                                H(i,j);
                            else if(board[i][j]=='C')
                                C(i,j);
                            else if(board[i][j]='G')
                                G_G(i,j);
                        }
                }
        }
        if(x-1>=0)
            if(temp[x-1][y]!='1')
            flag=1;
        if(x+1<=2)
            if(temp[x+1][y]!='1')
            flag=1;
        if(y-1>=3)
            if(temp[x][y-1]!='1')
            flag=1;
        if(y+1<=5)
            if(temp[x][y+1]!='1')
            flag=1;
            if(flag==0)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
            flag=0;
    }

}



测试数据


/*
2 1 4
G 10 5
R 6 4

3 1 5
H 4 5
G 10 5
C 7 5

2 1 5
R 4 4
G 10 5

3 1 5
G 10 4
R 5 5
H 3 7

4 1 5
G 10 4
C 6 5
H 5 5
R 1 1

5 1 5
G 10 4
C 6 5
H 5 5
H 4 5
R 1 1

3 1 5
G 10 4
C 2 7
H 3 7

3 1 5
G 10 4
R 5 5
R 1 6

4 1 5
G 10 4
R 5 5
R 1 6
H 3 7

0 0 0
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值