10196 - Check The Check

题目

Time limit: 3.000 seconds

Your task is to write a program that reads a chess board configuration and answers if there’s a king under attack (i.e. “in check”). A king is in check if it’s in a square which is attacked by an oponnet’s piece (i.e. it’s in square which can be taken by an oponnet’s piece in his next move).
White pieces will be represented by uppercase letters whereas black pieces will be represented by lowercase letters. White side will always be on the bottom of the board and black side will always be on the top of the board.
For those unfamiliar with chess, here are the movements of each piece:
Pawn (p or P): can only move straight ahead, one square at a time. But it takes pieces diagonally (and that’s what concerns to you in this problem).
Knight (n or N) : have a special movement and it’s the only piece that can jump over other pieces. The knight movement can be viewed as an “L”. See the example bellow.
Bishop (b or B) : can move any number of squares diagonally (forward or backward).
Rook (r or R) : can move any number of squares vertically or horizontally (forward or backward).
Queen (q or Q) : can move any number of squares in any direction (diagonally, horizontally or verti- cally, forward or backward).
King (k or K) : can move one square at a time, in any direction (diagonally, horizontally or vertically, forward or backward).
Movements examples (‘*’ indicates where the piece can take another pieces):
这里写图片描述
Remember that the knight is the only piece that can jumper over other pieces. The pawn movement will depend on its side. If it’s a black pawn, it can only move one square diagonally down the board. If it’s a white pawn, it can only move one square diagonally up the board. The example above is a black pawn as it’s a lowercase ‘p’ (we say “move” meaning the squares where the pawn can move to when it takes another piece).

Input
There will be an arbitrary number of board configurations on the input. Each board will consist of 8 lines of 8 characters each. A ‘.’ character will represent an empty square. Upper and lower case letters (as defined above) will represent the pieces. There will be no invalid characters (i.e. pieces) and there won’t be a configuration where both kings are in check. You must read until you find an empty board (i.e. a board that is formed only of ‘.’ characters) which should not be processed. There will be an empty line between each pair of board configurations. In all boards (except the last one which is empty) will appear both the white king and the black king (one, and only one of each).

Output
For each board configuration read you must output one of the following answers:
Game #d: white king is in check. Game #d: black king is in check. Game #d: no king is in check.
Where d stands for the game number (starting from 1).

Sample Input
这里写图片描述

Sample Output
Game #1: black king is in check.
Game #2: no king is in check.
Game #3:white king is in check.


解题思路

1. 固定好一个king,然后检查所有可能攻击到它的位置上是否有对手的棋子
2. 只有马可以越过其它棋子


通过代码

#include<stdio.h>

bool king;
bool KING;
char board[8][8];

bool empty(void){
    for(int i=0;i<8;++i)
        for(int j=0;j<8;++j)
            if(board[i][j]!='.')
                return false;
    return true;
}


