==== 소감 ==== 2005/02/19 Accepted 0:00.004 64 King의 check상태를 좀더 쉽게 알 수 있으려면 어떻게 해야 할까? ==== 코드 ==== {{{~cpp // no10196 - Check the Check #include #include using namespace std; const int MAX_CASE = 300; const int TIED = -1; const int BLACK = 0; const int WHITE = 1; char chess[10][10]; inline void eatline() { while(cin.get() != '\n') continue; }; bool DoItChess(); int WhoIsWin(); int Pawn(int row, int col, char enemy); int Rook(int row, int col, char enemy); int Bishop(int row, int col, char enemy); int Queen(int row, int col, char enemy); int Knight(int row, int col, char enemy); int main() { int win[MAX_CASE]; int nCase = 0; while(DoItChess()) { win[nCase++] = WhoIsWin(); eatline(); } for (int i=0; i=0; i--) { if (chess[i][col] == enemy) { check = true; break; } else if (chess[i][col] == '.') continue; else break; } for (i=row+1; i<8; i++) { if (chess[i][col] == enemy) { check = true; break; } else if (chess[i][col] == '.') continue; else break; } for (i=col-1; i>=0; i--) { if (chess[row][i] == enemy) { check = true; break; } else if (chess[row][i] == '.') continue; else break; } for (i=col+1; i<8; i++) { if (chess[row][i] == enemy) { check = true; break; } else if (chess[row][i] == '.') continue; else break; } if (check) { if(enemy == 'K') return BLACK; else return WHITE; } return TIED; } int Bishop(int row, int col, char enemy) { int i; bool check = false; bool temp[2] = {0,}; int count = 0; for (i=row-1; i>=0; i--) { count++; if (col - count >= 0 && !temp[0]) { if (chess[i][col-count] == enemy) { check = true; break; } else if (chess[i][col-count] == '.') ; else temp[0] = 1; } if (col + count < 8 && !temp[1]) { if (chess[i][col+count] == enemy) { check = true; break; } else if (chess[i][col+count] == '.') ; else temp[1] = 1; } } count = 0; temp[0] = temp[1] = 0; for (i=row+1; i<8; i++) { count++; if (col - count >= 0 && !temp[0]) { if (chess[i][col-count] == enemy) { check = true; break; } else if (chess[i][col-count] == '.') ; else temp[0] = 1; } if (col + count < 8 && !temp[1]) { if (chess[i][col+count] == enemy) { check = true; break; } else if (chess[i][col+count] == '.') ; else temp[1] = 1; } } if (check) { if(enemy == 'K') return BLACK; else return WHITE; } return TIED; } int Queen(int row, int col, char enemy) { if (Rook(row, col, enemy) == BLACK || Bishop(row, col, enemy) == BLACK) return BLACK; else if(Rook(row, col, enemy) == WHITE || Bishop(row, col, enemy) == WHITE) return WHITE; return TIED; } int Knight(int row, int col, char enemy) { bool check = false; if (chess[row-2][col-1] == enemy || chess[row-2][col+1] == enemy) check = true; if (chess[row-1][col-2] == enemy || chess[row-1][col+2] == enemy) check = true; if (chess[row+2][col-1] == enemy || chess[row+2][col+1] == enemy) check = true; if (chess[row+1][col-2] == enemy || chess[row+1][col+2] == enemy) check = true; if (check && enemy == 'K') return BLACK; if (check && enemy == 'k') return WHITE; return TIED; } }}} ---- [CheckTheCheck] [문보창]