UVA 10142 将军

来源:互联网 发布:windows脚本 编辑:程序博客网 时间:2024/04/30 02:33

写得比较繁琐

#include <iostream>#include <sstream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <vector>#include <set>#include <cctype>#include <algorithm>#include <cmath>#include <deque>#include <queue>#include <map>#include <stack>#include <list>#include <iomanip>using namespace std;///#define INF 0xffffff7#define maxn 1010///char board[20][20];int drow[] = {-1, -1, 0, 1, 1, 1,  0, -1};   int dcol[] = { 0,  1, 1, 1, 0,-1, -1, -1};int ddrow[] = {-2, -2, -1, 1, 2, 2, 1, -1};int ddcol[] = {-1,  1,  2, 2, 1, -1,-2,-2};//int dxrow[] = {1, 1, -1, -1};//int dxcol[] = {1, -1, 1, -1};bool checkB(int y, int x){int i, j;//卒if (y + 1 <= 8){if (x - 1 > 0){if (board[y + 1][x - 1] == 'P')return true;}if (x + 1 <= 8){if (board[y + 1][x + 1] == 'P')return true;}}//王for (i = 0; i < 8; i++){int tempY = y + drow[i];int tempX = x + dcol[i];if (tempY > 0 && tempY <= 8 && tempX > 0 && tempX <= 8){if (board[tempY][tempX] == 'K')return true;}}//马for (i = 0; i < 8; i++){int tempY = y + ddrow[i];int tempX = x + ddcol[i];if (tempY > 0 && tempY <= 8 && tempX > 0 && tempX <= 8){if (board[tempY][tempX] == 'N')return true;}}//车和后for (i = y + 1; i <= 8; i++){if (board[i][x] == 'Q' || board[i][x] == 'R')return true;if (board[i][x] != '.')break;}for (i = y - 1; i > 0; i--){if (board[i][x] == 'Q' || board[i][x] == 'R')return true;if (board[i][x] != '.')break;}for (i = x + 1; i <= 8; i++){if (board[y][i] == 'Q' || board[y][i] == 'R')return true;if (board[y][i] != '.')break;}for (i = x - 1; i > 0; i--){if (board[y][i] == 'Q' || board[y][i] == 'R')return true;if (board[y][i] != '.')break;}//象和后for (i = y + 1, j = x + 1; i <= 8 && j <= 8; i++, j++){if (board[i][j] == 'Q' || board[i][j] == 'B')return true;if (board[i][j] != '.')break;}for (i = y - 1, j = x + 1; i > 0  && j <= 8; i--, j++){if (board[i][j] == 'Q' || board[i][j] == 'B')return true;if (board[i][j] != '.')break;}for (i = y - 1, j = x - 1; i > 0  && j > 0; i--, j--){if (board[i][j] == 'Q' || board[i][j] == 'B')return true;if (board[i][j] != '.')break;}for (i = y + 1, j = x - 1; i <= 8 && j > 0; i++, j--){if (board[i][j] == 'Q' || board[i][j] == 'B')return true;if (board[i][j] != '.')break;}return false;}bool checkW(int y, int x){int i, j;//卒if (y - 1 > 0){if (x - 1 > 0){if (board[y - 1][x - 1] == 'p')return true;}if (x + 1 <= 8){if (board[y - 1][x + 1] == 'p')return true;}}//王for (i = 0; i < 8; i++){int tempY = y + drow[i];int tempX = x + dcol[i];if (tempY > 0 && tempY <= 8 && tempX > 0 && tempX <= 8){if (board[tempY][tempX] == 'k')return true;}}//马for (i = 0; i < 8; i++){int tempY = y + ddrow[i];int tempX = x + ddcol[i];if (tempY > 0 && tempY <= 8 && tempX > 0 && tempX <= 8){if (board[tempY][tempX] == 'n')return true;}}//车和后for (i = y + 1; i <= 8; i++){if (board[i][x] == 'q' || board[i][x] == 'r')return true;if (board[i][x] != '.')break;}for (i = y - 1; i > 0; i--){if (board[i][x] == 'q' || board[i][x] == 'r')return true;if (board[i][x] != '.')break;}for (i = x + 1; i <= 8; i++){if (board[y][i] == 'q' || board[y][i] == 'r')return true;if (board[y][i] != '.')break;}for (i = x - 1; i > 0; i--){if (board[y][i] == 'q' || board[y][i] == 'r')return true;if (board[y][i] != '.')break;}//象和后for (i = y + 1, j = x + 1; i <= 8 && j <= 8; i++, j++){if (board[i][j] == 'q' || board[i][j] == 'b')return true;if (board[i][j] != '.')break;}for (i = y - 1, j = x + 1; i > 0  && j <= 8; i--, j++){if (board[i][j] == 'q' || board[i][j] == 'b')return true;if (board[i][j] != '.')break;}for (i = y - 1, j = x - 1; i > 0  && j > 0; i--, j--){if (board[i][j] == 'q' || board[i][j] == 'b')return true;if (board[i][j] != '.')break;}for (i = y + 1, j = x - 1; i <= 8 && j > 0; i++, j--){if (board[i][j] == 'q' || board[i][j] == 'b')return true;if (board[i][j] != '.')break;}return false;}int main(){///int i, j;int cases = 1;int wKingX, wKingY, bKingX, bKingY;bool isIncheckW, isIncheckB;while (1){char temp[20];wKingY = wKingX = bKingX = bKingY = 0;isIncheckW = isIncheckB = false;//读入棋盘中白王和黑王的位置gets(board[1] + 1);if (!strcmp(board[1] + 1, ""))break;for (i = 2; i <= 8; i++){gets(board[i] + 1);}gets(temp);int cnt = 0;    //读取王的位置for (i = 1; i <= 8; i++){for (j = 1; j <= 8; j++){if (board[i][j] == 'k'){bKingY = i;bKingX = j;cnt++;}if (board[i][j] == 'K'){wKingY = i;wKingX = j;cnt++;}if (cnt == 2)break;}if (cnt == 2)break;}if (cnt == 0)break;//判读是否受到攻击isIncheckB = checkB(bKingY, bKingX);if (!isIncheckB){isIncheckW = checkW(wKingY, wKingX);}//输出结果if ((!isIncheckW) && !(isIncheckB))printf("Game #%d: no king is in check.\n", cases);if (isIncheckB)printf("Game #%d: black king is in check.\n", cases);if (isIncheckW)printf("Game #%d: white king is in check.\n", cases);cases++;}    ///    return 0;}


 

原创粉丝点击