diff --git "a/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.java" "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.java" new file mode 100644 index 00000000..8d1b66aa --- /dev/null +++ "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-10266] \354\213\234\352\263\204 \354\202\254\354\247\204\353\223\244.java" @@ -0,0 +1,84 @@ +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + //입력 값 + int n = Integer.parseInt(br.readLine()); + int[] a = new int[n]; + int[] b = new int[n]; + + StringTokenizer st = new StringTokenizer(br.readLine()); + for(int i=0; i0 && da[i] != da[j]) { + j = pi[j-1]; + } + if(da[i] == da[j]) { + pi[i] = ++j; + } + } + + //모든 점을 시작점으로 비교하기 위한 배열 + int[] target = new int[2*n]; + for(int i=0; i<2*n; i++){ + target[i] = db[i%n]; + } + + //kmp + j=0; + for(int i=0; i<2*n; i++) { + while(j>0 && target[i] != da[j]) { + j = pi[j-1]; + } + + if(target[i] == da[j]) { + //찾았다면 종료 + if(j == n-1) { + System.out.println("possible"); + return; + } + j++; + } + } + + System.out.println("impossible"); + } +} diff --git "a/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.java" "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.java" new file mode 100644 index 00000000..5ed7476d --- /dev/null +++ "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1507] \352\266\201\352\270\210\355\225\234 \353\257\274\355\230\270.java" @@ -0,0 +1,79 @@ +import java.io.*; +import java.util.*; + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int n = Integer.parseInt(br.readLine()); + //최소 거리 정보 + int[][] map = new int[n][n]; + //현재 도로 정보 + int[][] graph = new int[n][n]; + for(int i=0; i pq = new PriorityQueue<>((o1, o2) -> Integer.compare(dist[o1], dist[o2])); + //모든 도로를 하나씩 제거해보며 해당 도로가 최소한 있어야 하는 필수 도로인지 판별 + //i와 j를 잇는 도로를 제거했을 때 i -> j로 가는 최단 거리를 계산 + for(int i=0; i j로 가는 최단 거리가 다음과 같을 때 + //1. 제거 전보다 클 때 해당 도로는 필수 도로이다. + if(dist[j] > map[i][j]) { + result += map[i][j]; +// System.out.println((i+1)+"->"+(j+1)); + continue; + } + //2. 제거 전보다 작다면 문제가 잘못됨. + if(dist[j] < map[i][j]) { + System.out.println(-1); + return; + } + //3. 제거 전과 같다면 해당 도로는 필수 도로가 아니므로 도로 정보를 INF 값으로 설정하여 없는 도로로 설정 + graph[i][j] = (int) 1e9; + graph[j][i] = (int) 1e9; + } + } + + System.out.println(result); + } +} diff --git "a/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.java" "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.java" new file mode 100644 index 00000000..0ef23649 --- /dev/null +++ "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-1561] \353\206\200\354\235\264 \352\263\265\354\233\220.java" @@ -0,0 +1,92 @@ +import java.io.*; +import java.util.*; + +public class Main { + private static int n, m; + private static int[] cost; + + 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()); + + //cost[i]: i번 놀이기구 운행 시간 + cost = new int[m+1]; + long min = Integer.MAX_VALUE; //가장 작은 운행 시간 + for(int i=1; i<=m; i++) { + cost[i] = Integer.parseInt(st.nextToken()); + min = Math.min(min, cost[i]); + } + + //놀이기구 개수보다 사람이 적다면 바로 반환 + if(n <= m) { + System.out.println(n); + return; + } + + //이분 탐색(모든 사람이 탈 수 있는 최소 시간) + long left = 0; + long right = n*min;//모두 운행시간이 가장 작은 놀이기구를 탔을 때의 시간 + long minTime = right;//찾고자 하는 최소 시간 + + while(left <= right) { + long mid = left + (right-left)/2; + + long count = counting(mid); + if(count >= n) { + minTime = Math.min(minTime, mid); + right = mid-1; + continue; + } + left = mid+1; + } + + //모든 사람이 탈 수 있는 시간의 1분 전에 총 몇 명이 탑승할 수 있는지 + long count = 0; + long time = minTime-1; + //wait[i]: 다음 사람이 i번 놀이기구를 타기 위해 기다려야 하는 대기 시간 + long[] wait = new long[m+1]; + for(int i=1; i<=m; i++) { + //사람 수 계산 + if(time/cost[i] == 0) count += 1; + else if(time%cost[i] == 0) count += time/cost[i]; + else count += time/cost[i]+1; + //대기 시간 계산 + wait[i] = time%cost[i] == 0 ? 0 : cost[i] - time%cost[i]; + } + + //남은 사람 수 + long rest = n-count; + //대기 시간이 가장 짧은 순으로 놀이기구 번호 정렬 + PriorityQueue pq = new PriorityQueue<>((o1, o2) -> { + if(wait[o1] == wait[o2]) return Integer.compare(o1, o2); + return Long.compare(wait[o1], wait[o2]); + }); + for(int i=1; i<=m; i++) { + pq.add(i); + } + + //마지막 사람이 타는 놀이기구 번호 + long result = 0; + for(int i=0; i> graph = new ArrayList<>(); + for(int i=0; i<=n; i++) { + graph.add(new HashSet<>()); + } + for(int i=0; i> result = new ArrayList<>(); + for(int i=0; i<=n; i++) { + result.add(new HashSet<>()); + } + // 이 문제는 1부터 시작 + result.get(0).add(1); + //result 채우기 + boolean[] visited = new boolean[n+1]; + visited[1] = true; + for(int i=0,idx=1; i set = graph.get(order[i]); + for(int node : set) { + if(visited[node]) continue; + visited[node] = true; + result.get(idx).add(node); + } + } + + //result(순서 집합)에 맞게 나오는지 체크 + int idx = 0; + for(int i=0; i set = result.get(i); + if(set.size() == 0) break; + for(int j=0; j fishes; + private static int max; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + map = new Fish[n][n]; + fishes = new ArrayList<>(); + for(int i=0; i tempFishes = new ArrayList<>(); + for(int i=0; i=n || ny<0 || ny>=n) continue; + if(nx==shark[0] && ny==shark[1]) continue; + + fish.dir = dir; + Fish temp = map[nx][ny]; + map[nx][ny] = fish; + map[fish.x][fish.y] = temp; + temp.move(fish.x, fish.y); + fish.move(nx, ny); + break; + } + } + + // 상어 먹방 + boolean moved = false; + for(int idx=1; idx<=3; idx++) { + int nx = shark[0] + d[shark[2]][0] * idx; + int ny = shark[1] + d[shark[2]][1] * idx; + if(nx<0 || nx>=n || ny<0 || ny>=n) break; + + if(!map[nx][ny].isAlive()) continue; + + // 물고기를 잡아먹음 + int fishDir = map[nx][ny].die(); + moved = true; + + // 다음 단계로 진행 + dfs(new int[] {nx, ny, fishDir}, current + map[nx][ny].num); + + // 물고기 부활 + map[nx][ny].alive = true; + } + + // 상어가 더 이상 이동할 곳이 없을 때 + if(!moved) { + max = Math.max(max, current); + } + + // 맵 상태 복원 + map = tempMap; + fishes = tempFishes; + } + + private static class Fish implements Comparable{ + int x, y; + int num; + int dir; + boolean alive; + + public Fish(int x, int y, int num, int dir, boolean alive) { + this.x = x; + this.y = y; + this.num = num; + this.dir = dir; + this.alive = alive; + } + + public int die() { + this.alive = false; + return this.dir; + } + + public boolean isAlive() { + return this.alive; + } + + public void move(int x, int y) { + this.x = x; + this.y = y; + } + + @Override + public int compareTo(Fish o) { + return Integer.compare(this.num, o.num); + } + + @Override + public String toString() { + String t = ""; + switch(dir) { + case 0: t = "↑"; break; + case 1: t = "↖"; break; + case 2: t = "←"; break; + case 3: t = "↙"; break; + case 4: t = "↓"; break; + case 5: t = "↘"; break; + case 6: t = "→"; break; + case 7: t = "↗"; break; + } + return this.alive ? "["+this.num+","+t+"]" : "[ X ]"; + } + } +} diff --git "a/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.java" "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.java" new file mode 100644 index 00000000..db95252d --- /dev/null +++ "b/\354\234\240\353\263\221\352\267\234_13\354\243\274\354\260\250/[BOJ-23290] \353\247\210\353\262\225\354\202\254 \354\203\201\354\226\264\354\231\200 \353\263\265\354\240\234.java" @@ -0,0 +1,172 @@ +import java.io.*; +import java.util.*; + +public class Main { + private static int[][] fd = {{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1}}; + private static int[][] sd = {{-1,0},{0,-1},{1,0},{0,1}}; + private static int max; + private static int[][] map; + private static Node[] path; + + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int n = 4; + + map = new int[n][n]; + int[][] smallMap = new int[n][n]; + + StringTokenizer st = new StringTokenizer(br.readLine()); + int m = Integer.parseInt(st.nextToken()); + int s = Integer.parseInt(st.nextToken()); + + Set fishes = new HashSet<>(); + for(int i=0; i 0) smallMap[i][j]++; + } + } + + //기존 물고기 저장 + Queue q = new LinkedList<>(); + + //물고기 이동 + for(Fish fish : fishes) { + q.add(new Fish(fish.x, fish.y, fish.dir)); + + for(int i=0; i=n || ny<0 || ny>=4 || smallMap[nx][ny] > 0) continue; + if(nx==shark[0] && ny==shark[1]) continue; + + map[fish.x][fish.y]--; + map[nx][ny]++; + fish.x = nx; + fish.y = ny; + fish.dir = idx; + break; + } + + } + + //상어 이동 + max = -1; + path = new Node[3]; + dfs(0, shark[0], shark[1], 0, new Node(shark[0], shark[1], null)); + List remove = new ArrayList<>(); + for(int i=0; i<3; i++) { + Node node = path[i]; + if(map[node.x][node.y] == 0) continue; + map[node.x][node.y] = 0; + for(Fish fish : fishes) { + if(fish.x == node.x && fish.y == node.y) remove.add(fish); + } + smallMap[node.x][node.y] = 1; + } + fishes.removeAll(remove); + shark[0] = path[2].x; + shark[1] = path[2].y; + + //물고기 냄새 제거 + for(int i=0; i 2) smallMap[i][j] = 0; + } + } + + //물고기 복사 + while(!q.isEmpty()) { + Fish fish = q.poll(); + fishes.add(fish); + map[fish.x][fish.y]++; + } + + time++; + } + + int count = 0; + for(int i=0; i=0; i--) { + path[i] = current; + current = current.pre; + } + return; + } + + for(int i=0; i=4 || ny<0 || ny>=4) continue; + int nCount = count+map[nx][ny]; + Node temp = node; + while(temp.pre != null) { + if(temp.x == nx && temp.y == ny) { + nCount = count; + break; + } + temp = temp.pre; + } + dfs(dept+1, nx, ny, nCount, new Node(nx, ny, node)); + } + } + + private static class Fish{ + int x; + int y; + int dir; + + public Fish(int x, int y, int dir) { + this.x = x; + this.y = y; + this.dir = dir; + } + } + + private static class Node{ + int x; + int y; + Node pre; + + public Node(int x, int y, Node pre) { + this.x = x; + this.y = y; + this.pre = pre; + } + } +}