Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- class Set{
- int[] row, col, diag1, diag2;
- int row_t, col_t, diag1_t, diag2_t, size;
- Set(int n)
- {
- row = new int[n];
- col = new int[n];
- diag1 = new int[n];
- diag2 = new int[n];
- size = n;
- Arrays.fill(diag1, -n);
- Arrays.fill(diag2, -n);
- Arrays.fill(row, -n);
- Arrays.fill(col, -n);
- row_t = col_t = diag1_t = diag2_t = -1;
- }
- boolean isPresent(int[] arr, int n)
- {
- for(int i = 0; i < size; i++)
- {
- if(arr[i] == n)
- return true;
- }
- return false;
- }
- void rowAdd(int n)
- {
- if(!isPresent(row, n))
- row[++row_t] = n;
- }
- void colAdd(int n)
- {
- if(!isPresent(col, n))
- col[++col_t] = n;
- }
- void diag1Add(int n)
- {
- if(!isPresent(diag1, n))
- diag1[++diag1_t] = n;
- }
- void diag2Add(int n)
- {
- if(!isPresent(diag2, n))
- diag2[++diag2_t] = n;
- }
- boolean result()
- {
- if(row_t == col_t && row_t == diag1_t && row_t == diag2_t)
- return true;
- return false;
- }
- }
- public class queen_check {
- public static void main(String Args[])
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter no. of rows or columns : ");
- int size = sc.nextInt();
- int[][] board = new int[size][size];
- Set s = new Set(size);
- for(int i = 0; i < size; i++)
- {
- System.out.print("\n\nEnter queen "+ (i+1) + " row : ");
- int r = sc.nextInt();
- System.out.print("Enter queen "+ (i+1) + " column : ");
- int c = sc.nextInt();
- board[r][c] = 1;
- }
- System.out.println("\n");
- for(int i = 0; i < size; i++)
- {
- for(int j = 0; j < size; j++)
- {
- if(board[i][j] == 1)
- System.out.print("Q\t");
- else
- System.out.print(".\t");
- }
- System.out.println("\n");
- }
- for(int i = 0; i < size; i++)
- {
- for(int j = 0; j < size; j++)
- {
- if(board[i][j] == 1)
- {
- s.rowAdd(i);
- s.colAdd(j);
- s.diag1Add(i-j);
- s.diag2Add(i+j);
- }
- }
- }
- System.out.print("\n\n r c d1 d2 : "+s.row_t+" "+s.col_t+" "+s.diag1_t+" "+s.diag2_t);
- if(s.result())
- System.out.println("\n\nSUCCESS!!!");
- else
- System.out.println("\n\nFail!");
- }
- }
Add Comment
Please, Sign In to add comment