UVa 문제 10196 Check the Check (장군)
// 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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Docker를 사용한 React 및 .NET Core 6.0 샘플 프로젝트 - 1부이 기사에서는 Entity Framework Core Code First 접근 방식을 사용하는 ASP.NET Core 6.0 WEP API의 CRUD(만들기, 읽기, 업데이트 및 삭제) 작업에 대해 설명합니다. 웹 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.