# Check Mate CHECKMATE Solution Codechef

Page Contents

## Check Mate CHECKMATE Solution

You are given a standard 8×88×8 chessboard which has exactly 33 pieces placed on it – 22 black rooks and 11 white king. The rows are numbered 11 to 88 from bottom to top, and the columns are numbered 11 to 88 from left to right. The cell at the intersection of the ii-th column and jj-th row is denoted (i,j)(i,j) (like the coordinate system on the xyxy-plane).

Is it possible for the white king to be checkmated by the rooks in exactly one move? It is guaranteed that the initial position of pieces is such that the white king is not under check.

Note that in the given situation, black moves first.

Assume that all the chess rules are to be followed:

• Each square can contain at most one piece.
• Rooks can only travel in straight lines along the row or column they are placed at, and can’t jump over other pieces.
• The King can move to any of its 88 adjacent squares.
• A piece can’t capture a piece of the same color as itself.
• After a black makes a move, the white king can capture a rook if it’s present on any of the 88 adjacent squares and not protected by the other rook.
• The king is said to be in checkmate if the king is currently under check and there exists no legal move with the king to escape from the check.
• Stalemate is considered as a draw for both sides.

Note: The input and output are large, so use fast input-output methods.

• In C++, either use `printf` and `scanf`, or add the line `ios::sync_with_stdio(0); cin.tie(0);` to the beginning of `main` to speed up `cin` and `cout`. Also, do not use `endl`, only use `'\n'`.
• In python3/pypy3, add the lines
```import sys
```

to the beginning of your code and then use `input` as you would normally do

• In Java, use `BufferedReader` and `PrintWriter`

### Input Format

• The first line of input will contain a single integer TT, denoting the number of test cases. The description of TT test cases follows.
• Each test case contains three lines of input.
• The first line contains two space-separated integers xkxk and ykyk, the row and column respectively in which white king is placed.
• The second line contains two space-separated integers x1x1 and y1y1, the row and column respectively in which the first black rook is placed.
• The third line contains two space-separated integers x2x2 and y2y2, the row and column respectively in which the second black rook is placed.

### Output Format

For each test case, output a single line containing the answer: `"YES"` if it’s possible to checkmate the king in one move and `"NO"` if not.

You may print each character of the string in uppercase or lowercase (for example, the strings “yEs”, “yes”, “Yes” and “YES” will all be treated as identical).

### Constraints

• 1≤T≤1.6⋅1051≤T≤1.6⋅105
• 1≤xk,yk,x1,y1,x2,y2≤81≤xk,yk,x1,y1,x2,y2≤8
• Initially the king is not under check, i.e, xk≠x1xk≠x1 and xk≠x2xk≠x2 and yk≠y1yk≠y1 and yk≠y2yk≠y2.
• No two pieces overlap.

Subtask #1 (100 points): Original constraints

```4
1 1
8 2
7 3
1 8
8 2
8 3
1 1
8 2
2 3
1 1
8 8
8 2
```

```YES
NO
NO
NO
```

### Explanation

Test Case 11: The second rook can be moved to (7,1)(7,1), thus checkmating the king. Test Case 22 and 33: No possible move exists to checkmate the king. The following images describe why checkmating is impossible in test case 22:

Test Case 44: The game can end in a stalemate if the first rook is moved to (2,8)(2,8), but there exists no move to checkmate the king.

## SOLUTION

Program Python: Check Mate CHECKMATE Solution in Python

```# cook your dish here
t=int(input())
for i in range(t):
xk,yk=map(int,input().split(' '))
x1,y1=map(int,input().split(' '))
x2,y2=map(int,input().split(' '))
a="NO"

if((xk!=1 and yk!=1 and xk!=8 and yk!=8)):
a="NO"

else:
if(xk==1):
if((x1==2 or x2==2) and y1!=y2 ):
if(abs(y1-yk)>1 and abs(y2-yk)>1):
a="YES"

elif(xk==8):
if((x1==7 or x2==7) and y1!=y2 ):
if(abs(y1-yk)>1 and abs(y2-yk)>1):
a="YES"

if(yk==1):
if((y1==2 or y2==2) and x1!=x2 ):
if (abs(x1-xk)>1 and abs(x2-xk)>1):
a="YES"

elif(yk==8):
if((y1==7 or y2==7) and x1!=x2 ):
if(abs(x1-xk)>1 and abs(x2-xk)>1):
a="YES"
print(a)

```

Program C++: Check Mate CHECKMATE Solution in C++

```#include <iostream>
#include<cmath>
using namespace std;

int main() {
int t;
cin>>t;
while(t--)
{
int xk,yk,x1,y1,x2,y2;
cin>>xk>>yk>>x1>>y1>>x2>>y2;
string ans="NO";
if(!(xk==1 || xk==8 || yk==1 || yk==8))
{
ans="NO";
}
else
{
if(xk==1)
{
if((x1==2 || x2==2 )&& y1!=y2)
{
if(abs(y1-yk) > 1 && abs(y2-yk)>1)
{
ans="YES";
}
}
}
else if(xk==8)
{
if((x1==7 || x2==7) && y1!=y2)
{
if(abs(y1-yk) > 1 && abs(y2-yk)>1)
{
ans="YES";
}
}
}
if(yk==1)
{
if((y1==2 || y2==2) && x1!=x2)
{
if(abs(x1-xk) > 1 && abs(x2-xk)>1)
{
ans="YES";
}
}
}
else if(yk==8)
{
if((y1==7 || y2==7) && x1!=x2)
{
if(abs(x1-xk) > 1 && abs(x2-xk)>1)
{
ans="YES";
}
}
}
}
cout<<ans<<endl;
}
return 0;
}
```