UVa Problem 10196 Check the Check (将军)

  1. // Check the Check (将军)   
  2. // PC/UVa IDs: 110107/10196, Popularity: B, Success rate: average Level: 1   
  3. // Verdict: Accepted   
  4. // Submission Date: 2011-05-22   
  5. // UVa Run Time: 0.008s   
  6. //   
  7. // 版权所有(C)2011,邱秋。metaphysis # yeah dot net   
  8.       
  9. #include <iostream>   
  10. #include <cstdlib>   
  11.       
  12. using namespace std;  
  13.       
  14. char status[8][8];  // 表示国际象棋棋子的位置。   
  15.       
  16. // 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的卒攻击范围内,注意进攻方向的不同。   
  17. bool checkP (int x, int y, int kingX, int kingY)  
  18. {  
  19.     return (x - kingX) == 1 && abs(y - kingY) == 1;  
  20. }  
  21.       
  22. // 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的马攻击范围内。   
  23. bool checkN (int x, int y, int kingX, int kingY)  
  24. {  
  25.     return (abs(x - kingX) == 2 && abs(y - kingY) == 1) ||  
  26.             (abs(x - kingX) == 1 && abs(y - kingY) == 2);  
  27. }  
  28.       
  29. // 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的象攻击范围内。   
  30. bool checkB (int x, int y, int kingX, int kingY)  
  31. {  
  32.     int step, currentX = x, currentY = y, directX, directY;  
  33.     bool checked = false;  
  34.       
  35.     // 检查象与王是否处于对角线。   
  36.     if (abs(x - kingX) == abs(y - kingY))  
  37.     {  
  38.         checked = true;  
  39.           
  40.         // 判断相对方位。   
  41.         directX = (x < kingX) ? 1 : -1;  
  42.         directY = (y < kingY) ? 1 : -1;  
  43.       
  44.         // 是否有棋子相隔。   
  45.         step = abs (x - kingX);  
  46.         while (step > 1)  
  47.         {  
  48.             currentX += directX;  
  49.             currentY += directY;  
  50.             if (status[currentX][currentY] != '.')  
  51.             {  
  52.                 checked = false;  
  53.                 break;  
  54.             }  
  55.             step--;  
  56.         }  
  57.     }  
  58.     return checked;  
  59. }  
  60.       
  61. // 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的车攻击范围内。   
  62. bool checkR (int x, int y, int kingX, int kingY)  
  63. {  
  64.     int step, currentX = x, currentY = y, directX = 0, directY = 0;  
  65.     bool checked = false;  
  66.       
  67.     // 车与王是否在同一行或者同一列,车与王之间是否有棋子相隔。   
  68.     if (x == kingX || y == kingY)  
  69.     {  
  70.         checked = true;  
  71.           
  72.         // 判断相对方位。   
  73.         directX = (x == kingX) ? (0) : (x < kingX ? 1 : -1);  
  74.         directY = (y == kingY) ? (0) : (y < kingY ? 1 : -1);  
  75.           
  76.         // 是否有棋子相隔。   
  77.         step = (directX == 0) ? abs(y - kingY) : abs(x - kingX);  
  78.         while (step > 1)  
  79.         {  
  80.             currentX += directX;  
  81.             currentY += directY;  
  82.             if (status[currentX][currentY] != '.')  
  83.             {  
  84.                 checked = false;  
  85.                 break;  
  86.             }  
  87.             step--;  
  88.         }  
  89.     }  
  90.     return checked;  
  91. }  
  92.       
  93. // 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的王后攻击范围内。   
  94. bool checkQ (int x, int y, int kingX, int kingY)  
  95. {  
  96.     return checkR(x, y, kingX, kingY) || checkB(x, y, kingX, kingY);  
  97. }  
  98.       
  99. // 检查给定棋盘状态是否存在将军局面。   
  100. void check (int gameCount)  
  101. {  
  102.     bool bChecked = false, wChecked = false;  
  103.     int bKingX = -1, bKingY = -1, wKingX = -1, wKingY = -1;  
  104.     int directX = 0, directY = 0, currentX = 0, currentY = 0, step = 0;  
  105.       
  106.     // 查找白棋和黑棋王的位置。   
  107.     for (int i = 0; i < 8; i++)  
  108.         for (int j = 0; j < 8; j++)  
  109.         {  
  110.             if (status[i][j] == 'k')  
  111.             {  
  112.                 bKingX = i;  
  113.                 bKingY = j;  
  114.             }  
  115.             if (status[i][j] == 'K')  
  116.             {  
  117.                 wKingX = i;  
  118.                 wKingY = j;  
  119.             }  
  120.         }  
  121.           
  122.     // 假如未找到王的坐标,表明为空棋盘。   
  123.     if (bKingX == -1)  
  124.         return;  
  125.           
  126.     cout << "Game #" << gameCount << ": ";  
  127.       
  128.     // 检查棋子与王的是否存在将军情况。   
  129.     for (int i = 0; i < 8; i++)  
  130.         for (int j = 0; j < 8; j++)  
  131.         {  
  132.             switch (status[i][j])  
  133.             {  
  134.                 // 黑卒。   
  135.                 case 'p':  
  136.                     wChecked = checkP(wKingX, wKingY, i, j);  
  137.                     break;  
  138.                 // 白卒。   
  139.                 case 'P':  
  140.                     bChecked = checkP(i, j, bKingX, bKingY);  
  141.                     break;  
  142.                 // 黑马。   
  143.                 case 'n':  
  144.                     wChecked = checkN(i, j, wKingX, wKingY);  
  145.                     break;  
  146.                 // 白马。   
  147.                 case 'N':  
  148.                     bChecked = checkN(i, j, bKingX, bKingY);  
  149.                     break;  
  150.                 // 黑象。   
  151.                 case 'b':  
  152.                     wChecked = checkB(i, j, wKingX, wKingY);  
  153.                     break;  
  154.                 // 白象。   
  155.                 case 'B':  
  156.                     bChecked = checkB(i, j, bKingX, bKingY);  
  157.                     break;  
  158.                 // 黑车。   
  159.                 case 'r':  
  160.                     wChecked = checkR(i, j, wKingX, wKingY);  
  161.                     break;  
  162.                 // 白车。   
  163.                 case 'R':  
  164.                     bChecked = checkR(i, j, bKingX, bKingY);  
  165.                     break;  
  166.                 // 黑后。   
  167.                 case 'q':  
  168.                     wChecked = checkQ(i, j, wKingX, wKingY);  
  169.                     break;  
  170.                 // 白后。   
  171.                 case 'Q':  
  172.                     bChecked = checkQ(i, j, bKingX, bKingY);  
  173.                     break;  
  174.                 // 其他情况。   
  175.                 default:  
  176.                     break;  
  177.             }  
  178.               
  179.             // 检查将军情况是否存在。   
  180.             if (bChecked)  
  181.             {  
  182.                 cout << "black king is in check." << endl;  
  183.                 return;  
  184.             }  
  185.             if (wChecked)  
  186.             {  
  187.                 cout << "white king is in check." << endl;  
  188.                 return;  
  189.             }  
  190.         }  
  191.       
  192.     cout << "no king is in check." << endl;  
  193. }  
  194.       
  195. int main (int ac, char *av[])  
  196. {  
  197.     string line;  
  198.     int gameCount = 1, temp = 0;  
  199.       
  200.     while (getline(cin, line))  
  201.     {  
  202.         if (line != "")  
  203.         {  
  204.             for (int i = 0; i < 8; i++)  
  205.                 status[temp][i] = line[i];  
  206.             temp++;  
  207.         }  
  208.         else  
  209.         {  
  210.             check(gameCount);  
  211.             gameCount++;  
  212.             temp = 0;  
  213.         }  
  214.     }  
  215.       
  216.     return 0;  
  217. }  
// Check the Check (将军)
// PC/UVa IDs: 110107/10196, Popularity: B, Success rate: average Level: 1
// Verdict: Accepted
// Submission Date: 2011-05-22
// UVa Run Time: 0.008s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
	
#include <iostream>
#include <cstdlib>
	
using namespace std;
	
char status[8][8];	// 表示国际象棋棋子的位置。
	
// 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的卒攻击范围内,注意进攻方向的不同。
bool checkP (int x, int y, int kingX, int kingY)
{
	return (x - kingX) == 1 && abs(y - kingY) == 1;
}
	
// 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的马攻击范围内。
bool checkN (int x, int y, int kingX, int kingY)
{
	return (abs(x - kingX) == 2 && abs(y - kingY) == 1) ||
			(abs(x - kingX) == 1 && abs(y - kingY) == 2);
}
	
// 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的象攻击范围内。
bool checkB (int x, int y, int kingX, int kingY)
{
	int step, currentX = x, currentY = y, directX, directY;
	bool checked = false;
	
	// 检查象与王是否处于对角线。
	if (abs(x - kingX) == abs(y - kingY))
	{
		checked = true;
		
		// 判断相对方位。
		directX = (x < kingX) ? 1 : -1;
		directY = (y < kingY) ? 1 : -1;
	
		// 是否有棋子相隔。
		step = abs (x - kingX);
		while (step > 1)
		{
			currentX += directX;
			currentY += directY;
			if (status[currentX][currentY] != '.')
			{
				checked = false;
				break;
			}
			step--;
		}
	}
	return checked;
}
	
// 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的车攻击范围内。
bool checkR (int x, int y, int kingX, int kingY)
{
	int step, currentX = x, currentY = y, directX = 0, directY = 0;
	bool checked = false;
	
	// 车与王是否在同一行或者同一列,车与王之间是否有棋子相隔。
	if (x == kingX || y == kingY)
	{
		checked = true;
		
		// 判断相对方位。
		directX = (x == kingX) ? (0) : (x < kingX ? 1 : -1);
		directY = (y == kingY) ? (0) : (y < kingY ? 1 : -1);
		
		// 是否有棋子相隔。
		step = (directX == 0) ? abs(y - kingY) : abs(x - kingX);
		while (step > 1)
		{
			currentX += directX;
			currentY += directY;
			if (status[currentX][currentY] != '.')
			{
				checked = false;
				break;
			}
			step--;
		}
	}
	return checked;
}
	
// 检查坐标为(kingX,kingY)的王是否在坐标为(x,y)的王后攻击范围内。
bool checkQ (int x, int y, int kingX, int kingY)
{
	return checkR(x, y, kingX, kingY) || checkB(x, y, kingX, kingY);
}
	
// 检查给定棋盘状态是否存在将军局面。
void check (int gameCount)
{
	bool bChecked = false, wChecked = false;
	int bKingX = -1, bKingY = -1, wKingX = -1, wKingY = -1;
	int directX = 0, directY = 0, currentX = 0, currentY = 0, step = 0;
	
	// 查找白棋和黑棋王的位置。
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
		{
			if (status[i][j] == 'k')
			{
				bKingX = i;
				bKingY = j;
			}
			if (status[i][j] == 'K')
			{
				wKingX = i;
				wKingY = j;
			}
		}
		
	// 假如未找到王的坐标,表明为空棋盘。
	if (bKingX == -1)
		return;
		
	cout << "Game #" << gameCount << ": ";
	
	// 检查棋子与王的是否存在将军情况。
	for (int i = 0; i < 8; i++)
		for (int j = 0; j < 8; j++)
		{
			switch (status[i][j])
			{
				// 黑卒。
				case 'p':
					wChecked = checkP(wKingX, wKingY, i, j);
					break;
				// 白卒。
				case 'P':
					bChecked = checkP(i, j, bKingX, bKingY);
					break;
				// 黑马。
				case 'n':
					wChecked = checkN(i, j, wKingX, wKingY);
					break;
				// 白马。
				case 'N':
					bChecked = checkN(i, j, bKingX, bKingY);
					break;
				// 黑象。
				case 'b':
					wChecked = checkB(i, j, wKingX, wKingY);
					break;
				// 白象。
				case 'B':
					bChecked = checkB(i, j, bKingX, bKingY);
					break;
				// 黑车。
				case 'r':
					wChecked = checkR(i, j, wKingX, wKingY);
					break;
				// 白车。
				case 'R':
					bChecked = checkR(i, j, bKingX, bKingY);
					break;
				// 黑后。
				case 'q':
					wChecked = checkQ(i, j, wKingX, wKingY);
					break;
				// 白后。
				case 'Q':
					bChecked = checkQ(i, j, bKingX, bKingY);
					break;
				// 其他情况。
				default:
					break;
			}
			
			// 检查将军情况是否存在。
			if (bChecked)
			{
				cout << "black king is in check." << endl;
				return;
			}
			if (wChecked)
			{
				cout << "white king is in check." << endl;
				return;
			}
		}
	
	cout << "no king is in check." << endl;
}
	
int main (int ac, char *av[])
{
	string line;
	int gameCount = 1, temp = 0;
	
	while (getline(cin, line))
	{
		if (line != "")
		{
			for (int i = 0; i < 8; i++)
				status[temp][i] = line[i];
			temp++;
		}
		else
		{
			check(gameCount);
			gameCount++;
			temp = 0;
		}
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值