Advertisement
AhmedGhazaly

method1

Mar 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1.  
  2. import java.io.*;
  3. import java.util.*;
  4. public class IEEE {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.         Scanner sc=new Scanner(System.in);
  8.         int n=sc.nextInt();
  9.         int x,y;
  10.         //method 1(using the 2d array);
  11.         int board[][]=new int [1001][1001];
  12.        
  13.         //filling the board with pieces
  14.         for(int i=0;i<n;i++){
  15.             x=sc.nextInt();
  16.             y=sc.nextInt();
  17.             board[x][y]=1;
  18.         }
  19.         //_______end of the input________\\
  20.        
  21.         int sum =0;//number of attacks
  22.         int counter=0;//number of pieces in every row
  23.        
  24.         //loop for the rows:
  25.         for(int i=1;i<1001;i++){
  26.             for(int j=1;j<1001;j++){
  27.                 if(board[i][j]==1)counter++;
  28.             }
  29.             sum+=(counter*(counter-1))/2;//adding the number of attack
  30.             counter=0;//reset the value to zero to start a new row
  31.         }
  32.         //loop for the colums
  33.         for(int i=1;i<1001;i++){
  34.             for(int j=1;j<1001;j++){
  35.                 if(board[j][i]==1)counter++;
  36.             }
  37.             sum+=(counter*(counter-1))/2;
  38.             counter=0;
  39.         }
  40.         System.out.println(sum);
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement