UVa 문제 10196 Check the Check (장군)

4473 단어 cDateString
// 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;
}

좋은 웹페이지 즐겨찾기