Open In App

Find position of Queen in chessboard from given attack lines of queen

Improve
Improve
Like Article
Like
Save
Share
Report

Given an N×N Matrix Mat[][] of N rows and N columns. There is exactly one Queen on the chessboard and the cell that is under the attack of the Queen is represented by ‘Q’ and the cell which is not under attack of the Queen is represented by ‘.’. The task is to find the position of the Queen.

Note: If the position cannot be found print return a pair of -1.

Examples:

Input: {{‘Q’, ‘ Q’, Q’}, {‘Q’, Q’, Q’}, {‘Q’, Q’, Q’}}
Output: 2 2
Explanation: Queen is at the middle of the matrix, because,  
it’s the only place from where you can attack in every cell 
of the matrix of dimension 3?3.

Input: {{Q, Q}, {Q, Q}}
Output: -1 -1
Explanation: Queen can be in any of the four positions.

Approach: The problem can be solved based on the following observation:

  • If N = 1, Queen can only be at [1, 1]
  • If N = 2, It’s impossible to find the position of Queen.
  • If N = 3, it’s a special case.
    • Check if every cell is under the attack of the queen, then it is in [2, 2].
    • Else, use a nested loop to traverse every cell of the grid and find the position of the queen by checking 4 corners, 4 edges, and 1 Middle somewhere conditions one by one. 
  • For N > 3 also, use the 2nd method as used for when N = 3.
  • If the loop ends, and nothing returns then either the Queen is not there or it is impossible to find the Queen so, return -1, -1.

Below is the implementation for the above approach.

C++




// C++ code for the above approach:
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the position of the queen
pair<int, int> solve(vector<vector<char> >& Mat,
                     int n)
{
    if (n == 1) {
        return { 1, 1 };
    }
 
    if (n == 2) {
        return { -1, -1 };
    }
 
    if (n == 3) {
        if (Mat[1][1] == 'Q') {
            if (Mat[0][0] == 'Q' && Mat[0][2] == 'Q'
                && Mat[2][0] == 'Q' && Mat[2][2] == 'Q'
                && Mat[1][0] == 'Q' && Mat[1][2] == 'Q'
                && Mat[0][1] == 'Q' && Mat[2][1] == 'Q')
                return { 2, 2 };
        }
    }
 
    for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
 
            if (r == 0 && c == 0) {
                if (Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (r == 0 && c == (n - 1)) {
                if (Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (r == (n - 1) && c == 0) {
                if (Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (r == (n - 1) && c == (n - 1)) {
                if (Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (c == 0) {
                if (Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (c == (n - 1)) {
                if (Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (r == 0) {
                if (Mat[r] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r + 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (r == (n - 1)) {
                if (Mat[r] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r - 1] == 'Q')
                    return { r + 1, c + 1 };
            }
            if (Mat[r] == 'Q') {
                if (Mat[r - 1] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r + 1] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r] == 'Q'
                    && Mat[r - 1] == 'Q'
                    && Mat[r + 1] == 'Q')
                    return { r + 1, c + 1 };
            }
        }
    }
 
    // If the position of the queen
    // cannot be determined
    return { -1, -1 };
}
 
// Driver code
int main()
{
    int N = 3;
    vector<vector<char> > v = { { 'Q', 'Q', 'Q' },
                                { 'Q', 'Q', 'Q' },
                                { 'Q', 'Q', 'Q' } };
 
    // Function call
    pair<int, int> p = solve(v, N);
    cout << p.first << " " << p.second << endl;
 
    return 0;
}


Java




// Java code for the above approach
import java.io.*;
 
class pair {
  int first, second;
  public pair(int first, int second)
  {
    this.first = first;
    this.second = second;
  }
}
 
class GFG {
 
  // Function to find the position of the queen
  static pair solve(int[][] Mat, int n)
  {
    if (n == 1) {
      return new pair(1, 1);
    }
 
    if (n == 2) {
      return new pair(-1, -1);
    }
 
    if (n == 3) {
      if (Mat[1][1] == 'Q') {
        if (Mat[0][0] == 'Q' && Mat[0][2] == 'Q'
            && Mat[2][0] == 'Q' && Mat[2][2] == 'Q'
            && Mat[1][0] == 'Q' && Mat[1][2] == 'Q'
            && Mat[0][1] == 'Q' && Mat[2][1] == 'Q')
          return new pair(2, 2);
      }
    }
 
    for (int r = 0; r < n; r++) {
      for (int c = 0; c < n; c++) {
 
        if (r == 0 && c == 0) {
          if (Mat[r] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r + 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (r == 0 && c == (n - 1)) {
          if (Mat[r] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r + 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (r == (n - 1) && c == 0) {
          if (Mat[r] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r - 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (r == (n - 1) && c == (n - 1)) {
          if (Mat[r] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r - 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (c == 0) {
          if (Mat[r] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (c == (n - 1)) {
          if (Mat[r] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (r == 0) {
          if (Mat[r] == 'Q' && Mat[r] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r + 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (r == (n - 1)) {
          if (Mat[r] == 'Q' && Mat[r] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r - 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
        if (Mat[r] == 'Q') {
          if (Mat[r - 1] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r + 1] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r] == 'Q'
              && Mat[r - 1] == 'Q'
              && Mat[r + 1] == 'Q')
            return new pair(r + 1, c + 1);
        }
      }
    }
 
    // If the position of the queen
    // cannot be determined
    return new pair(-1, -1);
  }
 
  public static void main(String[] args)
  {
    int N = 3;
    int[][] v = { { 'Q', 'Q', 'Q' },
                 { 'Q', 'Q', 'Q' },
                 { 'Q', 'Q', 'Q' } };
 
    // Function call
    pair p = solve(v, N);
    System.out.println(p.first + " " + p.second);
  }
}
 
// This code is contributed by lokeshmvs21.


Python3




#  Python code for the above approach:
 
#  Function to find the position of the queen
def solve(Mat, n):
    if (n == 1):
 
        return [1, 1]
 
    if (n == 2):
 
        return [-1, -1]
 
    if (n == 3):
        if (Mat[1][1] == 'Q'):
            if (Mat[0][0] == 'Q' and Mat[0][2] == 'Q'
                and Mat[1][0] == 'Q' and Mat[1][2] == 'Q'
                    and Mat[0][1] == 'Q' and Mat[2][1] == 'Q'):
 
                return [2, 2]
 
    for r in range(n):
        for c in range(n):
 
            if (r == 0 and c == 0):
                if (Mat[r] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r] == 'Q'
                        and Mat[r + 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (r == 0 and c == (n - 1)):
                if (Mat[r] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r] == 'Q'
                        and Mat[r + 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (r == (n - 1) and c == 0):
                if (Mat[r] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r] == 'Q'
                        and Mat[r - 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (r == (n - 1) and c == (n - 1)):
 
                if (Mat[r] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r] == 'Q'
                        and Mat[r - 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (c == 0):
 
                if (Mat[r] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                        and Mat[r] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (c == (n - 1)):
                if (Mat[r] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                        and Mat[r] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (r == 0):
 
                if (Mat[r] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r + 1] == 'Q'
                        and Mat[r + 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (r == (n - 1)):
 
                if (Mat[r] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                        and Mat[r - 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
            if (Mat[r] == 'Q'):
                if (Mat[r - 1] == 'Q'
                    and Mat[r - 1] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r + 1] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r] == 'Q'
                    and Mat[r - 1] == 'Q'
                        and Mat[r + 1] == 'Q'):
 
                    return [r + 1, c + 1]
 
    #  If the position of the queen
    #  cannot be determined
    return [-1, -1]
 
#  Driver code
if __name__ == "__main__":
    N = 3
 
    v = [['Q', 'Q', 'Q'],
         ['Q', 'Q', 'Q'],
         ['Q', 'Q', 'Q']]
 
    #  Function call
    p = solve(v, N)
    print(p[0], "", p[1])
     
# This code is contributed by vivek maddeshiya


C#




// C# implementation
 
using System;
using System.Collections.Generic;
 
public class GFG{
   
  // Function to find the position of the queen
public static List<int> solve(char[,] Mat,
                     int n)
{
    if (n == 1) {
      List<int> abc = new List<int>();
      abc.Add(1);
      abc.Add(1);
        return abc;
    }
 
    if (n == 2) {
      List<int> abc = new List<int>();
      abc.Add(-1);
      abc.Add(-1);
        return abc;
    }
 
    if (n == 3) {
        if (Mat[1,1] == 'Q') {
            if (Mat[0,0] == 'Q' && Mat[0,2] == 'Q'
                && Mat[2,0] == 'Q' && Mat[2,2] == 'Q'
                && Mat[1,0] == 'Q' && Mat[1,2] == 'Q'
                && Mat[0,1] == 'Q' && Mat[2,1] == 'Q')
            {
              List<int> abc = new List<int>();
              abc.Add(2);
              abc.Add(2);
                return abc;
            }
        }
    }
 
    for (int r = 0; r < n; r++) {
        for (int c = 0; c < n; c++) {
 
            if (r == 0 && c == 0) {
                if (Mat[r,c] == 'Q'
                    && Mat[r + 1,c] == 'Q'
                    && Mat[r,c + 1] == 'Q'
                    && Mat[r + 1,c + 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (r == 0 && c == (n - 1)) {
                if (Mat[r,c] == 'Q'
                    && Mat[r + 1,c] == 'Q'
                    && Mat[r,c - 1] == 'Q'
                    && Mat[r + 1,c - 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (r == (n - 1) && c == 0) {
                if (Mat[r,c] == 'Q'
                    && Mat[r - 1,c] == 'Q'
                    && Mat[r,c + 1] == 'Q'
                    && Mat[r - 1,c + 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (r == (n - 1) && c == (n - 1)) {
                if (Mat[r,c] == 'Q'
                    && Mat[r - 1,c] == 'Q'
                    && Mat[r,c - 1] == 'Q'
                    && Mat[r - 1,c - 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (c == 0) {
                if (Mat[r,c] == 'Q'
                    && Mat[r + 1,c] == 'Q'
                    && Mat[r - 1,c] == 'Q'
                    && Mat[r + 1,c + 1] == 'Q'
                    && Mat[r - 1,c + 1] == 'Q'
                    && Mat[r,c + 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (c == (n - 1)) {
                if (Mat[r,c] == 'Q'
                    && Mat[r + 1,c] == 'Q'
                    && Mat[r - 1,c] == 'Q'
                    && Mat[r + 1,c - 1] == 'Q'
                    && Mat[r - 1,c - 1] == 'Q'
                    && Mat[r,c - 1] == 'Q')
                {
                  List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (r == 0) {
                if (Mat[r,c] == 'Q'
                    && Mat[r,c + 1] == 'Q'
                    && Mat[r,c - 1] == 'Q'
                    && Mat[r + 1,c + 1] == 'Q'
                    && Mat[r + 1,c - 1] == 'Q'
                    && Mat[r + 1,c] == 'Q')
                {
                List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
            if (r == (n - 1)) {
                if (Mat[r,c] == 'Q'
                    && Mat[r,c + 1] == 'Q'
                    && Mat[r,c - 1] == 'Q'
                    && Mat[r - 1,c + 1] == 'Q'
                    && Mat[r - 1,c - 1] == 'Q'
                    && Mat[r - 1,c] == 'Q')
                {
                List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
                     
            }
            if (Mat[r,c] == 'Q') {
                if (Mat[r - 1,c - 1] == 'Q'
                    && Mat[r - 1,c + 1] == 'Q'
                    && Mat[r + 1,c - 1] == 'Q'
                    && Mat[r + 1,c + 1] == 'Q'
                    && Mat[r,c - 1] == 'Q'
                    && Mat[r,c + 1] == 'Q'
                    && Mat[r - 1,c] == 'Q'
                    && Mat[r + 1,c] == 'Q')
                {
                List<int> abc = new List<int>();
                  abc.Add(r + 1);
                  abc.Add(c+1);
                    return abc;
                }
            }
        }
    }
 
    // If the position of the queen
    // cannot be determined
  List<int> a = new List<int>();
                  a.Add(-1);
                  a.Add(-1);
                    return a;
}
 
 
    static public void Main (){
 
        // Code
      int N = 3;
    char[,] v = { { 'Q', 'Q', 'Q' },
                                { 'Q', 'Q', 'Q' },
                                { 'Q', 'Q', 'Q' } };
 
    // Function call
      List<int> p = new List<int>();
       p = solve(v, N);
    Console.WriteLine(p[0]+" "+p[1]);
       
    }
}
// this code is contributed by ksam24000


Javascript




<script>
    // JavaScript code for the above approach:
 
    // Function to find the position of the queen
    const solve = (Mat, n) => {
        if (n == 1) {
            return [1, 1];
        }
 
        if (n == 2) {
            return [-1, -1];
        }
 
        if (n == 3) {
            if (Mat[1][1] == 'Q') {
                if (Mat[0][0] == 'Q' && Mat[0][2] == 'Q'
                    && Mat[2][0] == 'Q' && Mat[2][2] == 'Q'
                    && Mat[1][0] == 'Q' && Mat[1][2] == 'Q'
                    && Mat[0][1] == 'Q' && Mat[2][1] == 'Q')
                    return [2, 2];
            }
        }
 
        for (let r = 0; r < n; r++) {
            for (let c = 0; c < n; c++) {
 
                if (r == 0 && c == 0) {
                    if (Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (r == 0 && c == (n - 1)) {
                    if (Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (r == (n - 1) && c == 0) {
                    if (Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (r == (n - 1) && c == (n - 1)) {
                    if (Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (c == 0) {
                    if (Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r] == 'Q')
                        return [r + 1, c + 1];
                }
                if (c == (n - 1)) {
                    if (Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r] == 'Q')
                        return [r + 1, c + 1];
                }
                if (r == 0) {
                    if (Mat[r] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r + 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (r == (n - 1)) {
                    if (Mat[r] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r - 1] == 'Q')
                        return [r + 1, c + 1];
                }
                if (Mat[r] == 'Q') {
                    if (Mat[r - 1] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r + 1] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r] == 'Q'
                        && Mat[r - 1] == 'Q'
                        && Mat[r + 1] == 'Q')
                        return [r + 1, c + 1];
                }
            }
        }
 
        // If the position of the queen
        // cannot be determined
        return [-1, -1];
    }
 
    // Driver code
 
    let N = 3;
    let v = [['Q', 'Q', 'Q'],
    ['Q', 'Q', 'Q'],
    ['Q', 'Q', 'Q']];
 
    // Function call
    let p = solve(v, N);
    document.write(`${p[0]} ${p[1]}`);
 
// This code is contributed by rakeshsahni
 
</script>


Output

2 2

Time Complexity: O(N*N)
Auxiliary Space: O(1)



Last Updated : 14 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads