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
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package _5월_1주차;

import java.util.*;
import java.io.*;

public class Main_백준_22255_호석사우르스_골드2_176ms {
static int n, m, sx, sy, ex, ey;
static int[][] map;

static class Node implements Comparable<Node> {
int x, y, no, w;

public Node(int x, int y, int no, int w) {
this.x = x;
this.y = y;
this.no = no;
this.w = w;
}

@Override
public int compareTo(Node o) {
return this.w - o.w;
}
}

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());
st = new StringTokenizer(br.readLine());
sx = Integer.parseInt(st.nextToken()) - 1;
sy = Integer.parseInt(st.nextToken()) - 1;
ex = Integer.parseInt(st.nextToken()) - 1;
ey = Integer.parseInt(st.nextToken()) - 1;

map = new int[n][m];
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < m; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}

System.out.println(solve());

}

static int[] dx = { 1, -1, 0, 0 };
static int[] dy = { 0, 0, 1, -1 };

private static int solve() {
PriorityQueue<Node> pq = new PriorityQueue<>();
pq.offer(new Node(sx, sy, 1, 0));
int[][][] visited = new int[n][m][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
Arrays.fill(visited[i][j], -1);
}
}
visited[sx][sy][1] = 0;
Node cur;
while (!pq.isEmpty()) {
cur = pq.poll();
if (visited[cur.x][cur.y][cur.no] != -1 && visited[cur.x][cur.y][cur.no] < cur.w) {
continue;
}
for (int i = 0; i < 4; i++) {
if (cur.no == 1 && i > 1) {
continue;
} else if (cur.no == 2 && i < 2) {
continue;
}
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= m || map[nx][ny] == -1)
continue;
int no = (cur.no + 1) % 3;
if (visited[nx][ny][no] == -1 || visited[nx][ny][no] > cur.w + map[nx][ny]) {
visited[nx][ny][no] = cur.w + map[nx][ny];
pq.offer(new Node(nx, ny, no, visited[nx][ny][no]));
}
}
}
int min = Integer.MAX_VALUE;
for (int i = 0; i < 3; i++) {
if (visited[ex][ey][i] == -1)
continue;
min = Math.min(min, visited[ex][ey][i]);
}
return min == Integer.MAX_VALUE ? -1 : min;
}
}
35 changes: 35 additions & 0 deletions _5월_1주차/프로그래머스_미로탈출명령어.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package _5월_1주차;

public class 프로그래머스_미로탈출명령어 {
int n, m, x, y, r, c, k;
String answer = "";
public String solution(int N, int M, int X, int Y, int R, int C, int K) {
n = N; m = M; x = X-1; y = Y-1; r = R-1; c = C-1; k = K;
dfs(0, x, y, "");
return "".equals(answer)? "impossible" : answer;
}

int[] dx = {1, 0, 0, -1};
int[] dy = {0, -1, 1, 0};
char[] dir = {'d', 'l', 'r', 'u'};
void dfs(int depth, int i, int j, String path) {
if(!"".equals(answer)) return;
if(depth == k) {
if(i==r && j==c) answer = path;
return;
}
int dist = Math.abs(i-r)+Math.abs(j-c);
int cnt = k-depth;
// 남은 거리가 갈 수 있는 횟수보다 클 때
if(dist > cnt) return;
// 남은 거리와 갈 수 있는 횟수의 차이가 홀수 일 때
if(Math.abs(dist-cnt)%2 == 1) return;

for(int d=0; d<4; d++) {
int nx = i+dx[d];
int ny = j+dy[d];
if(nx<0||nx>=n||ny<0||ny>=m) continue;
dfs(depth+1, nx, ny, path+dir[d]);
}
}
}
5 changes: 5 additions & 0 deletions _5월_1주차/프로그래머스_사라지는발판.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package _5월_1주차;

public class 프로그래머스_사라지는발판 {

}
55 changes: 55 additions & 0 deletions _5월_1주차/프로그래머스_양궁대회.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package _5월_1주차;

