UVA 10142 将军

写得比较繁琐

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值