Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package JavaProModule.JavaAdvanced.MultidimensionalArraysLab;
- import java.util.Scanner;
- public class FindTheRealQueen_07 {
- public static void main(String[] args) {
- Scanner read = new Scanner(System.in); trimReader(read);
- int rows = 8;
- int cols = 8;
- char[][] board = new char[rows][cols];
- for (int row = 0; row < board.length; row++) {
- for (int col = 0; col < board[row].length; col++) {
- if (board[row][col] == 'q' && isValidQ(board, row, col)) {
- System.out.println(row + " " + col);
- }
- }
- }
- }
- private static String trimReader(Scanner read) {
- String input = read.nextLine().trim();
- while(input.length() == 0){
- input = read.nextLine().trim();
- }
- return input;
- }
- private static char[][] readMatrix(Scanner read, int cols, int rows) {
- char [][] myMatrix = new char[rows][cols];
- for (int row = 0; row < rows; row++) {
- String input = trimReader(read);
- String line = input.replaceAll("\\s+","");
- myMatrix[row] = line.toCharArray();
- }
- return myMatrix;
- }
- private static boolean isValidQ(char[][] board, int row, int col) {
- for (int rowDirect = -1; rowDirect <= 1; rowDirect++) {
- for (int colDirect = -1; colDirect <= 1; colDirect++) {
- if (rowDirect == 0 && colDirect == 0){
- continue;
- }
- int currentRow = row + rowDirect;
- int currentCol = col + colDirect;
- boolean inBounds = isInBounds(board, currentRow, currentCol);
- while (inBounds){
- if (board[currentRow][currentCol] == 'q'){
- return false;
- }
- currentRow = currentRow + rowDirect;
- currentCol = currentCol + colDirect;
- inBounds = isInBounds(board,currentRow,currentCol);
- }
- }
- }
- return false;
- }
- private static boolean isInBounds(char[][] board, int currentRow, int currentCol) {
- return currentRow >= 0 && currentRow < board.length && currentCol >= 0
- && currentCol < board[currentRow].length;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement