Find path traveling which bishop traverse each black cell atleast once
Last Updated : 6 Apr, 2023
Given two integers X and Y such that X+Y is even, Which denotes the initial position of the bishop on a chessboard of 8 x 8 dimension. Then the task is to output a continuous path with not more than 32 coordinates(total black cells on board) such that traveling on that path bishop covers all the cells (i, j) having even sum (i + j = even). These cells need to be covered at least once over the course of travel, return the number of coordinates taken in the path, and the coordinates of the path to be followed.
Note: A single cell can be traversed more than once.
Initially, the bishop is placed at (5, 3). Then it moves as follows: A (4, 4) -> B (1, 1) -> C (8, 8 ) -> D (7, 7) -> E (8, 6) -> F (3, 1) and so on. So, In total there are 19 moves, Which are shown in the output. Following this output coordinates all the cells (i, j) having even sum, ie. (i + j) = even, will be traversed. Formally, all black cells will be traversed.
Input: X =1, Y = 1 Output: 17 1 1 8 8 7 7 8 6 . . . . and so on. Explanation: In total there will be 17 moves, and the starting of the path is as: (1, 1) -> (8, 8) -> (7, 7) -> (8, 6) and so on . . . .
Approach: Implement the idea below to solve the problem:
The problem is observation based and can be solved by using those observations. There exists a common path that applies for all the inputs of X and Y. By thinking about this problem deeply, we can observe a common path.
Steps were taken to solve the problem:
Create a method commonPath() containing the below co-ordinates:
If ( X == Y && X == 1 ) then print all the points of the common_path() method.
Else if ( X == Y && X != 1 ) then print 18 coordinates, Formally (X, Y) with all the points of the common_path() method.
Else print 19 coordinates, Formally (X, Y), ( (X+Y)/2, (X+Y)/2 ) all the points of common_path() method.
Below is the code to implement the approach:
C++
#include<iostream>usingnamespacestd;// Method for printing pathvoidpathPrinter(intX,intY){// Condition for implementing// approachif(X==Y&&X==1){cout<<17<<endl;// call commonPath}elseif(X==Y&&X!=1){cout<<18<<endl;cout<<X<<" "<<Y<<endl;// call commonPath}else{cout<<19<<endl;cout<<X<<" "<<Y<<endl;intd=(X+Y)/2;cout<<d<<" "<<d<<endl;// call commonPath}}// Method containing common path// co-ordinates for all inputsvoidcommonPath(){cout<<"1 1"<<endl;cout<<"8 8"<<endl;cout<<"7 7"<<endl;cout<<"8 6"<<endl;cout<<"3 1"<<endl;cout<<"1 3"<<endl;cout<<"6 8"<<endl;cout<<"5 7"<<endl;cout<<"4 8"<<endl;cout<<"1 5"<<endl;cout<<"5 1"<<endl;cout<<"8 4"<<endl;cout<<"7 3"<<endl;cout<<"8 2"<<endl;cout<<"7 1"<<endl;cout<<"1 7"<<endl;cout<<"2 8"<<endl;}// Driver Functionintmain(){// InputsintX=6;intY=2;// Function callpathPrinter(X,Y);commonPath();}
Java
// Java code to implement the approachimportjava.io.*;importjava.lang.*;importjava.util.*;// Driver FunctionclassGFG{publicstaticvoidmain(String[]args){// InputsintX=6;intY=2;// Function callpathPrinter(X,Y);}// Method for printing pathstaticvoidpathPrinter(intX,intY){// Condition for implementing// approachif(X==Y&&X==1){System.out.println(17);commonPath();}elseif(X==Y&&X!=1){System.out.println(18);System.out.println(X+" "+Y);commonPath();}else{System.out.println(19);System.out.println(X+" "+Y);intd=(X+Y)/2;System.out.println(d+" "+d);commonPath();}}// Method containing common path// co-ordinates for all inputspublicstaticvoidcommonPath(){System.out.println(1+" "+1);System.out.println(8+" "+8);System.out.println(7+" "+7);System.out.println(8+" "+6);System.out.println(3+" "+1);System.out.println(1+" "+3);System.out.println(6+" "+8);System.out.println(5+" "+7);System.out.println(4+" "+8);System.out.println(1+" "+5);System.out.println(5+" "+1);System.out.println(8+" "+4);System.out.println(7+" "+3);System.out.println(8+" "+2);System.out.println(7+" "+1);System.out.println(1+" "+7);System.out.println(2+" "+8);}}
Python3
# Method for printing pathdefpathPrinter(X,Y):# Condition for implementing # approachifX==YandX==1:print(17)# call commonPathelifX==YandX!=1:print(18)print(X,Y)# call commonPathelse:print(19)print(X,Y)d=(X+Y)//2print(d,d)# call commonPath# Method containing common path# co-ordinates for all inputsdefcommonPath():print("1 1")print("8 8")print("7 7")print("8 6")print("3 1")print("1 3")print("6 8")print("5 7")print("4 8")print("1 5")print("5 1")print("8 4")print("7 3")print("8 2")print("7 1")print("1 7")print("2 8")# Driver Function# InputsX=6Y=2# Function callpathPrinter(X,Y)commonPath()# This code is contributed by prasad264
C#
// C# code to implement the approachusingSystem;publicclassGFG{// Method for printing pathstaticvoidpathPrinter(intX,intY){// Condition for implementing// approachif(X==Y&&X==1){Console.WriteLine(17);commonPath();}elseif(X==Y&&X!=1){Console.WriteLine(18);Console.WriteLine(X+" "+Y);commonPath();}else{Console.WriteLine(19);Console.WriteLine(X+" "+Y);intd=(X+Y)/2;Console.WriteLine(d+" "+d);commonPath();}}// Method containing common path// co-ordinates for all inputspublicstaticvoidcommonPath(){Console.WriteLine(1+" "+1);Console.WriteLine(8+" "+8);Console.WriteLine(7+" "+7);Console.WriteLine(8+" "+6);Console.WriteLine(3+" "+1);Console.WriteLine(1+" "+3);Console.WriteLine(6+" "+8);Console.WriteLine(5+" "+7);Console.WriteLine(4+" "+8);Console.WriteLine(1+" "+5);Console.WriteLine(5+" "+1);Console.WriteLine(8+" "+4);Console.WriteLine(7+" "+3);Console.WriteLine(8+" "+2);Console.WriteLine(7+" "+1);Console.WriteLine(1+" "+7);Console.WriteLine(2+" "+8);}// Driver CodestaticpublicvoidMain(){// InputsintX=6;intY=2;// Function callpathPrinter(X,Y);}}// This code is contributed by Rohit Pradhan
JavaScript
// Method for printing pathfunctionpathPrinter(X,Y){// Condition for implementing approachif(X===Y&&X===1){console.log(17);// call commonPath}elseif(X===Y&&X!==1){console.log(18);console.log(X+" "+Y);// call commonPath}else{console.log(19);console.log(X+" "+Y);letd=Math.floor((X+Y)/2);console.log(d+" "+d);// call commonPath}}// Method containing common path coordinates for all inputsfunctioncommonPath(){console.log("1 1");console.log("8 8");console.log("7 7");console.log("8 6");console.log("3 1");console.log("1 3");console.log("6 8");console.log("5 7");console.log("4 8");console.log("1 5");console.log("5 1");console.log("8 4");console.log("7 3");console.log("8 2");console.log("7 1");console.log("1 7");console.log("2 8");}// Driver functionfunctionmain(){// InputsletX=6;letY=2;// Function callpathPrinter(X,Y);commonPath();}main();