zoukankan      html  css  js  c++  java
  • UVA

    类型:简单模拟

    大致题意:已知国际象棋行棋规则,给你一个局面,问是否将军?谁将谁的军?(保证不会同时将军)

    思路:都以小写字母 测试 是否将 大写字母。 然后一个局面测两次(一次直接测,一次反转棋盘,同时大小写互换,测)

    原题:

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21173

    The Problem
    
    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
    ........
    ........
    ........
    ........
    ...p....
    ..*.*...
    ........
    ........
    
        
    
    Rook
    ...*....
    ...*....
    ...*....
    ...*....
    ***r****
    ...*....
    ...*....
    ...*....
    
        
    
    Bishop
    .......*
    *.....*.
    .*...*..
    ..*.*...
    ...b....
    ..*.*...
    .*...*..
    *.....*.
    
        
    
    Queen
    ...*...*
    *..*..*.
    .*.*.*..
    ..***...
    ***q****
    ..***...
    .*.*.*..
    *..*..*.
    
        
    
    King
    ........
    ........
    ........
    ..***...
    ..*k*...
    ..***...
    ........
    ........
    
        
    
    Knight
    ........
    ........
    ..*.*...
    .*...*..
    ...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).
    The 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).
    The 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.
    View Code

    代码:

    (!仅通过样例测试,未提交!)

    // 大量相同代码,用宏定义 少用复制。
    // 宏定义特点:写了两段发现除了个别参数不一样,其他完全一样,想复制。
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    char chessboard[10][10];
    char tmpboard[10][10];
    #define IN_BOARD(I,J) (0<=(I) && (I)<8 && 0<=(J) && (J)<8)
    
    #define CK(A,B) if (IN_BOARD(A,B) && (tmpboard[A][B] == 'K')) return true;
    bool checkP(int i, int j) {
        CK(i+1,j-1);
        CK(i+1,j+1);
        return false;
    }
    
    #define CK_LINE(A,B) 
    for (int k = 1; IN_BOARD(A,B); k++) { 
        if(tmpboard[A][B] == 'K') return true; 
        else if (tmpboard[A][B] != '.') break; 
    }
    bool checkR(int i, int j) {
        CK_LINE(i+k,j);
        CK_LINE(i-k,j);
        CK_LINE(i,j+k);
        CK_LINE(i,j-k);
        return false;
    }
    
    bool checkB(int i, int j) {
        CK_LINE(i+k,j+k);
        CK_LINE(i+k,j-k);
        CK_LINE(i-k,j+k);
        CK_LINE(i-k,j-k);
        return false;
    }
    
    bool checkQ(int i, int j) {
        if (checkB(i,j)) return true;
        if (checkR(i,j)) return true;
        return false;
    }
    
    bool checkK(int i, int j) {
        //检查k 是否将军K
        CK(i-1,j-1);
        CK(i-1,j);
        CK(i-1,j+1);
    
        CK(i,j-1);
        CK(i,j);
        CK(i,j+1);
    
        CK(i+1,j-1);
        CK(i+1,j);
        CK(i+1,j+1);
    
        return false;
    }
    
    bool checkN(int i, int j) {
        CK(i-2,j-1);
        CK(i-2,j+1);
        CK(i-1,j-2);
        CK(i-1,j+2);
    
        CK(i+1,j-2);
        CK(i+1,j+2);
        CK(i+2,j-1);
        CK(i+2,j+1);
    
        return false;
    }
    
    // 检查 小写字母 是否将军 大写字母
    bool isInCheck(){
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z') {
                    switch (tmpboard[i][j]) {
                        case 'p': if(checkP(i,j))return true;break;
                        case 'r': if(checkR(i,j))return true;break;
                        case 'b': if(checkB(i,j))return true;break;
                        case 'q': if(checkQ(i,j))return true;break;
                        case 'k': if(checkK(i,j))return true;break;
                        case 'n': if(checkN(i,j))return true;break;
                        default: puts("Error low char");break;
                    }
                }
            }
        }
        return false;
    }
    
    //读入棋盘,如果是空的返回false
    bool read() {
        for (int i = 0; i < 8; i++) {
            scanf("%s", chessboard[i]);
        }
        bool notEmpty = false;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (chessboard[i][j] != '.') notEmpty = true;
            }
        }
        return notEmpty;
    }
    
    int main() {
        int cas = 1;
        while (read())
        {
            printf("Game #%d: ", cas++);
    
            for (int i = 0; i < 8; i++)
            {
                sprintf(tmpboard[i], "%s", chessboard[i]);
            }
            if (isInCheck())
            {
                puts("white king is in check.");
                continue;
            }
    
    
            for (int i = 0; i < 8; i++)
            {
                sprintf(tmpboard[i], "%s", chessboard[7-i]);
                for (int j = 0; j < 8; j++)
                {
                    if ('a' <= tmpboard[i][j] && tmpboard[i][j] <= 'z')
                    {
                        tmpboard[i][j] = tmpboard[i][j] -'a'+'A';
                    } else if ('A' <= tmpboard[i][j] && tmpboard[i][j] <= 'Z')
                    {
                        tmpboard[i][j] = tmpboard[i][j] - 'A' + 'a';
                    }
                }
            }
            if (isInCheck())
            {
                puts("black king is in check.");
                continue;
            }
            puts("no king is in check.");
        }
        return 0;
    }
  • 相关阅读:
    转:Omnet++ 4.0 installation for Ubuntu
    转:myeclipse假死的解决方案
    omnet++ 4.0下使用XML的例子
    转:Microsoft JET Database Engine (0x80004005) 未指定的错误的完美解决
    C# 数据库删除操作错误报错 System.Data.SqlClient.SqlException (0x80131904)
    Windows 7 转移用户文件夹
    CentOS自动登录Gnome
    Archlinux GRUB2 配置
    Archlinux 登录管理器切换
    html2chm工具1.0发布
  • 原文地址:https://www.cnblogs.com/shinecheng/p/3578124.html
Copyright © 2011-2022 走看看