Home G9663. N-Queen
Post
Cancel

G9663. N-Queen

문제

image


제출 코드

image


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
package gold;

import java.util.Scanner;

public class G9663_NQueen {

	static int N;
	static int cnt = 0;
	static int[] isQueen;

	static void setQueen(int n) {
		if(notQueen(n-1)) return;

		if(n > N) {
			cnt++;
			return;
		}
		for(int i=1; i<=N; i++) {
			isQueen[n] = i;
			setQueen(n+1);
		}
	}

	static boolean notQueen(int n) {
		for(int i=1; i<n; i++) {
			if(isQueen[i]==isQueen[n] || n-i==Math.abs(isQueen[n]-isQueen[i])) return true;
		}
		return false;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		N = sc.nextInt();
		isQueen = new int[N+1];

		setQueen(1);

		System.out.println(cnt);
	}
}
This post is licensed under CC BY 4.0 by the author.