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
41 changes: 41 additions & 0 deletions 하동준/240319/boj_13975_파일합치기3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import java.io.*;
import java.util.*;

public class Main {
static Queue<Long> answer = new LinkedList<>();

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

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

while(T-->0){
int K = Integer.parseInt(br.readLine());

StringTokenizer st = new StringTokenizer(br.readLine());

Queue<Long> q = new PriorityQueue<>();
for(int i = 0; i<K; i++)
q.offer(Long.parseLong(st.nextToken()));

fileCombine(q);
}

while(!answer.isEmpty())
System.out.println(answer.poll());
}

static void fileCombine(Queue<Long> q){
long sum = 0;

while(q.size() > 1){
long firstNum = q.poll();
long secondNum = q.poll();

sum += firstNum + secondNum;
q.offer(firstNum + secondNum);
}

answer.offer(sum);
}
}
156 changes: 156 additions & 0 deletions 하동준/240319/boj_15644_구슬탈출3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import java.io.*;
import java.util.*;

class Main {

static class Point {
int x, y;

public Point(int x, int y) {
this.x = x;
this.y = y;
}

public boolean equals(Point o) {
return this.x == o.x && this.y == o.y;
}
}

static class Element {
Point R, B;
int t;
String root;

public Element(Point R, Point B, int t, String root) {
this.R = R;
this.B = B;
this.t = t;
this.root = root;
}
}

static BufferedReader br;
static BufferedWriter bw;

static int N, M;
static int[] dx = {0,0,1,-1}; //R, L, D, U
static int[] dy = {1,-1,0,0};
static char[][] map;
static Point R, B, O;

static String nextRoot(String cur, int d) {
if (d == 0) return cur + "R";
else if (d == 1) return cur + "L";
else if (d == 2) return cur + "D";
else return cur + "U";
}

//빨강 우선인지 파랑 우선인지 확인
static boolean isRedFirst(Point R, Point B, int d) {
if (d == 0) return R.y > B.y;
else if (d == 1) return R.y < B.y;
else if (d == 2) return R.x > B.x;
else return R.x < B.x;
}

//구슬 움직임
static void move(Point target, Point other, int d) {
while (true) {
target.x += dx[d];
target.y += dy[d];
//벽일 경우
if (map[target.x][target.y] == '#') {
target.x -= dx[d];
target.y -= dy[d];
break;
}
//다른 구슬일 경우
if (target.equals(other)) {
target.x -= dx[d];
target.y -= dy[d];
break;
}
//구멍일 경우
if (target.equals(O)) {
target.x = -1;
break;
}
}
}

static void solve() throws IOException {
Queue<Element> q = new ArrayDeque<>();
q.add(new Element(R, B, 0, ""));
while(!q.isEmpty()) {
Element cur = q.poll();
if (cur.t == 10) break;
for (int d = 0; d < 4; d++) {
Point NR = new Point(cur.R.x, cur.R.y);
Point NB = new Point(cur.B.x, cur.B.y);
String NRoot = nextRoot(cur.root, d);

if (isRedFirst(cur.R, cur.B, d)) {
//빨강 먼저
move(NR, NB, d);
move(NB, NR, d);
} else {
//파랑 먼저
move(NB, NR, d);
move(NR, NB, d);
}

//1. 파랑만 빠졌을 경우
if (NR.x != -1 && NB.x == -1)
continue;
//2. 둘 다 빠졋을 경우
if (NR.x == -1 && NB.x == -1)
continue;
//3. 빨강만 빠졌을 경우
if (NR.x == -1 && NB.x != -1) {
bw.write(cur.t + 1 + "\n");
bw.write(NRoot);
return;
}

q.add(new Element(NR, NB, cur.t + 1, NRoot));

}
}

//불가능
bw.write("-1\n");
}

static void input() throws IOException {
bw = new BufferedWriter(new OutputStreamWriter(System.out));
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];
for (int i = 0; i < N; i++) {
String in = br.readLine();
for (int j = 0; j < M; j++) {
map[i][j] = in.charAt(j);
if (map[i][j] == 'R') {
R = new Point(i, j);
map[i][j] = '.';
} else if (map[i][j] == 'B') {
B = new Point(i, j);
map[i][j] = '.';
} else if (map[i][j] == 'O') {
O = new Point(i, j);
map[i][j] = '.';
}
}
}
}

public static void main(String[] args) throws IOException {
input();
solve();
bw.flush();
}


}