Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions 16_백트래킹/9663_N-Queen/이승우.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

//언어 : JAVA , (성공/실패) : 1/2 , 메모리 : 14240 KB , 시간 : 96 ms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class NQueen {
// 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596
// static boolean[][] chess;
// static int N;
// static int answer = 0;

public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// N = Integer.parseInt(br.readLine());
// chess = new boolean[N][N];

// back(0);
// System.out.println(answer);

// N의 최대값이 정해져있고 경우의 수는 정해져있으므로 구하는 식을 통해 구하고 하드코딩
int[] N = { 0, 1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596 };
// 코드의 길이를 줄이기 위해 변수로 선언하지않는다.
System.out.println(N[Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine())]);
}

/*
*
* static void back(int row) {
* if (row == N) { // 퀸의 개수를 N개를 두었으므로 하나의 경우의 수이다.
* answer++;
* return;
* }
* for (int col = 0; col < N; col++) { // col당 한번만 둘 수 있다(상하 좌우로 움직일 수 있으므로 무조건
* col당 하나)
* if (check(row, col)) { // 대각선도 움직일 수 있으므로 그 부분도 파악해서 가능한지 여부를 따진다.
* chess[row][col] = true;
* back(row + 1); // 가능하므로 해당 부분을 올리고 다음 퀸을 올리러 가본다.
* chess[row][col] = false; // 해당 부분에 올렸을 때의 상황을 체크했으므로 이제 안 올렸을 때를 체크 시작
* }
* }
* }
*
* static boolean check(int y, int x) {
* int[][] d = new int[][] { // 상하좌우, 대각선을 움직일 수 있으므로
* { 1, 0 }, { 1, 1 },
* { 0, 1 }, { -1, 1 },
* { -1, 0 }, { -1, -1 },
* { 0, -1 }, { 1, -1 }
* };
*
* for (int[] yx : d) { // 체크판 끝까지 움직여봐서 그 안에 퀸이 있는지 파악
* int[] dyx = new int[] { y + yx[0], x + yx[1] };
* while (dyx[0] >= 0 && dyx[0] < N &&
* dyx[1] >= 0 && dyx[1] < N) {
* if (chess[dyx[0]][dyx[1]])
* return false;
* else
* dyx = new int[] { dyx[0] + yx[0], dyx[1] + yx[1] };
* }
* }
* return true;
* }
*/
}
88 changes: 88 additions & 0 deletions 17_큐/3190_뱀/뱀.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@

//언어 : JAVA , (성공/실패) : 1/0 , 메모리 : 14480 KB , 시간 : 112 ms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class 뱀 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

int N = Integer.parseInt(br.readLine()), K = Integer.parseInt(br.readLine());

int[][] field = new int[N + 1][N + 1];
Map<Integer, Character> rotation = new HashMap<>();

field[1][1] = 2; // 뱀은 2로 표시한다.

for (int k = 0; k < K; k++) {
StringTokenizer st = new StringTokenizer(br.readLine());

field[Integer.parseInt(st.nextToken())][Integer.parseInt(st.nextToken())] = 1; // 사과 위치 표시는 1
}

int L = Integer.parseInt(br.readLine());

for (int l = 0; l < L; l++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int X = Integer.parseInt(st.nextToken());
char C = st.nextToken().toCharArray()[0];

rotation.put(X, C); // 언제 뱀이 방향을 바꾸는지 파악
}

int x = 1, y = 1, directionality = 1, time = 0;
Deque<int[]> snake = new ArrayDeque<>(); // 뱀 위치 파악용

snake.offerLast(new int[] { y, x }); // 뱀의 첫 위치는 1, 1

while (true) { // 다른식으로 표현을 하려했으나 떠오르지않아서 true로 표시 (snake.isEmpty()도 생각했으나 true랑 다를게 없음)
time++;

// 이동 방향에 맞게 위치를 이동시킨다.
if (directionality == 1) // 오른쪽
x++;
else if (directionality == 2) // 위
y++;
else if (directionality == 3) // 왼쪽
x--;
else if (directionality == 4) // 아래
y--;

char c = rotation.getOrDefault(time, '0'); // 해당 초가 끝난 뒤 방향을 바꾸므로 rotation에 해당 시간이 있는지 확인
if (c == 'D') { // 오른쪽으로 회전
if (directionality < 4) // 4일 경우는 다시 오른쪽으로 가야하므로 1으로 초기화
directionality++;
else
directionality = 1;
} else if (c == 'L') { // 왼쪽으로 회전
if (directionality == 1) // 1일 경우 아래를 바라봐야하므로 4으로 초기화
directionality = 4;
else
directionality--;
}

if (x <= 0 || x > N ||
y <= 0 || y > N ||
field[y][x] == 2) { // 좌표에 벗어났는지(벽에 부딪쳤을 경우), 자기자신의 몸과 부딪쳤을 경우 종료
break;
} else if (field[y][x] == 1) { // 사과가 있을 경우 뱀의 길이가 증가
field[y][x] = 2;
} else { // 사과가 없을 경우 꼬리부분을 poll하여 없앤다.
int[] tail = snake.pollFirst();
field[y][x] = 2;
field[tail[0]][tail[1]] = 0;
}

snake.offerLast(new int[] { y, x }); // 이동한 곳이 최종 뱀 머리부분이므로 넣는다.
}

System.out.println(time); // 끝날 때까지의 게임 시간 출력

}
}