Skip to content

0919 임웅균 3문제 #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
135 changes: 135 additions & 0 deletions 임웅균/Week26/BOJ_20058.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package Week26;

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

public class BOJ_20058 {
static int[][] map, visited;
static int N, Q, L, powL, totalIce;

static int[] dr = {-1, 0, 1, 0};
static int[] dc = {0, -1, 0, 1};
static Queue<int[]> meltingQ = new LinkedList<>();
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());
Q = Integer.parseInt(st.nextToken());
totalIce = 0;
map = new int[(int) Math.pow(2,N)][(int) Math.pow(2,N)];
for(int r = 0; r < map.length; r++){
st = new StringTokenizer(br.readLine());
for(int c = 0; c < map.length; c++){
map[r][c] = Integer.parseInt(st.nextToken());
totalIce += map[r][c];
}
}
// L받기
st = new StringTokenizer(br.readLine());
// Q번만큼 파이어스톰 돌리기
while(Q-- > 0){
L = Integer.parseInt(st.nextToken());
powL = (int) Math.pow(2,L);
fireStorm();
}
// 전체 덩어리의 합 출력
System.out.println(totalIce);
// 가장 큰 덩어리가 차지하는 칸의 개수 출력(bfs)
visited = new int[(int) Math.pow(2,N)][(int) Math.pow(2,N)];
int idx = 1;
int maxSize = 0;
for(int r = 0; r < map.length; r++){
for(int c = 0; c < map.length; c++){
if(map[r][c] != 0 && visited[r][c] == 0){
maxSize = Math.max(bfs(r,c,idx++), maxSize);
}
}
}
System.out.println(maxSize);
}

private static int bfs(int R, int C, int idx) {

int size = 1;
Queue<int[]> q = new LinkedList<>();
q.add(new int[]{R,C});
visited[R][C] = idx;
while(!q.isEmpty()){
int[] curr = q.poll();
for(int d = 0; d < 4; d++){
int nr = curr[0] + dr[d];
int nc = curr[1] + dc[d];
if(nr < 0 || nc < 0 || nr >= map.length || nc >= map.length){
continue;
}
if(visited[nr][nc] != 0 || map[nr][nc] == 0){
continue;
}
visited[nr][nc] = idx;
size++;
q.add(new int[]{nr,nc});
}
}
return size;
}


private static void fireStorm() {
for(int r = 0; r < map.length; r+=powL){
for(int c = 0; c < map.length; c+=powL){
// 배열돌리기
rotateArray(r,c);
}
}
// 전체 탐색하면서 음이 있는 칸 3개 또는 그 이상과 인접해있지 않은 칸은 얼음의 양 1 줄이기
for(int r = 0; r < map.length; r++){
for(int c = 0; c < map.length; c++){
// 4방향 탐색
int iceCnt = 0;
for(int d = 0; d < 4; d++){
int nr = r + dr[d];
int nc = c + dc[d];
if(nr < 0 || nc < 0 || nr >= map.length || nc >= map.length){
continue;
}
// 얼음이 있으면 iceCnt +1
iceCnt += map[nr][nc] > 0 ? 1 : 0;
}
if (map[r][c] > 0 && iceCnt < 3) { // 얼음이 있고, 줄어들어야 하는 조건을 추가
meltingQ.add(new int[]{r, c});
}

}
}
// 전체탐색을 마쳤으니 얼음 한번에 지워주기
while (!meltingQ.isEmpty()) {
int[] curr = meltingQ.poll();
if (map[curr[0]][curr[1]] > 0) { // 얼음이 이미 녹아서 0이 아닌 경우만 녹인다.
map[curr[0]][curr[1]]--;
totalIce--;
}
}
}

