Day 35: 9663 (https://www.acmicpc.net/problem/9663)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | static boolean checkThreat(boolean[][] currentBoard, int xToCheck, int yToCheck) { for (Tuple<Integer> t: queensOnBoard) { if (onDiagonal(t, xToCheck, yToCheck) || onHoriVerti(t, xToCheck, yToCheck)) { return true; } } return false; } static boolean onDiagonal(Tuple queenOnBoard, int xToCheck, int yToCheck) { int xDiff = queenOnBoard.x - xToCheck; int yDiff = queenOnBoard.y - yToCheck; if (xDiff == 0) { return false; } else { return Math.abs(yDiff/xDiff) == 1.0; } } static boolean onHoriVerti(Tuple queenOnBoard, int xToCheck, int yToCheck) { int queenX = queenOnBoard.x; int queenY = queenOnBoard.y; return queenX == xToCheck || queenY == yToCheck; } | cs |
1 2 3 4 5 6 7 8 9 10 11 | static class Tuple<Integer> { public int x; public int y; public Tuple(int x, int y) { this.x = x; this.y = y; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; public class b9663 { static int N; static int numWays; static ArrayList<Tuple> queensOnBoard; static boolean addQueen(boolean[][] currentBoard, int xToAdd, int yToAdd) { if (!checkThreat(currentBoard, xToAdd, yToAdd)) { currentBoard[xToAdd][yToAdd] = true; return queensOnBoard.add(new Tuple(xToAdd, yToAdd)); } return false; } static void simulateQueens(boolean[][] board, ArrayList<Tuple> queensOnBoard, int startingX, int startingY) { for (int k = 0; k < N; k++) { int rowsCompleted = 0; for (int rowIndex = 0; rowIndex < N; rowIndex++) { for (int columnIndex = 0; columnIndex < N; columnIndex++) { if (addQueen(board, rowIndex, columnIndex)) { rowsCompleted++; break; } else if (columnIndex == N-1){ } } } if (rowsCompleted == N) numWays++; debugCase(board, N); } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); boolean[][] board = new boolean[N][N]; queensOnBoard = new ArrayList<Tuple>(); simulateQueens(board, queensOnBoard, 0, 0); } static void debugCase(boolean[][] board, int N) { System.out.println(numWays); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (board[i][j]) System.out.print(1 + " "); else System.out.print(0 + " "); } System.out.println(); } } } | cs |
abcdefg