230915 N-Queen

Jongleee·2023년 9월 15일
0

TIL

목록 보기
365/737
private static int cnt = 0;

public int solution(int n) {
	int[] board = new int[n];
	solveNQueens(board, 0);
	return cnt;
}

private static void solveNQueens(int[] board, int row) {
	int n = board.length;
	if (row == n) {
		cnt++;
		return;
	}

	for (int col = 0; col < n; col++) {
		board[row] = col;
		if (isSafe(board, row, col)) {
			solveNQueens(board, row + 1);
		}
	}
}

private static boolean isSafe(int[] board, int row, int col) {
	for (int prevRow = 0; prevRow < row; prevRow++) {
		int prevCol = board[prevRow];
		if (prevCol == col || Math.abs(prevRow - row) == Math.abs(prevCol - col)) {
			return false;
		}
	}
	return true;
}

출처:https://school.programmers.co.kr/learn/courses/30/lessons/12952

0개의 댓글