1. ๋ฌธ์ ์ค๋ช ๐
(1) ๋งํฌ๐
https://www.acmicpc.net/problem/9663
(2) ํด์ค๐ต
์ฒด์ค์์ Queen
์ 8๋ฐฉํฅ์ ๊ฐ ์ ์๋ ๋ง์ด๋ค. (์ฌ๋ฐฉ ํ์ + ์ฌ๋ฐฉ์ ๋๊ฐ)
์ฒด์ค๋ ์์ ์ด ํ ๋ฒ์ ์์ง์์ผ๋ก ๊ฐ ์ ์๋ ๊ฒฝ๋ก์ ์๋ ๋ง์ ์ฃฝ์ผ ์ ์๋ค.
์ด Queen์ ๋ชจ๋ ํ์ ํ๋์ฉ ๋ ๋, ์๋ก๊ฐ ์๋ก๋ฅผ ์ฃฝ์ผ ์ ์๊ฒ ๋๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ๋ ๊ฒ์ด๋ค.
2. ์๊ฐ์ ํ๋ฆ: ์ฝ๋๊ฐ ๋์ค๊ธฐ๊น์ง ๐๏ธ
(1) IDEA ๋์ถ๐ก
KEY WORD
: BACK TRACKING
N
์ ์ต๋๊ฐ์ด 15๋ผ์, 1515 ์ฒด์คํ์์ ๋ฐฑํธ๋ํน์ ํด๋ ์๊ฐ ์ด๊ณผ๊ฐ ๋์ง ์๋๋ค. ๋ฐ๋ผ์ *๋ฐฑ ํธ๋ํน
**์ ํ์ฉํ๋ค.
๊ฒฝ๋ก๊ฐ ๊ฒน์น๋์ง ํ์ธํ๊ธฐ ์ํด์๋ ๋ค์ 4๊ฐ์ง๋ฅผ ํ์ธํด์ผ ํ๋ค.
- 1๏ธโฃ ํ์ฌ Queen์ ๋์ผ๋ ค๋ ํ์ ์ด๋ฏธ ๋ค๋ฅธ Queen์ด ์กด์ฌํ๋ค.
- 2๏ธโฃ ํ์ฌ Queen์ ๋์ผ๋ ค๋ ์ด์ ์ด๋ฏธ ๋ค๋ฅธ Queen์ด ์กด์ฌํ๋ค.
- 3๏ธโฃ ํ์ฌ Queen์ ๋์ผ๋ ค๋ ๊ณณ์ ์ค๋ฅธ์ชฝ ์ฌ์ ๋๊ฐ ๊ฒฝ๋ก์ Queen์ด ์กด์ฌํ๋ค.
- 4๏ธโฃ ํ์ฌ Queen์ ๋์ผ๋ ค๋ ๊ณณ์ ์ผ์ชฝ ์ฌ์ ๋๊ฐ ๊ฒฝ๋ก์ Queen์ด ์กด์ฌํ๋ค.
1๏ธโฃ๋ฒ์ ๊ฒฝ์ฐ, ์ด์งํผ ํ๋ณ๋ก ํ๋์ Queen๋ง์ ๋์ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ๋ฐ๋ก ์ ๊ฒํ ํ์๊ฐ ์๋ค.2๏ธโฃ3๏ธโฃ4๏ธโฃ ๋ฒ์ ๊ฒฝ์ฐ, queen์ ๋๋ ๊ฒฝ๋ก์ ์ด, ์ข ์ฌ์ , ์ฐ ์ฌ์ ์ ํ์ธํด์ผ ํ๋ค.
(2) SUDO CODE ๐
- 0๏ธโฃ
์ธํ
: NN ์ฒด์คํ์ ๋ง๋ ๋ค. ์ฐ๋ฆฌ๋ ํ๋ง๋ค ํ๋์ queen์ ๋์ผ๋ฉด์ ๋ค์ ํ์ผ๋ก ์ด๋ํ ๊ฒ์ด๋ค. ๋ง์ฝ *๋ชจ๋ ํ์ queen์ ๋์๋ค๋ฉด, ํด๋น ๊ฒฝ์ฐ์ ์๋ N-queen ๋ฌธ์ ์ ๋ต์ด ๋๋ ๊ฒฝ์ฐ์ ์์ด๋ฏ๋ก cnt + 1์ ํ๊ณ ๋์ด๊ฐ๋ค.** - 1๏ธโฃ ํ์ ํ๋ ์กฐํํ๋ค.
- 2๏ธโฃ ํด๋น ํ์ ์ด ์ค ํ๋์ queen์ ๋๋๋ค.
- 3๏ธโฃ ํด๋น ์ขํ์ queen์ ๋์์ ์, ์ด, ์ข ์ฌ์ , ์ฐ ์ฌ์ ์ ์๋๋ ๊ฒฝ์ฐ๊ฐ ์๋์ง ํ์ธํ๋ค.
- 4๏ธโฃ ๊ทธ๋ฌํ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ฉด, ์ด์ ํ์ผ๋ก ์ฌ๊ท๋ฅผ ํตํด ๋๋์๊ฐ๋ค. ๋ง์ฝ์ ์๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ฉด ๋ค์ ํ์ผ๋ก ๋์ด๊ฐ๋ค.
- 5๏ธโฃ ๋ง์ง๋ง ํ๊น์ง Queen์ ๋๋ ๊ฒฝ์ฐ์ ์์ ๊ฐ์๋ฅผ ์ผ๋ค.
(3) ์๊ฐ๋ณต์ก๋ ๋ถ์ ๐
N
์ ํฌ๊ธฐ๊ฐ ์ต๋ 15๋ผ์ ์๊ฐ๋ณต์ก๋๋ฅผ ๋ถ์ํ๋ ํ์๊ฐ ์๋ฏธ๊ฐ ์๋ค.
3. ๊ตฌํ ์ฝ๋ ๐
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main {
static int N, sum = 0;
static int [][] boards;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
boards = new int [N][N];
garo(0);
System.out.println(sum);
}
public static void garo (int depth) {
if(depth == N) {
sum++;
return;
}
for (int i = 0; i < N; i++) {
if(!sero(depth,i)) continue;
if(!right_deGak(depth, i)) continue;
if(!left_deGak(depth, i)) continue;
boards[depth][i] = 1;
garo(depth+1);
boards[depth][i] = 0;
}
}
public static boolean sero (int depth, int now) {
for (int i = 0; i <= depth; i++) {
if(boards[i][now] == 1) return false;
}
return true;
}
public static boolean right_deGak (int depth, int now) {
int row = depth; int col = now;
while (OOB(row,col) && row >= 0 && col >= 0) {
if(boards[row][col] == 0) {
row--; col--;
}
else return false;
}
row = depth; col = now;
while (row < N && col < N){
if(OOB(row,col) && boards[row][col] == 0) {
row++; col++;
}
else return false;
}
return true;
}
public static boolean left_deGak (int depth, int now) {
int row = depth; int col = now;
while (OOB(row,col) && row >= 0 && col < N) {
if(boards[row][col] == 0) {
row--; col ++;
}
else return false;
}
row = depth; col = now;
while (OOB(row,col) && row < N && col >= 0){
if(boards[row][col] == 0) {
row ++; col --;
}
else return false;
}
return true;
}
public static boolean OOB (int row, int col){
return row >= 0 && col >= 0 && row < N && col < N;
}
}
class Solution {
static int N;
static int [][] rate;
static int [] nums;
public void solution () throws IOException {
input();
}
public void input () throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
rate = new int [N][4];
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
rate[i][0] = Integer.parseInt(st.nextToken());
rate[i][1] = Integer.parseInt(st.nextToken());
rate[i][2] = Integer.parseInt(st.nextToken());
rate[i][3] = Integer.parseInt(st.nextToken());
}
}
public void cal_rate () {
nums = new int [N];
for (int i = 0; i < N; i++) {
int A = rate[i][0];
int B = rate[i][1];
int a_rate = rate[i][2];
int b_rate = rate[i][3];
int GCD = GCD(a_rate, b_rate);
if(nums[A] == 0 && nums[B] == 0) {
nums[A] = a_rate/GCD;
nums[B] = b_rate/GCD;
}
}
}
public int GCD(int a, int b) {
int r = a%b;
while (r != 0){
a = b;
b = r;
r = a%b;
}
return b;
}
}
4. ๋ฐฐ์ด ๊ฒ๋ค ๐ฏ
back-tracking ๋๋ฌด ํ๊ธฐ ์ซ๋ค!!!!!!!!!!!!