forked from wotjd4305/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswe2806.java
More file actions
74 lines (60 loc) · 1.67 KB
/
swe2806.java
File metadata and controls
74 lines (60 loc) · 1.67 KB
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
72
73
74
import java.util.Arrays;
import java.util.HashMap;
import java.util.Scanner;
public class swe2806 {
static Scanner sc = new Scanner(System.in);
static int answer=0;
static int col[];
static int N;
public static void main(String[] args) {
int t = sc.nextInt();
//N개의 퀸이 N*N배열
for(int T=1; T<=t; T++) {
answer = 0;
N = sc.nextInt();
//같은 행에 하나만 가지고있으면되니까
col = new int[N+1];
//솔루션, dfs
solution();
System.out.println("#" + T + " " + answer);
}
}
public static int solution()
{
//0행 i열
for(int i=1; i<=N; i++) {
col[1] = i;
dfs(col, 1);
}
return answer;
}
//여러 선택지중 한 행에 놓았으면, 다음 행에 놓고를 반복
public static void dfs(int col[], int row)
{
//다 놓았으면 끝냄
if(row == N)
answer++;
else {
//놓일수잇는가? 다돌려보자
for (int i = 1; i <= N; i++) {
//가능하면 다음 행
col[row+1] = i;
if (isPossible(col, row + 1)) {
dfs(col, row + 1);
}
}
}
}
public static Boolean isPossible(int Arr[], int row)
{
for(int i=1; i < row; i++) {
// 같으면
if(Arr[i] == Arr[row])
return false;
//대각선
if(Math.abs(i - row) == Math.abs(Arr[i] - Arr[row]))
return false;
}
return true;
}
}