private static void rotateArray(int r, int c) {
int[][] rotated = new int[powL][powL];

// 회전된 배열 생성
for(int i = r; i < r + powL; i++) {
for(int j = c; j < c + powL; j++) {
rotated[j-c][r + powL - 1 - i] = map[i][j];
}
}

// 원래 배열에 회전된 배열 복사
for(int i = r; i < r + powL; i++) {
for(int j = c; j < c + powL; j++) {
map[i][j] = rotated[i-r][j-c];
}
}
}
}
128 changes: 128 additions & 0 deletions 임웅균/Week26/BOJ_2310.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package Week26;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class BOJ_2310 {

static int n;
static List<Room> rooms;
static boolean[][] visited;

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

while (true) {
n = Integer.parseInt(br.readLine());
if (n == 0) break;
// 금액으로 방문체크
visited = new boolean[n][501];
rooms = new ArrayList<>();

for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
char type = st.nextToken().charAt(0);
int price = Integer.parseInt(st.nextToken());
List<Integer> room = new LinkedList<>();
while (st.countTokens() > 1) {
room.add(Integer.parseInt(st.nextToken()) - 1);
}
rooms.add(new Room(type, price, room));
}
System.out.println(solve(0, 0) ? "Yes" : "No");
}
}

private static boolean solve(int startIdx, int startMoney) {
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{startIdx, startMoney});

while (!queue.isEmpty()) {
int[] current = queue.poll();
int idx = current[0];
int money = current[1];
Room room = rooms.get(idx);

if (idx == n - 1) {
if (room.type == 'T') {
money -= room.money;
if (money < 0) {
continue;
}
}
return true;
}

if (room.type == 'T') {
money -= room.money;
if (money < 0) {
continue;
}
} else if (room.type == 'L') {
if (money < room.money) {
money = room.money;
}
}

for (int nextIdx : room.connectedRooms) {
if (!visited[nextIdx][money]) {
visited[nextIdx][money] = true;
queue.offer(new int[]{nextIdx, money});
}
}
}

return false;
}

static class Room {
char type;
int money;
List<Integer> connectedRooms;

public Room(char type, int money, List<Integer> connectedRooms) {
this.type = type;
this.money = money;
this.connectedRooms = connectedRooms;
}
}

// private static boolean solve(int idx, int money) {
// Room room = rooms.get(idx);
// int tempMoney = money;
// if (idx == n - 1) {
// // 여기서도 계산 한번 해줘서 갈 수 있는지 판단해야한다.
// // 이거 안해줘서 1번이 계속 틀렸다.
// if (room.type == 'T') {
// tempMoney -= room.money;
// if (tempMoney < 0) {
// return false;
// }
// }
// return true;
// }
// if (room.type == 'T') {
// tempMoney -= room.money;
// if (tempMoney < 0) {
// return false;
// }
// } else if (room.type == 'L') {
// if (tempMoney < room.money) {
// tempMoney = room.money;
// }
// }
// // 다음에 갈곳 지정
// for (int nextIdx : room.connectedRooms) {
// if (visited[nextIdx][tempMoney] == false) { // 이전에 이 금액으로 방문하지 않았다면
// visited[nextIdx][tempMoney] = true; // 방문 체크
// if (solve(nextIdx, tempMoney)) { // 다음 방으로 이동
// return true;
// }
// }
// }
// return false;
// }
}
60 changes: 60 additions & 0 deletions 임웅균/Week26/BOJ_27497.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package Week26;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
import java.util.StringTokenizer;

public class BOJ_27497 {
static int N;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st;
Deque<Character> deque = new ArrayDeque<>();
Stack<Integer> stack = new Stack<>();
for(int n = 0; n < N; n++){
st = new StringTokenizer(br.readLine());
if(st.countTokens() == 1){
// 무조건 3임
if(!stack.empty()) {
if(stack.pop() == 1) {
// 맨 뒤 문자 제거
deque.pollLast();
}
else {
// 맨 앞 문자 제거
deque.pollFirst();
}
}
}
else{
// 문자열 맨 뒤에 c가 적힌 블록 추가
stack.push(Integer.parseInt(st.nextToken()));
char c = st.nextToken().charAt(0);
if(stack.peek() == 1){
deque.addLast(c);
}
else{
deque.addFirst(c);
}
}
}
System.out.println(printDeque(deque));
}

private static String printDeque(Deque<Character> deque) {
StringBuilder sb = new StringBuilder();
if(deque.size() == 0){
sb.append(0);
return sb.toString();
}
while(!deque.isEmpty()){
sb.append(deque.pollFirst());
}
return sb.toString();
}
}