• uva 10196 将军 模拟


     

    代码 

    // 11235.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    //
    
    #include <iostream>
    #include <vector>
    #include <string>
    
    using namespace std;
    
    
    /*
    ..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.
    */
    
    vector<string> board;
    
    int horseX[8] = { 2,2,-2,-2,1,1,-1,-1 };
    int horseY[8] = { 1,-1,1,-1,2,2,-2,-2 };
    
    int slantX[4] = {-1,1,1,-1};
    int slantY[4] = {-1,1,-1,1};
    
    int lineX[4] = { 1,-1,0,0 };
    int lineY[4] = { 0,0,1,-1 };
    
    bool CheckCheck(int x, int y)
    {
        if (board[x][y] != 'k' && board[x][y] != 'K') return false;
        //检查王旁边有没马
        for (int i = 0; i < 8; i++) {
            int newx = x + horseX[i];
            int newy = y + horseY[i];
    
            if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) {
                if (board[x][y] == 'k' &&  board[newx][newy] == 'H') {
                    return true;
                }
                else if ( board[x][y] == 'K' &&  board[newx][newy] == 'h') {
                    return true;
                }
            }
        }
    
        //检测斜线有无
    
        for (int i = 0; i < 4; i++) {
            int newx = x; int newy = y;
            while (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) {
                newx = newx + slantX[i];
                newy = newy + slantY[i];
                int firstStep = 1;
                if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) {
                    if (board[newx][newy] != '.') {
                        if (board[x][y] == 'k' &&
                            (board[newx][newy] == 'Q' || board[newx][newy] == 'B'))
                        {
                            return true;
                        }
                        else if (board[x][y] == 'K' &&
                            (board[newx][newy] == 'q' || board[newx][newy] == 'b'))
                        {
                            return true;
                        }
    
                        if (firstStep == 1 && (board[newx][newy] == 'k' || board[newx][newy] == 'K')) return true;
    
                        if (firstStep == 1 && board[x][y] == 'k' &&
                            board[newx][newy] == 'P' && (newx - x) == 1)
                        {
                            return true;
                        }
                        if (firstStep == 1 && board[x][y] == 'K' &&
                            board[newx][newy] == 'p' && (x - newx) == 1)
                        {
                            return true;
                        }
                        break;
                    }
                    firstStep = 0;
                }
            }
        }
    
        
        //检测横线有无
        for (int i = 0; i < 4; i++) {
            int newx = x; int newy = y;
            while (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) {
                newx = newx + lineX[i];
                newy = newy + lineY[i];
                int firstStep = 1;
                if (newx >= 0 && newx < 8 && newy >= 0 && newy < 8) {
                    if (board[newx][newy] != '.') {
                        if (board[x][y] == 'k' &&
                            (board[newx][newy] == 'Q' || board[newx][newy] == 'R'))
                        {
                            return true;
                        }
                        else if (board[x][y] == 'K' &&
                            (board[newx][newy] == 'q' || board[newx][newy] == 'r'))
                        {
                            return true;
                        }
    
                        if (firstStep == 1 && (board[newx][newy] == 'k' || board[newx][newy] == 'K')) return true;
    
                        if (firstStep == 1 && board[x][y] == 'k' &&
                            board[newx][newy] == 'P' && (newx - x) == 1)
                        {
                            return true;
                        }
                        if (firstStep == 1 && board[x][y] == 'K' &&
                            board[newx][newy] == 'p' && (x - newx) == 1)
                        {
                            return true;
                        }
                        break;
                    }
                    firstStep = 0;
                }
            }
        }
    
    
        return false;
    }
    
    int main()
    {
        int idx = 0;
        while (1) {
            string s = "no king is in check.";
            board.clear();
            int isFinish = 1;
            for (int i = 0; i < 8; i++) {
                string s;
                cin >> s;
                board.push_back(s);
                if (s != "........") isFinish = 0;
            }
            if (1 == isFinish) break;
    
            for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                    if(CheckCheck(i,j) == 1){
                        if (board[i][j] == 'K') {
                            s = "white king is in check.";
                        }else{
                            s = "black king is in check.";
                        }
                        goto RESULT;
                    }
                }
            }
        RESULT:
            idx++;
            cout << "Game #" << idx << ": " << s << endl;
        }
    
        return 0;
    }
    作 者: itdef
    欢迎转帖 请保持文本完整并注明出处
    技术博客 http://www.cnblogs.com/itdef/
    B站算法视频题解
    https://space.bilibili.com/18508846
    qq 151435887
    gitee https://gitee.com/def/
    欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
    如果觉得不错,欢迎点赞,你的鼓励就是我的动力
    阿里打赏 微信打赏
  • 相关阅读:
    2017 ACM-ICPC, Universidad Nacional de Colombia Programming Contest gym101466 题解
    Codeforces Round #634 (Div. 3)题解
    AtCoder Beginner Contest 162 题解
    2018-2019 ACM-ICPC, Asia Nakhon Pathom Regional Contest-gym 102091 题解
    多线程资料
    文件自动导入
    GWT+CodeTemplate+TableCreate快速开发
    阅读大神博客笔记
    Java数据类型
    Java断言assert
  • 原文地址:https://www.cnblogs.com/itdef/p/13299281.html
Copyright © 2020-2023  润新知