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
103 changes: 103 additions & 0 deletions baekjoon/11967_불켜기/src/Harin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package BOJ;

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

public class BJ11967_불켜기 {

private static int n;
private static Map<Integer, ArrayList<int[]>> roomMap;
private static int[] drow = new int[]{-1, 1, 0, 0};
private static int[] dcol = new int[]{0, 0, -1, 1};
private static boolean[][] light;
private static boolean[][] visit;
private static int lightCnt = 1;

private static class Room{
int r, c;

public Room(int r, int c) {
this.r = r;
this.c = c;
}
}

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());
int m = Integer.parseInt(st.nextToken());

roomMap = new HashMap<>();

for(int i=0; i<m; i++){
st = new StringTokenizer(br.readLine());
int r = Integer.parseInt(st.nextToken()); //방 위치
int c = Integer.parseInt(st.nextToken());
int sr = Integer.parseInt(st.nextToken()); //스위치 위치
int sc = Integer.parseInt(st.nextToken());

int roomNum = n*(r-1) + c;

try{
roomMap.get(roomNum).add(new int[]{sr, sc});
} catch(Exception e){
roomMap.put(roomNum, new ArrayList<>());
roomMap.get(roomNum).add(new int[]{sr, sc});
}
}

light = new boolean[n+1][n+1];
visit = new boolean[n+1][n+1];
light[1][1] = true;
int lightBefore = 0;

do{
lightBefore = lightCnt;
lightCnt = bfs();
} while(lightBefore != lightCnt);

System.out.println(lightCnt);
}

private static int bfs(){

for(boolean[] arr : visit){Arrays.fill(arr, false);}
visit[1][1] = true;

int lightUpdate = lightCnt;

Queue<Room> q = new ArrayDeque<>();
q.add(new Room(1, 1));

while(!q.isEmpty()){
Room cur = q.poll();
int roomNum = n*(cur.r-1) + cur.c;

//현재 도착한 방에서 불켜기
if(roomMap.containsKey(roomNum)){
for(int[] arr : roomMap.get(roomNum)){
int r = arr[0], c = arr[1];
if(!light[r][c]){ //불켜기
light[r][c] = true;
lightUpdate++;
}
}
}

for(int d=0; d<4; d++){
int nr = cur.r + drow[d];
int nc = cur.c + dcol[d];

try{
//방문하지 않았고 불이 켜져있으면 갈 수 있음
if(!visit[nr][nc] && light[nr][nc]){
visit[nr][nc] = true;
q.add(new Room(nr, nc));
}
} catch(Exception e){ continue; }
}
}
return lightUpdate;
}
}
119 changes: 119 additions & 0 deletions baekjoon/8980_택배/src/Harin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package BOJ;

import java.io.*;
import java.util.*;
public class BJ8980_택배 {

private static class Delivery{
int from, to, post;

public Delivery(int from, int to, int post) {
this.from = from;
this.to = to;
this.post = post;
}

@Override
public String toString() {
return "Delivery{" +
"from=" + from +
", to=" + to +
", post=" + post +
'}';
}
}

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

int n = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());

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

ArrayList<Delivery> deliveries = new ArrayList<>();

for(int i=0; i<m; i++){
st = new StringTokenizer(br.readLine());
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
int post = Integer.parseInt(st.nextToken());

deliveries.add(new Delivery(from, to, post));
}

Collections.sort(deliveries, (o1, o2) -> o1.from - o2.from);

ArrayList<Delivery> truck = new ArrayList<>();
int curTruckContain = 0;
int delivered = 0;

for(int town=1; town<=n; town++){

//1. 배송 할거 먼저 내리고
if(!truck.isEmpty()){
ArrayList<Delivery> toDelete = new ArrayList<>();

//마을에 택배 배송 완료
for(Delivery del : truck){
if(del.to == town){
delivered += del.post;
curTruckContain -= del.post;
toDelete.add(del);
}
}
//배송 완료된건 리스트에서 삭제
for(Delivery del : toDelete){
truck.remove(del);
}
}

//2. 새로 담기
for(Delivery del : deliveries){
if(del.from == town){
//용량이 부족할 경우
if(curTruckContain + del.post > c){
//현재 트럭에 있는 도착지보다 새로 담을 택배 도착지가 먼저인 경우
while(!truck.isEmpty() && truck.get(truck.size()-1).to > del.to) {
//기존에 담았던 짐을 버리고 가까운 도착지 택배를 담는다
//일부만 버려도 되는경우
if(truck.get(truck.size()-1).post > del.post){
truck.get(truck.size()-1).post -= del.post;
curTruckContain -= del.post;
break;
}
//다 버려야 하는 경우
else{
curTruckContain -= truck.get(truck.size()-1).post;
truck.remove(truck.get(truck.size()-1));
}
}

//비웠는데도 용량 부족하면 되는 만큼만 담는다
if(curTruckContain + del.post > c){
del.post = c - curTruckContain;
curTruckContain = c;
truck.add(del);
}

//허용 범위면 그대로 추가
else{
curTruckContain += del.post;
truck.add(del);
}
}

//용량이 부족하지 않은 경우 그냥 추가
else{
curTruckContain += del.post;
truck.add(del);
}


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