class 프로그래머스_양궁대회 {
int n, max;
int[] info;
int[] answer = new int[11];

public int[] solution(int N, int[] Info) {
n = N;
info = Info;
int[] selected = new int[11];
combination(0,0,selected);
return max == 0 ? new int[]{-1} : answer;
}

void combination(int depth, int start, int[] selected) {
if(depth == n) {
check(selected);
return;
}
for(int i=start; i<11; i++) {
selected[i]++;
combination(depth+1, i, selected);
selected[i]--;
}
}

void check(int[] selected) {
int sub = 0;
for(int i=0; i<11; i++) {
if(info[i] < selected[i]) {
sub += 10-i;
} else if(info[i] != 0) {
sub -= 10-i;
}
}
// 점수 차이가 최대
if(sub > 0) {
if(max < sub) {
max = sub;
System.arraycopy(selected, 0, answer, 0, 11);
} else if(max == sub) {
// 가장 낮은 점수를 더 많이 맞힌 경우
for(int i=10; i>=0; i--) {
if(selected[i] > answer[i]) {
System.arraycopy(selected, 0, answer, 0, 11);
break;
}
else if(selected[i] < answer[i]) break;
}
}
}
}

}
58 changes: 58 additions & 0 deletions _5월_1주차/프로그래머스_이모티콘할인행사.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package _5월_1주차;

class 프로그래머스_이모티콘할인행사 {
int[][] users;
int[] emoticons;
int maxCnt, maxTotal;

public int[] solution(int[][] Users, int[] Emoticons) {
int[] answer = {};
users = Users;
emoticons = Emoticons;

// 1. 이모티콘 할인 경우의 수 만들기
// 2. 모든 유저에 대해 플러스 가입 수 계산
// 3. 가입 수 동일한 경우 매출액 최대 선택

int[] selected = new int[emoticons.length];
permutation(0, selected);

return new int[]{maxCnt, maxTotal};
}

void permutation(int depth, int[] selected) {
if(depth == emoticons.length) {
check(selected);
return;
}
for(int i=1; i<=4; i++) {
selected[depth] = i*10;
permutation(depth+1, selected);
}
}

void check(int[] selected) {
int[] e = new int[emoticons.length];
// 이모티콘 할인 계산
for(int i=0; i<emoticons.length; i++) {
e[i] = (int)(emoticons[i]*(100 - selected[i])/100);
}
int cnt = 0, total = 0;
for(int i=0; i<users.length; i++) {
int sum = 0;
for(int j=0; j<emoticons.length; j++) {
if(users[i][0] <= selected[j]) {
sum += e[j];
}
}
if(sum >= users[i][1]) cnt++;
else total += sum;
}
if(cnt > maxCnt) {
maxCnt = cnt;
maxTotal = total;
} else if(cnt==maxCnt && total > maxTotal) {
maxTotal = total;
}
}
}
61 changes: 61 additions & 0 deletions _5월_1주차/프로그래머스_표현가능한이진트리.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package _5월_1주차;

import java.util.Arrays;

public class 프로그래머스_표현가능한이진트리 {
public static void main(String[] args) {
System.out.println(Arrays.toString(solution(new long[] {42})));
}

static long[] pow2;
public static int[] solution(long[] numbers) {
int[] answer = new int[numbers.length];

pow2 = new long[51];
pow2[0] = 1;
for(int i=1; i<51; i++) {
pow2[i] = pow2[i-1] * 2;
}

for(int i=0; i<numbers.length; i++) {
answer[i] = solve(numbers[i]);
}

return answer;
}

static int flag;
static int solve(long num) {
int cnt = 0; // 노드 개수
for(; cnt<51; cnt++) {
if(pow2[cnt]-1 >= num) break;
}
int h = 0; // 트리 높이
for(; h<51; h++) {
if(pow2[h]-1 >= cnt) break;
}
cnt = (int)(pow2[h]-1);
String nodes = Long.toBinaryString(num);
int len = nodes.length();
for(int i=0; i<cnt-len; i++) nodes = "0" + nodes;

flag = 1;
check(0, cnt-1, nodes);
return flag;
}

static int check(int start, int end, String nodes) {
if(flag==0) return -1;
if(start >= end) return nodes.charAt(start) - '0';

int mid = start + (end-start)/2;
int left = check(start, mid-1, nodes);
int right = check(mid+1, end, nodes);
if(nodes.charAt(mid) == '0' && (left==1 || right==1)) flag = 0;

if(left==1) return 1;
else if(nodes.charAt(mid) == '1') return 1;
else if(right==1) return 1;
return 0;
}
}