1. 程式人生 > 實用技巧 >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; }