Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 운영체제
- kotlin
- Java8
- Brute-force
- 네트워크
- LEVEL2
- 프로젝트
- java
- 모던자바
- 스프링
- baekjoon
- Spring
- 자바
- 백트래킹
- TDD
- 알고리즘
- programmers
- DP
- 백준
- algorithm
- DFS
- backtracking
- OS
- 프로그래머스
- 그래프
- back-end
- 코틀린
- lambda
- 자료구조
- BFS
Archives
- Today
- Total
요깨비's LAB
[백준, Brute-Force, Java] P.9663 n-queen 본문
간만에 맞았던문제를 다시 풀어보았다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static int N;
static int result = 0;
static List<Tile> queenTiles = new ArrayList<>();
public static void main(String[] args) {
YoggaebReader input = new YoggaebReader();
N = input.nextInt();
doAlgorithm(0, 0);
System.out.println(result);
}
public static void doAlgorithm(int queenCount, int row) {
if(queenTiles.size() == N) {
result++;
return;
}
if (row == 0) {
for (int i = 0; i < N; i++) {
Tile tile = new Tile(row, i);
queenTiles.add(tile);
doAlgorithm(queenCount + 1, row + 1);
queenTiles.remove(queenCount);
}
} else {
for (int i = 0; i < N; i++) {
if (isAttackable(row, i)) {
continue;
} else {
Tile tile = new Tile(row, i);
queenTiles.add(tile);
doAlgorithm(queenCount + 1, row + 1);
queenTiles.remove(queenCount);
}
}
}
}
// 1 1 1
// 1 1 1
// 1 1 1
public static boolean isAttackable(int row, int col) {
for (Tile t : queenTiles) {
if(t.row == row || t.col == col) return true;
if (t.row - t.col == row - col) return true; // 대각선
if ((t.row + t.col) == (row + col)) return true; // 역대각선
}
return false;
}
static class YoggaebReader {
private BufferedReader br;
private StringTokenizer st;
public YoggaebReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
class Tile {
int row, col;
public Tile(int row, int col) {
this.row = row;
this.col = col;
}
}
row와 col을 저장한 Tile 객체를 따로 정의하여 List에 담았다가 빼면서 연산을 진행하였으나 메모리 초과로 1차원 배열로 다시 정의하여 풀었다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N;
static int result = 0;
static int[] queenTile;
public static void main(String[] args) {
YoggaebReader input = new YoggaebReader();
N = input.nextInt();
queenTile = new int[N];
for(int i=0;i<N;i++) {
queenTile[i] = -1;
}
doAlgorithm(0, 0);
System.out.println(result);
}
public static void doAlgorithm(int queenCount, int row) {
if(queenCount == N) {
result++;
return;
}
if (row == 0) {
for (int i = 0; i < N; i++) {
queenTile[row] = i;
doAlgorithm(queenCount + 1, row + 1);
queenTile[row] = -1;
}
} else {
for (int i = 0; i < N; i++) {
if (isAttackable(row, i)) {
continue;
} else {
queenTile[row] = i;
doAlgorithm(queenCount + 1, row + 1);
queenTile[row] = -1;
}
}
}
}
public static boolean isAttackable(int row, int col) {
for(int i=0;i<row;i++) {
if(i == row || queenTile[i] == col) return true;
if (i - queenTile[i] == row - col) return true; // 대각선
if ((i + queenTile[i]) == (row + col)) return true; // 역대각선
}
return false;
}
static class YoggaebReader {
private BufferedReader br;
private StringTokenizer st;
public YoggaebReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
'알고리즘(Java) > Brute-Force' 카테고리의 다른 글
[백준, Brute-Force, Java] P.2470 두 용액 (0) | 2021.10.14 |
---|---|
[백준, Brute-Force, Java] P.7795 먹을 것인가 먹힐 것인가 (0) | 2021.10.02 |
[백준, Brute-Force, Java] P.1182 부분수열의 합 (0) | 2021.09.30 |
Comments