Two Rooks CodeChef Solution

Problem – Two Rooks CodeChef Solution

You are given a standard 8×8 chessboard which has exactly 2 rooks on it and no other pieces. The rows are numbered 1 to 8 from bottom to top, and the columns are numbered 1 to 8 from left to right. The cell at the intersection of the i-th column and j-th row is denoted (i,j).

Given the initial positions of the rooks in the form of coordinates ((X1​,Y1​) and (X2​,Y2​), you need to tell whether the 2 rooks currently attack each other or not. Assume, 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. For a more detailed explanation of the moves of rooks, along with images, please click here.

Input Format

  • The first line contains T – the number of test cases. Then the test cases follow.
  • The first line of each test case contain four space-separated integers each X1​,Y1​,X2​,Y2​ – (X1​,Y1​) is the position of the first rook and (X2​,Y2​) is the position of the second rook.

Output Format

For each test case, output on a single line YES (without quotes) if the rooks attack each other, and NO otherwise.

You may print each character of the string in uppercase or lowercase (for example, the strings YeSYEsyes and yeS will all be treated as identical).


  • 1≤T≤5000
  • 1≤X1​,X2​,Y1​,Y2​≤8
  • (X1​,Y1​)=(X2​,Y2​)

Sample 1:

1 2 5 2
1 2 1 5
1 1 8 8


  • Test case 1: The two rooks can attack each other by moving along the second column.
  • Test case 2: The two rooks can attack each other by moving along the first row.
  • Test case 3: No matter how a rook moves it cannot reach the second rook in one move. Hence, they do not attack each other.

Two Rooks CodeChef Solution in Java

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
	public static void main (String[] args) throws java.lang.Exception
		Scanner in = new Scanner(;
		int t = in.nextInt();
		while(t-- >0){
		    int x1 = in.nextInt();
		    int y1 = in.nextInt();
		    int x2 = in.nextInt();
		    int y2 = in.nextInt();
		    if(x1==x2 || y1==y2){

Two Rooks CodeChef Solution in C++17

using namespace std;

void sol(){
  int x1,x2,y1,y2;
  cout<<((x1==x2 || y1==y2)? "YES":"NO")<<endl;

int main(){

    ios_base::sync_with_stdio (false);

    int t;


    return 0;

Two Rooks CodeChef Solution in Pyth 3

# cook your dish here
t = int(input())
for i in range(0,t):
    x1,y1,x2,y2 = map(int,input().split())
    if x1==x2 or y1==y2: print ("YES")
    else: print("NO")
Two Rooks CodeChef Solution Review:

In our experience, we suggest you solve this Two Rooks CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Two Rooks CodeChef Solution

Find on CodeChef


I hope this Two Rooks CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *