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
29 changes: 29 additions & 0 deletions 4주차/가장큰수/가장큰수_곽미래.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package programmers;

import java.util.Arrays;

public class PGMS_가장큰수 {

public static String solution(int[] numbers) {
String[] nums = new String[numbers.length];
for(int i=0; i<numbers.length; i++) {
nums[i] = Integer.toString(numbers[i]);
}
Arrays.sort(nums, (o1, o2) -> (o1+o2).compareTo(o2+o1) * -1);

if(nums[0].equals("0")) return "0";

String answer = "";
for(int i=0; i<nums.length; i++) {
answer += nums[i];
}
return answer;
}

public static void main(String[] args) {
System.out.println(solution(new int[]{6, 10, 2}));
System.out.println(solution(new int[]{3, 30, 34, 5, 9}));
System.out.println(solution(new int[]{0, 0, 0}));
}

}
121 changes: 121 additions & 0 deletions 4주차/두동전/두동전_곽미래.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package boj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.StringTokenizer;

/**
* 문제]
* NxM 보드, 두개의 동전
* 버튼은 상하좌우 이동 4가지
* 벽 -> 동전 이동 x
* 칸이 없으면 동전은 떨어짐
* 두 동전 중 하나만 보드에서 떨어뜨리는 최소 횟수
*
* 조건]
* 1 <= N, M <= 20
* 보드 상태
* - o : 동전
* - . : 빈칸
* - # : 벽
* 두 동전을 떨어뜨릴 수 없거나 버튼을 10번보다 많이 눌러야하면 -1 출력
*
* 풀이]
* 구현 문제
* 4방향 돌면서 BFS 때려
*
*/
public class boj_16197_두동전 {
static class Point {
int r, c;
public Point(int r, int c) {
this.r = r;
this.c = c;
}
}

static class Coin{
int count;
Point coin1;
Point coin2;

public Coin(int count, Point coin1, Point coin2) {
this.count = count;
this.coin1 = coin1;
this.coin2 = coin2;
}
}

static int N, M, MIN_COUNT = 11;
static char[][] map;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());

map = new char[N][M];
Point coin1 = null;
Point coin2 = null;
for (int i = 0; i < N; i++) {
String line = br.readLine();
for (int j = 0; j < M; j++) {
map[i][j] = line.charAt(j);
if(map[i][j] == 'o') {
if(coin1 == null) coin1 = new Point(i, j);
else if(coin2 == null) coin2 = new Point(i, j);
}
}
}

dropCoin(new Coin(0, coin1, coin2));
System.out.println(MIN_COUNT == 11 ? -1 : MIN_COUNT);
}

static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, 1, 0, -1};
public static void dropCoin(Coin coin) {
boolean[][][][] visited = new boolean[N][M][N][M];
Queue<Coin> queue = new ArrayDeque<>();
queue.add(coin);
visited[coin.coin1.r][coin.coin1.c][coin.coin2.r][coin.coin2.c] = true;
while (!queue.isEmpty()) {
Coin curr = queue.poll();
if(curr.count >= 10) break;

for(int d=0; d<4; d++) {
int nr1 = curr.coin1.r + dr[d];
int nc1 = curr.coin1.c + dc[d];
int nr2 = curr.coin2.r + dr[d];
int nc2 = curr.coin2.c + dc[d];

if(!isOutOfRange(nr1, nc1) && map[nr1][nc1] == '#') {
nr1 = curr.coin1.r;
nc1 = curr.coin1.c;
}
if(!isOutOfRange(nr2, nc2) && map[nr2][nc2] == '#') {
nr2 = curr.coin2.r;
nc2 = curr.coin2.c;
}

if(isOutOfRange(nr1, nc1) && isOutOfRange(nr2, nc2)) continue;
else if(isOutOfRange(nr1, nc1) || isOutOfRange(nr2, nc2)) {
MIN_COUNT = Math.min(MIN_COUNT, curr.count+1);
return;
}

if(!visited[nr1][nc1][nr2][nc2]) {
queue.add(new Coin(curr.count+1, new Point(nr1, nc1), new Point(nr2, nc2)));
visited[nr1][nc1][nr2][nc2] = true;
}
}
}
}

public static boolean isOutOfRange(int r, int c) {
return r < 0 || r >= N || c < 0 || c >= M;
}
}
138 changes: 138 additions & 0 deletions 4주차/새로운게임2/새로운게임2_곽미래.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package boj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;

/**
* 문제]
* NXN 체스판, K개의 말
* 상하좌우 이동
* 한턴에서 1번 말부터 K번까지 차례로 이동
* 한 말이 이동할 때 올려져있는 말도 함께 이동
* 말이 4개 이상 쌓이는 순간 게임 종료
*
* A번 말 이동 룰
* 1. 흰색인 경우 그 칸으로 이동. 이동하려는 칸에 말이 있으면 겹침
* - 말 위에 다른 말이 있을 경우 함께 이동
* 2. 빨간색인 경우 이동한 후 A번 말과 그 위에 말들의 쌓여있는 순서를 바꾼다
* 3. 파란색인 경우 A번 말의 이동방향을 반대로 하고 한칸 이동한다. 방향을 바꾼 후 칸이 파란색이면 이동하지 않고 가만히 있는다.
* 4. 체스판을 벗어나는 경우 파란색과 같은 경우
*
* 조건]
* 4 <= N <= 12 : 체스판 크기
* 4 <= K <= 10 : 말의 개수
* 0:흰색, 1:빨간색, 2:파란색
* 이동방향 = 1:우, 2:좌, 3:상, 4:하
* 게임이 종료되는 턴의 번호 출력
* 1000보다 크거나 종료되지 않으면 -1 출력
*
* 풀이]
*
*
*/
public class boj_17837_새로운게임2 {
static int N, K;
static int[][] map, horse;
static List<Integer>[][] piece;

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());

map = new int[N+1][N+1];
piece = new List[N+1][N+1];
for (int i = 1; i <= N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 1; j <= N; j++) {
piece[i][j] = new ArrayList<>();
map[i][j] = Integer.parseInt(st.nextToken());
}
}
horse = new int[K+1][3];
for (int i = 1; i <= K; i++) {
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

horse[i][0] = r;
horse[i][1] = c;
horse[i][2] = Integer.parseInt(st.nextToken());

piece[r][c].add(i);
}

System.out.println(simulation());
}

private static int simulation() {
int[] dr = {0, 0, 0, -1, 1};
int[] dc = {0, 1, -1, 0, 0};

int count = 0;
while(count < 1000) {
count++;

for(int i=1; i<=K; i++) {
int r = horse[i][0];
int c = horse[i][1];
int d = horse[i][2];

int nr = r + dr[d];
int nc = c + dc[d];

if(isOutOfRange(nr, nc)) {
int nd = d % 2 == 0 ? d -1 : d + 1;
horse[i][2] = nd;
if(map[r+dr[nd]][c+dc[nd]] != 2) i--;
continue;
}

if(map[nr][nc] == 0) {
int idx = piece[r][c].indexOf(i);
piece[nr][nc].addAll(piece[r][c].subList(idx, piece[r][c].size()));
piece[r][c] = piece[r][c].subList(0, idx);
horse[i][0] = nr;
horse[i][1] = nc;
for(Integer j: piece[nr][nc]) {
horse[j][0] = nr;
horse[j][1] = nc;
}
} else if(map[nr][nc] == 1) {
int idx = piece[r][c].indexOf(i);
List<Integer> temp = piece[r][c].subList(idx, piece[r][c].size());
Collections.reverse(temp);

piece[nr][nc].addAll(temp);
piece[r][c] = piece[r][c].subList(0, idx);
for(Integer j: piece[nr][nc]) {
horse[j][0] = nr;
horse[j][1] = nc;
}
} else if(map[nr][nc] == 2) {
int nd = d % 2 == 0 ? d -1 : d + 1;
horse[i][2] = nd;

if(isOutOfRange(r+dr[nd], c+dc[nd])) continue;

if(map[r+dr[nd]][c+dc[nd]] != 2) i--;
}

if(piece[nr][nc].size() > 3) {
return count;
}
}
}
return -1;
}

private static boolean isOutOfRange(int nr, int nc) {
return nr < 1 || nr > N || nc < 1 || nc > N;
}
}
Loading