I have coded this two weeks ago and I still get WA for it.
I cant figure out where is the problem, because I got right answer for every testcases i have tested.
Coded in C++:
Code: Select all
// CheckTheCheck.cpp : Defines the entry point for the console application.
// UVA 10196
//#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
char board[8][8];
int caseNum = 1;
int status = 0; // 0 = no king in check. 1 = Black king in check. 2 = White king in check
bool checkTheCheck(int row, int col) {
switch (board[row][col]) {
case 'k': // Black king
if (row > 0) { // check up for rook or queen
for (int i = row - 1; i >= 0; i--) {
if (board[i][col] == 'R' || board[i][col] == 'Q') {
status = 1;
return true;
} else if (board[i][col] != '.') // reached a piece that is not king
break;
}
}
if (row < 7) { // check down for rook or queen
for (int i = row + 1; i <= 7; i++) {
if (board[i][col] == 'R' || board[i][col] == 'Q') {
status = 1;
return true;
} else if (board[i][col] != '.') // reached a piece that is not king
break;
}
}
if (col > 0) { // check left for rook or queen
for (int i = col - 1; i >= 0; i--) {
if (board[row][i] == 'R' || board[row][i] == 'Q') {
status = 1;
return true;
} else if (board[row][i] != '.') // reached a piece that is not king
break;
}
}
if (col < 7) { // check right for rook or queen
for (int i = col + 1; i <= 7; i++) {
if (board[row][i] == 'R' || board[row][i] == 'Q') {
status = 1;
return true;
} else if (board[row][i] != '.') // reached a piece that is not king
break;
}
}
//////////////////////////////// check for bishop or queen
if (row > 0 && col > 0) { // check up left
int temp = (row > col) ? col : row;
for (int i = 1; i <= temp; i++) {
if (board[row - i][col - i] == 'B' || board[row - i][col - i] == 'Q') {
status = 1;
return true;
} else if (board[row - i][col - i] != '.') // reached a piece that is not bishop
break;
}
}
if (row > 0 && col < 7) { // check up right
int temp = (row > 7 - col) ? (7 - col) : row;
for (int i = 1; i <= temp; i++) {
if (board[row - i][col + i] == 'B' || board[row - i][col + i] == 'Q') {
status = 1;
return true;
} else if (board[row - i][col + i] != '.') // reached a piece that is not bishop
break;
}
}
if (row < 7 && col > 0) { // check down left
int temp = (7 - row > col) ? col : (7 - row);
for (int i = 1; i <= temp; i++) {
if (board[row + i][col - i] == 'B' || board[row + i][col - i] == 'Q') {
status = 1;
return true;
} else if (board[row + i][col - i] != '.') // reached a piece that is not bishop
break;
}
}
if (row < 7 && col < 7) { // check down right
int temp = (7 - row > 7 - col) ? (7 - col) : (7 - row);
for (int i = 1; i <= temp; i++) {
if (board[row + i][col + i] == 'B' || board[row + i][col + i] == 'Q') {
status = 1;
return true;
} else if (board[row + i][col + i] != '.') // reached a piece that is not bishop
break;
}
}
///////////////////////// check for pawn
if (row != 7 && col != 0 && board[row + 1][col - 1] == 'P') { // check down right
status = 1;
return true;
}
if (row != 7 && col != 7 && board[row + 1][col + 1] == 'P') { // check down right
status = 1;
return true;
}
//////////////////////// check for knight
/////////////// Up pieces
if (row > 0 && col > 1 && board[row - 1][col - 2] == 'N') {
status = 1;
return true;
}
if (row > 1 && col > 0 && board[row - 2][col - 1] == 'N') {
status = 1;
return true;
}
if (row > 1 && col < 7 && board[row - 2][col + 1] == 'N') {
status = 1;
return true;
}
if (row > 0 && col < 6 && board[row - 1][col + 2] == 'N') {
status = 1;
return true;
}
//////////////// Down pieces
if (row < 7 && col > 1 && board[row + 1][col - 2] == 'N') {
status = 1;
return true;
}
if (row < 6 && col > 0 && board[row + 2][col - 1] == 'N') {
status = 1;
return true;
}
if (row < 6 && col < 7 && board[row + 2][col + 1] == 'N') {
status = 1;
return true;
}
if (row < 7 && col < 6 && board[row + 1][col + 2] == 'N') {
status = 1;
return true;
}
break;
case 'K': // White king
if (row > 0) { // check up for rook or queen
for (int i = row - 1; i >= 0; i--) {
if (board[i][col] == 'r' || board[i][col] == 'q') {
status = 2;
return true;
} else if (board[i][col] != '.') // reached a piece that is not king
break;
}
}
if (row < 7) { // check down for rook or queen
for (int i = row + 1; i <= 7; i++) {
if (board[i][col] == 'r' || board[i][col] == 'q') {
status = 2;
return true;
} else if (board[i][col] != '.') // reached a piece that is not king
break;
}
}
if (col > 0) { // check left for rook or queen
for (int i = col - 1; i >= 0; i--) {
if (board[row][i] == 'r' || board[row][i] == 'q') {
status = 2;
return true;
} else if (board[row][i] != '.') // reached a piece that is not king
break;
}
}
if (col < 7) { // check right for rook or queen
for (int i = col + 1; i <= 7; i++) {
if (board[row][i] == 'r' || board[row][i] == 'q') {
status = 2;
return true;
} else if (board[row][i] != '.') // reached a piece that is not king
break;
}
}
//////////////////////////////// check for bishop and queen
if (row > 0 && col > 0) { // check up left
int temp = (row > col) ? col : row;
for (int i = 1; i <= temp; i++) {
if (board[row - i][col - i] == 'b' || board[row - i][col - i] == 'q') {
status = 2;
return true;
} else if (board[row - i][col - i] != '.') // reached a piece that is not bishop
break;
}
}
if (row > 0 && col < 7) { // check up right
int temp = (row > 7 - col) ? (7 - col) : row;
for (int i = 1; i <= temp; i++) {
if (board[row - i][col + i] == 'b' || board[row - i][col + i] == 'q') {
status = 2;
return true;
} else if (board[row - i][col + i] != '.') // reached a piece that is not bishop
break;
}
}
if (row < 7 && col > 0) { // check down left
int temp = (7 - row > col) ? col : (7 - row);
for (int i = 1; i <= temp; i++) {
if (board[row + i][col - i] == 'b' || board[row + i][col - i] == 'q') {
status = 2;
return true;
} else if (board[row + i][col - i] != '.') // reached a piece that is not bishop
break;
}
}
if (row < 7 && col < 7) { // check down right
int temp = (7 - row > 7 - col) ? (7 - col) : (7 - row);
for (int i = 1; i <= temp; i++) {
if (board[row + i][col + i] == 'b' || board[row + i][col + i] == 'q') {
status = 2;
return true;
} else if (board[row + i][col + i] != '.') // reached a piece that is not bishop
break;
}
}
///////////////////////// check for pawn
if (row != 0 && col != 0 && board[row - 1][col - 1] == 'p') { // check down right
status = 2;
return true;
}
if (row != 0 && col != 7 && board[row - 1][col + 1] == 'p') { // check down right
status = 2;
return true;
}
//////////////////////// check for knight
/////////////// Up pieces
if (row > 0 && col > 1 && board[row - 1][col - 2] == 'n') {
status = 2;
return true;
}
if (row > 1 && col > 0 && board[row - 2][col - 1] == 'n') {
status = 2;
return true;
}
if (row > 1 && col < 7 && board[row - 2][col + 1] == 'n') {
status = 2;
return true;
}
if (row > 0 && col < 6 && board[row - 1][col + 2] == 'n') {
status = 2;
return true;
}
//////////////// Down pieces
if (row < 7 && col > 1 && board[row + 1][col - 2] == 'n') {
status = 2;
return true;
}
if (row < 6 && col > 0 && board[row + 2][col - 1] == 'n') {
status = 2;
return true;
}
if (row < 6 && col < 7 && board[row + 2][col + 1] == 'n') {
status = 2;
return true;
}
if (row < 7 && col < 6 && board[row + 1][col + 2] == 'n') {
status = 2;
return true;
}
break;
}
return false;
}
int main()
{
// ifstream in("CheckTheCheck.in");
bool end = false;
string temp;
while (!end) {
end = true;
status = 0;
for (int i = 0; i < 8; i++) {
// getline(in, temp);
getline(cin, temp);
for (int j = 0; j < 8; j++) {
if ((board[i][j] = temp[j]) != '.')
end = false;
}
}
bool checked = false;
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 8; j++) {
if (board[i][j] == 'k' || board[i][j] == 'K') {
checked = checkTheCheck(i, j);
if (checked)
break;
}
}
if (checked)
break;
}
if (end)
break;
if (caseNum > 1)
cout << endl;
switch (status) {
case 0:
cout << "Game #" << caseNum << ": no king is in check.";
break;
case 1:
cout << "Game #" << caseNum << ": black king is in check.";
break;
case 2:
cout << "Game #" << caseNum << ": white king is in check.";
break;
}
// getline(in, temp);
getline(cin, temp);
caseNum++;
}
// cin >> temp;
// in.close();
return 0;
}
Thanks.