void check_white(int x,int y){
    //pawn,king
    if(x-1>=0 && y-1>=0)
        if(board[x-1][y-1]=='p' || board[x-1][y-1]=='k')
            KING=true;  

    if(x-1>=0 && y+1<8)
        if(board[x-1][y+1]=='p' || board[x-1][y+1]=='k')
            KING=true;

    if(x-1>=0 && board[x-1][y]=='k')
        KING=true;

    if(x+1<8 && board[x+1][y]=='k')
        KING=true;

    if(y-1>=0 && board[x][y-1]=='k')
        KING=true;

    if(y+1<8 && board[x][y+1]=='k')
        KING=true;

    if(x+1<8 && y-1>=0 && board[x+1][y-1]=='k')
        KING=true;

    if(x+1<8 && y+1<8 && board[x+1][y+1]=='k')
        KING=true;

    //knight
    if(x-1>=0 && y-2>=0 && board[x-1][y-2]=='n')
        KING=true;

    if(x-2>=0 && y-1>=0 && board[x-2][y-1]=='n')
        KING=true;

    if(x-2>=0 && y+1<8 && board[x-2][y+1]=='n')
        KING=true;

    if(x-1>=0 && y+2<8 && board[x-1][y+2]=='n')
        KING=true;

    if(x+1<8 && y-2>=0 && board[x+1][y-2]=='n')
        KING=true;

    if(x+2<8 && y-1>=0 && board[x+2][y-1]=='n')
        KING=true;

    if(x+1<8 && y+2<8 && board[x+1][y+2]=='n')
        KING=true;

    if(x+2<8 && y+1<8 && board[x+2][y+1]=='n')
        KING=true;

    //rook,queen
    bool piece=false;
    int i=x;
    int j=y;
    while(--j>=0 && !piece)
        if(board[i][j]=='r' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++j<8 && !piece)
        if(board[i][j]=='r' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(--i>=0 && !piece)
        if(board[i][j]=='r' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && !piece)
        if(board[i][j]=='r' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    //bishop,queen
    piece=false;
    i=x;
    j=y;
    while(--i>=0 && --j>=0 && !piece)
        if(board[i][j]=='b' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(--i>=0 && ++j<8 && !piece)
        if(board[i][j]=='b' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && --j>=0 && !piece)
        if(board[i][j]=='b' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && ++j<8 && !piece)
        if(board[i][j]=='b' || board[i][j]=='q')
            KING=true;
        else if(board[i][j]!='.')
            piece=true;
}


void check_black(int x,int y){
    //PAWN,KING
    if(x-1>=0 && y-1>=0 && board[x-1][y-1]=='K')
        king=true;

    if(x-1>=0 && y+1<8 && board[x-1][y+1]=='K')
        king=true;

    if(x-1>=0 && board[x-1][y]=='K')
        king=true;

    if(x+1<8 && board[x+1][y]=='K')
        king=true;

    if(y-1>=0 && board[x][y-1]=='K')
        king=true;

    if(y+1<8 && board[x][y+1]=='K')
        king=true;

    if(x+1<8 && y-1>=0)
        if(board[x+1][y-1]=='K' || board[x+1][y-1]=='P')
            king=true;

    if(x+1<8 && y+1<8)
        if(board[x+1][y+1]=='K' || board[x+1][y+1]=='P')
        king=true;

    //KNIGHT
    if(x-1>=0 && y-2>=0 && board[x-1][y-2]=='N')
        king=true;

    if(x-2>=0 && y-1>=0 && board[x-2][y-1]=='N')
        king=true;

    if(x-2>=0 && y+1<8 && board[x-2][y+1]=='N')
        king=true;

    if(x-1>=0 && y+2<8 && board[x-1][y+2]=='N')
        king=true;

    if(x+1<8 && y-2>=0 && board[x+1][y-2]=='N')
        king=true;

    if(x+2<8 && y-1>=0 && board[x+2][y-1]=='N')
        king=true;

    if(x+1<8 && y+2<8 && board[x+1][y+2]=='N')
        king=true;

    if(x+2<8 && y+1<8 && board[x+2][y+1]=='N')
        king=true;

    //ROOK,QUEEN
    bool piece=false;
    int i=x;
    int j=y;
    while(--j>=0 && !piece)
        if(board[i][j]=='R' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++j<8 && !piece)
        if(board[i][j]=='R' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(--i>=0 && !piece)
        if(board[i][j]=='R' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && !piece)
        if(board[i][j]=='R' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    //BISHOP,QUEEN
    piece=false;
    i=x;
    j=y;
    while(--i>=0 && --j>=0 && !piece)
        if(board[i][j]=='B' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(--i>=0 && ++j<8 && !piece)
        if(board[i][j]=='B' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && --j>=0 && !piece)
        if(board[i][j]=='B' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;

    piece=false;
    i=x;
    j=y;
    while(++i<8 && ++j<8 && !piece)
        if(board[i][j]=='B' || board[i][j]=='Q')
            king=true;
        else if(board[i][j]!='.')
            piece=true;
}



int main(){
    int N=0;

#ifdef DEBUG
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif

    while(true){
        for(int i=0;i<8;++i)
            scanf("%s",board[i]);

        if(empty())
            break;

        N++;
        king=false;
        KING=false;

        bool end=false;
        bool END=false;
        for(int i=0;i<8 && !(end &&  END);++i)
            for(int j=0;j<8 && !(end && END);++j)
                if(board[i][j]=='K'){
                    check_white(i,j);
                    END=true;
                }
                else if(board[i][j]=='k'){
                    check_black(i,j);
                    end=true;
                }

        if(king==true)
            printf("Game #%d: black king is in check.\n",N); 
        else if(KING==true)
            printf("Game #%d: white king is in check.\n",N); 
        else
            printf("Game #%d: no king is in check.\n",N);
    }
    return 0;
}


运行截图


这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值