UV problem 10196 check the check (General)

Source: Internet
Author: User
// Check the check (general) // PC/Ultraviolet IDs: 110107/10196, popularity: B, success rate: average level: 1 // verdict: accepted // submission date: 2011-05-22 // UV Run Time: 0.008 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net # include <iostream> # include <cstdlib> using namespace STD; char status [8] [8]; // indicates the position of the chess piece. // Check whether the king with the coordinates of (kingx, kingy) is within the range of attacks with the coordinates of (x, y). Pay attention to the different attack directions. Bool checkp (int x, int y, int kingx, int kingy) {return (X-kingx) = 1 & ABS (Y-kingy) = 1 ;} // check whether the king with the coordinates (kingx, kingy) is within the range of Horse attacks with the coordinates (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);} // check that the coordinates are (kingx, kingy) is the king in the range of (x, y) attacks. Bool checkb (int x, int y, int kingx, int kingy) {int step, currentx = x, currenty = Y, DirectX, directy; bool checked = false; // check whether the elephant is in the diagonal line with the king. If (ABS (X-kingx) = ABS (Y-kingy) {checked = true; // determine the relative position. DirectX = (x <kingx )? 1:-1; directy = (Y <kingy )? 1:-1; // whether the pawns are separated. Step = ABS (X-kingx); While (Step> 1) {currentx + = DirectX; currenty + = directy; If (status [currentx] [currenty]! = '. ') {Checked = false; break;} Step --;} return checked;} // check whether the king whose coordinates are (kingx, kingy) is (x, y) within the range of vehicle attacks. Bool checkr (int x, int y, int kingx, int kingy) {int step, currentx = x, currenty = Y, DirectX = 0, directy = 0; bool checked = false; // whether the car and the King are in the same line or in the same column, and whether there are pawns between the car and the king. If (x = kingx | Y = kingy) {checked = true; // determine the relative position. DirectX = (x = kingx )? (0): (x <kingx? 1:-1); directy = (y = kingy )? (0): (Y <kingy? 1:-1); // whether the pawns are separated. 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;} // check whether the king whose coordinates are (kingx, kingy) is (x, y) within the scope of the Queen attack. Bool checkq (int x, int y, int kingx, int kingy) {return checkr (X, Y, kingx, kingy) | checkb (X, Y, kingx, kingy);} // check whether there is a general situation in the given checkerboard status. 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; // search for the positions of the white game and the black game players. 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 the coordinate of the King is not found, the chessboard is empty. If (bkingx =-1) return; cout <"game #" <gamecount <":"; // checks whether the pawns and kings have generals. For (INT I = 0; I <8; I ++) for (Int J = 0; j <8; j ++) {Switch (status [I] [J]) {// blacklist. Case 'p': wchecked = checkp (wkingx, wkingy, I, j); break; // whitelist. Case 'p': bchecked = checkp (I, j, bkingx, bkingy); break; // dark horse. Case 'N': wchecked = checkn (I, j, wkingx, wkingy); break; // white horse. Case 'N': bchecked = checkn (I, j, bkingx, bkingy); break; // black elephant. Case 'B': wchecked = checkb (I, j, wkingx, wkingy); break; // white elephant. Case 'B': bchecked = checkb (I, j, bkingx, bkingy); break; // black car. Case 'r': wchecked = checkr (I, j, wkingx, wkingy); break; // white car. Case 'r': bchecked = checkr (I, j, bkingx, bkingy); break; // black. Case 'q': wchecked = checkq (I, j, wkingx, wkingy); break; // after the white. Case 'q': bchecked = checkq (I, j, bkingx, bkingy); break; // other cases. Default: break;} // check whether the general situation exists. 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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.