VOOZH about

URL: https://www.geeksforgeeks.org/dsa/chessboard-pawn-pawn-game/

⇱ Chessboard Pawn-Pawn game - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Chessboard Pawn-Pawn game

Last Updated : 3 Aug, 2022

There is an 8*8 chessboard and two chess players having a single pawn each. A player has to move his pawn in each turn, either one step forward or one step diagonally only when this move kills the other pawn. The player who is unable to make any move loses. 
Given row and column numbers of white and black pawns. The task is to predict who would win assuming both plays optimally. Note that White plays first and a pawn cannot move outside the chessboard.
 

👁 Image


Examples: 
 

Input: rowW = 2, colW = 2, rowB = 3, colB = 3 
Output: White
Input: rowW = 2, colW = 2, rowB = 3, colB = 3 
Output: White 
 


 


Approach: 
 

  • If its white pawn's turn we have to check whether white pawn is on the 8th row then black wins because white pawn has no further move. If it's black pawn's turn then we have to check whether it is on the 1st row then white wins because black pawn has no further move.
  • If its white pawn's turn and black pawn is adjacent diagonally then white pawn will kill black pawn and white pawn wins else white pawn will move one step forward (if not already occupied by the black pawn) else white will lose.
  • If it's black pawn's turn and white pawn is adjacent diagonally then black pawn will kill white pawn and black pawn wins else black pawn will move one step forward (if not already occupied by the white pawn) else black will lose.


Below is the implementation of the above approach: 
 


Output: 
White

 

Time Complexity: O(1) as the time is constant.

Auxiliary Space: O(1)

Comment
Article Tags: