1. 程式人生 > >UVA10196 Check The Check【模擬+回溯】

UVA10196 Check The Check【模擬+回溯】

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 vertically, 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):

Pawn   Rook   Bishop  Queen   King   Knight

........ ...*.... .......* ...*...* ........ ........

........ ...*.... *.....*. *..*..*. ........ ........

........ ...*.... .*...*.. .*.*.*.. ........ ..*.*...

........ ...*.... ..*.*... ..***... ..***... .*...*..

...p.... ***r**** ...b.... ***q**** ..*k*... ...n....

..*.*... ...*.... ..*.*... ..***... ..***... .*...*..

........ ...*.... .*...*.. .*.*.*.. ........ ..*.*...

........ ...*.... *.....*. *..*..*. ........ ........

  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

..k.....

ppp.pppp

........

.R...B..

........

........

PPPPPPPP

K.......

rnbqkbnr

pppppppp

........

........

........

........

PPPPPPPP

RNBQKBNR

rnbqk.nr

ppp..ppp

....p...

...p....

.bPP....

.....N..

PP..PPPP

RNBQKB.R

........

........

........

........

........

........

........

........

Sample Output

Game #1: black king is in check.

Game #2: no king is in check.

Game #3: white king is in check.

問題簡述

  模擬題,判斷國際象棋棋盤上的局面是否被將軍。

問題分析

  按照題意進行模擬即可,需要程式的細節,儘可能把程式寫簡潔。

程式說明:(略)

題記:(略)

參考連結:(略)

AC的C++語言程式如下:

/* UVA10196 Check The Check */

#include <bits/stdc++.h>

using namespace std;

const int ZERO = 0;
const int WHITE = 1;
const int BLACK = -1;

const int drow[] = {1, 1, -1, -1, 1, 0, -1, 0, 1, 1, -1, -1, 2, 2, -2, -2};
const int dcol[] = {1, -1, 1, -1, 0, 1, 0, -1, 2, -2, 2, -2, 1, -1, 1, -1};

const int N = 8;
char board[N][N];

// 將軍檢查
bool judge(int row, int col, char k, int start, int end)
{
    for(int i = start; i <= end; i++) {
        int nextrow = row + drow[i];
        int nextcol = col + dcol[i];
        if(nextrow >= 0 && nextrow < N && nextcol >= 0 && nextcol < N)
            if(board[nextrow][nextcol] == k)
                return 1;
    }
    return 0;
}

// 迴圈判斷
int judge2( int row, int col, char k, int start, int end )
{
    for ( int i = start ; i <= end ; i++ ) {
        int nextrow = row+drow[i];
        int nextcol = col+dcol[i];
        while(nextrow >= 0 && nextrow < N && nextcol >= 0 && nextcol < N) {
            if(board[nextrow][nextcol] == k)
                return 1;
            if(board[nextrow][nextcol] != '.')
                break;
            nextrow += drow[i];
            nextcol += dcol[i];
        }
    }
    return 0;
}

//兵
int pawn( int x, int y, char k )
{
    if(k == 'K')
        return judge(x, y, k, 0, 1);
    else // if(k == 'k')
        return judge(x, y, k, 2, 3);
}

//馬
int knight( int row, int col, char k )
{
    return judge( row, col, k, 8, 15 );
}

//象
int bishop( int row, int col, char k )
{
    return judge2( row, col, k, 0, 3 );
}

//車
int rook( int row, int col, char k )
{
    return judge2( row, col, k, 4, 7 );
}

//後
int queen( int row, int col, char k )
{

    return judge2( row, col, k, 0, 7 );
}

void deal()
{
    for(int i = 0; i < N; i++)
        for(int j = 0; j < N; j++) {
            int  flag = ZERO;
            switch(board[i][j]) {
                case 'p' : if(pawn(i, j, 'K')) flag = WHITE; break;
                case 'P' : if(pawn(i, j, 'k')) flag = BLACK; break;
                case 'n' : if(knight(i, j, 'K')) flag = WHITE; break;
                case 'N' : if(knight(i, j, 'k')) flag = BLACK; break;
                case 'b' : if(bishop(i, j, 'K')) flag = WHITE; break;
                case 'B' : if(bishop(i, j, 'k')) flag = BLACK; break;
                case 'r' : if(rook(i, j, 'K')) flag = WHITE; break;
                case 'R' : if(rook(i, j, 'k')) flag = BLACK; break;
                case 'q' : if(queen(i, j, 'K')) flag = WHITE; break;
                case 'Q' : if(queen(i, j, 'k')) flag = BLACK; break;
                default : break;
            }
            if(flag) {
                if(flag == WHITE)
                    printf("white king is in check.\n");
                else if(flag == BLACK)
                    printf("black king is in check.\n");
                return;
            }
        }

    printf("no king is in check.\n");
}

int main()
{
    int caseno = 0;

    for(;;) {
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                cin >> board[i][j];

        int cnt = 0;
        for(int i = 0; i < N; i++)
            for(int j = 0; j < N; j++)
                if(board[i][j] == '.')
                    cnt++;
        if(cnt == N * N)
            break;

        printf("Game #%d: ", ++caseno);
        deal();
    }

    return 0;
}