Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class FindTheRealQueen {
- public static void main(String[] args) {
- int r = 8; int c = 8;
- char[][] board = matrixReader(r, c);
- for (int i = 0; i < r; i++){
- for (int j = 0; j < c; j++){
- if (board[i][j] == 'q'){
- if (checkNeighbours(i, j, board)) {
- System.out.println(i + " " + j);
- }
- }
- }
- }
- }
- private static char[][] matrixReader(int r, int c){
- char[][] matrix = new char[r][c];
- Scanner sc = new Scanner(System.in);
- for (int i = 0; i < r; i++){
- String[] input = sc.nextLine().split("\\s+");
- for (int j = 0; j < c; j++){
- matrix[i][j] = input[j].charAt(0);
- }
- }
- sc.close();
- return matrix;
- }
- public static boolean checkNeighbours(int r, int c, char[][] board){
- // check north
- for (int i = r - 1; i >= 0; i--){
- if (board[i][c] == 'q')
- return false;
- }
- // check south
- for(int i = r + 1; i < 8; i++){
- if (board[i][c] == 'q')
- return false;
- }
- // check west
- for(int i = c - 1; i >= 0; i--){
- if (board[r][i] == 'q')
- return false;
- }
- // check east
- for(int i = c + 1; i < 8; i++){
- if (board[r][i] == 'q')
- return false;
- }
- // check northwest
- int i = r;
- int j = c;
- while (i > 0 && j > 0){
- i--;
- j--;
- if (board[i][j] == 'q')
- return false;
- }
- // check northeast
- i = r;
- j = c;
- while (i > 0 && j < 7){
- i--;
- j++;
- if (board[i][j] == 'q')
- return false;
- }
- // check southwest
- i = r;
- j = c;
- while (i < 7 && j > 0){
- i++;
- j--;
- if (board[i][j] == 'q')
- return false;
- }
- // check southeast
- i = r;
- j = c;
- while (i < 7 && j < 7){
- i++;
- j++;
- if (board[i][j] == 'q')
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment