Count of rooks that can attack each other out of K rooks placed on a N*N chessboard - GeeksforGeeks

07 Jan, 2022
Given pair of coordinates of K rooks on an N X N chessboard, the task is to count the number of rooks that can attack each other. Note: 1 <= K <= N*NExamples:
Input: K = 2, arr[][] = { {2, 2}, {2, 3} }, N = 8Output: 2Explanation: Both the rooks can attack each other, because they are in the same row. Therefore, count of rooks that can attack each other is 2 
Input: K = 1, arr[][] = { {4, 5} }, N = 4Output: 0
Approach: The task can easily be solved using the fact that, 2 rooks can attack each other if they are either in the same row or in the same column, else they can’t attack each other.
Below is the implementation of the above code:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to count the number of attacking rooksint willAttack(vector<vector<int> >& arr, int k, int N){    int ans = 0;     for (int i = 0; i < k; i++) {        for (int j = 0; j < k; j++) {             if (i != j) {                 // Check if rooks are in same row                // or same column                if ((arr[i][0] == arr[j][0])                    || (arr[i][1] == arr[j][1]))                    ans++;            }        }    }     return ans;} // Driver Codeint main(){    vector<vector<int> > arr = { { 2, 2 }, { 2, 3 } };    int K = 2, N = 8;    cout << willAttack(arr, K, N);    return 0;}
import java.io.*;import java.util.*; class Solution {  static int willAttack(int arr[][], int k, int N)  {    int ans = 0;     for (int i = 0; i < k; i++) {      for (int j = 0; j < k; j++) {         if (i != j) {           // Check if rooks are in same row          // or same column          if ((arr[i][0] == arr[j][0])              || (arr[i][1] == arr[j][1]))            ans++;        }      }    }     return ans;  }     // Driver code  public static void main(String[] args)  {    int[][] arr = { { 2, 2 }, { 2, 3 } };    int K = 2, N = 8;    System.out.println(willAttack(arr, K, N));  }} // This code is contributed by dwivediyash.
# python program for the above approach # Function to count the number of attacking rooksdef willAttack(arr, k, N):     ans = 0     for i in range(0, k):        for j in range(0, k):             if (i != j):                 # Check if rooks are in same row                # or same column                if ((arr[i][0] == arr[j][0])                        or (arr[i][1] == arr[j][1])):                    ans += 1     return ans # Driver Codeif __name__ == "__main__":     arr = [[2, 2], [2, 3]]    K = 2    N = 8    print(willAttack(arr, K, N))     # This code is contributed by rakeshsahni
using System;class Solution{  static int willAttack(int[,] arr, int k, int N)  {    int ans = 0;     for (int i = 0; i < k; i++)    {      for (int j = 0; j < k; j++)      {         if (i != j)        {           // Check if rooks are in same row          // or same column          if ((arr[i, 0] == arr[j, 0])              || (arr[i, 1] == arr[j, 1]))            ans++;        }      }    }     return ans;  }   // Driver code  public static void Main()  {    int[,] arr = { { 2, 2 }, { 2, 3 } };    int K = 2, N = 8;    Console.WriteLine(willAttack(arr, K, N));  }} // This code is contributed by gfgking
<script>      // JavaScript code for the above approach       // Function to count the number of attacking rooks      function willAttack(arr, k, N) {          let ans = 0;           for (let i = 0; i < k; i++) {              for (let j = 0; j < k; j++) {                   if (i != j) {                       // Check if rooks are in same row                      // or same column                      if ((arr[i][0] == arr[j][0])                          || (arr[i][1] == arr[j][1]))                          ans++;                  }              }          }           return ans;      }       // Driver Code      let arr = [[2, 2], [2, 3]];      let K = 2, N = 8;      document.write(willAttack(arr, K, N)); // This code is contributed by Potta Lokesh  </script>
2
Time Complexity: O(K*K)Auxiliary Space: O(1)
lokeshpotta20
dwivediyash
rakeshsahni
gfgking
Algo-Geek 2021
chessboard-problems
Algo Geek
Greedy
Matrix
Greedy
Matrix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of operation required to water all the plants
Lexicographically smallest string formed by concatenating any prefix and its mirrored form
Divide given number into two even parts
Check if an edge is a part of any Minimum Spanning Tree
Check if the given string is valid English word or not
Dijkstra's shortest path algorithm | Greedy Algo-7
Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
Prim’s Minimum Spanning Tree (MST) | Greedy Algo-5
Program for array rotation
Huffman Coding | Greedy Algo-3