uva 10196 将军 模拟

uva 10196 将军 模拟

 

uva 10196 将军 模拟

 

uva 10196 将军 模拟

 

 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;
}

 

上一篇:acwing 189. 乳草的入侵 bfs


下一篇:DFS-深度优先搜索