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;
}
复制代码

 

posted on   itdef  阅读(201)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示