문제
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV7GKs06AU0DFAXB
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
이전에 백준에서 푼 문제. 풀이는 밑 링크에 가면 있다.
https://hse06028.tistory.com/202
코드
import java.util.*;
import java.io.*;
public class Main {
static int n;
static int result = 0;
static boolean up[];
static boolean downcross[];
static boolean upcross[];
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int test_case = 1; test_case <= T; test_case++)
{
n = Integer.parseInt(br.readLine());
up = new boolean [n];
downcross = new boolean [2*n -1];
upcross = new boolean [2*n -1];
queen(0);
System.out.println("#"+test_case+" "+result);
}
}
static void queen(int cnt) {
if(cnt == n) {
result++;
return;
}
for(int i=0;i<n;i++) {
if(!up[i] && !upcross[cnt+i] && !downcross[cnt - i + n - 1]) {
up[i] = true;
upcross[cnt+i] = true;
downcross[cnt - i + n - 1] = true;
queen(cnt+1);
up[i] = false;
upcross[cnt+i] = false;
downcross[cnt - i + n - 1] = false;
}
}
}
}