Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19a4d3f

Browse files
Merge pull request #194 from icegosimperson/main
[14주차] 이혜원
2 parents 6b5b140 + e09447b commit 19a4d3f

File tree

8 files changed

+498
-0
lines changed

8 files changed

+498
-0
lines changed

‎BOJ/20001-25000번/HW_20181.java‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class HW_20181 {
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
11+
int N = Integer.parseInt(st.nextToken()); // 먹이 개수
12+
int K = Integer.parseInt(st.nextToken()); // 최소 만족도
13+
int[] arr = new int[N];
14+
long[] dp = new long[N+1]; // dp[i] : i번쨰 먹이까지의 최대 탈피 에너지
15+
16+
st = new StringTokenizer(br.readLine());
17+
for(int i=0; i<N; i++){
18+
arr[i] = Integer.parseInt(st.nextToken());
19+
}
20+
21+
long sum = 0; // 만족도 합
22+
int left = 0;
23+
24+
for(int right=1; right <=N; right++){
25+
sum += arr[right-1];
26+
27+
while(K <= sum){
28+
dp[right] = Math.max(dp[right], dp[left] + sum - K); // 탈피 에너지 계산
29+
sum -= arr[left];
30+
left++; // 구간 축소
31+
}
32+
dp[right] = Math.max(dp[right], dp[right - 1]); // 최대 탈피 에너지값 출력
33+
}
34+
System.out.println(dp[N]);
35+
}
36+
}

‎BOJ/20001-25000번/HW_20291.java‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class HW_20291 {
5+
public static void main(String[] args) throws IOException {
6+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
int N = Integer.parseInt(br.readLine());
8+
9+
TreeMap<String, Integer> words = new TreeMap<>();
10+
11+
for(int i=0; i<N; i++) {
12+
String input = br.readLine();
13+
String extension = input.substring(input.lastIndexOf('.')+1);
14+
15+
words.put(extension, words.getOrDefault(extension, 0)+1);
16+
}
17+
18+
for(Map.Entry<String, Integer> entry : words.entrySet()) {
19+
System.out.println(entry.getKey() + " " + entry.getValue());
20+
}
21+
22+
}
23+
}

‎BOJ/20001-25000번/HW_22944.java‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class HW_22944 {
5+
static int N, H, D;
6+
static char[][] board;
7+
static int[][] check;
8+
static int[] dx = {-1, 1, 0, 0};
9+
static int[] dy = {0, 0, -1, 1};
10+
11+
static class Node {
12+
int x, y, h, d, m;
13+
14+
public Node(int x, int y, int h, int d, int m) {
15+
this.x = x;
16+
this.y = y;
17+
this.h = h; // 현재 체력
18+
this.d = d; // 우산 내구도
19+
this.m = m; // 이동 횟수
20+
}
21+
}
22+
23+
public static void main(String[] args) throws IOException {
24+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
25+
StringTokenizer st = new StringTokenizer(br.readLine());
26+
27+
N = Integer.parseInt(st.nextToken());
28+
H = Integer.parseInt(st.nextToken());
29+
D = Integer.parseInt(st.nextToken());
30+
31+
board = new char[N][N];
32+
check = new int[N][N];
33+
34+
int startX = 0, startY = 0;
35+
for (int i = 0; i < N; i++) {
36+
String line = br.readLine();
37+
for (int j = 0; j < N; j++) {
38+
board[i][j] = line.charAt(j);
39+
if (board[i][j] == 'S') {
40+
startX = i;
41+
startY = j;
42+
}
43+
}
44+
}
45+
46+
for (int i = 0; i < N; i++) {
47+
Arrays.fill(check[i], -1);
48+
}
49+
50+
int result = bfs(startX, startY);
51+
System.out.println(result);
52+
}
53+
54+
public static int bfs(int sx, int sy) {
55+
Queue<Node> queue = new LinkedList<>();
56+
queue.add(new Node(sx, sy, H, 0, 0)); // 시작점 추가
57+
check[sx][sy] = H; // 시작점 방문 처리
58+
59+
while (!queue.isEmpty()) {
60+
Node cur = queue.poll();
61+
62+
for (int i = 0; i < 4; i++) {
63+
int newH = cur.h, newD = cur.d, newM = cur.m;
64+
int nx = cur.x + dx[i];
65+
int ny = cur.y + dy[i];
66+
67+
68+
if (!isValid(nx, ny))
69+
continue;
70+
71+
if (board[nx][ny] == 'E') { // 안전지대에 도달하면 이동 횟수 반환
72+
return newM + 1;
73+
}
74+
75+
if (board[nx][ny] == 'U')
76+
newD = D;
77+
78+
if (newD > 0) {
79+
newD--;
80+
} else {
81+
newH--;
82+
}
83+
84+
if (newH == 0)
85+
continue;
86+
87+
if (check[nx][ny] < newH + newD) {
88+
check[nx][ny] = newH + newD;
89+
queue.add(new Node(nx, ny, newH, newD, newM + 1));
90+
}
91+
}
92+
}
93+
return -1; // 안전지대에 도달하지 못한 경우
94+
}
95+
96+
public static boolean isValid(int nx, int ny) {
97+
return 0<= nx && nx < N && 0 <=ny && ny < N;
98+
}
99+
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class HW_색깔_폭탄 {
5+
static int n, m;
6+
static int[][] board;
7+
static int score = 0;
8+
static int[] dx = {-1,1,0,0};
9+
static int[] dy = {0,0,-1,1};
10+
11+
static class Bomb {
12+
int size;
13+
int redCount;
14+
int stdX;
15+
int stdY;
16+
List<int[]> blocks; // 폭탄 묶음 내 좌표
17+
18+
Bomb(int size, int redCount, int stdX, int stdY, List<int[]> blocks) {
19+
this.size = size;
20+
this.redCount = redCount;
21+
this.stdX = stdX;
22+
this.stdY = stdY;
23+
this.blocks = blocks;
24+
}
25+
}
26+
27+
public static void main(String[] args) throws IOException {
28+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
29+
StringTokenizer st = new StringTokenizer(br.readLine());
30+
31+
n = Integer.parseInt(st.nextToken());
32+
m = Integer.parseInt(st.nextToken());
33+
board = new int[n][n];
34+
35+
for (int i=0; i<n; i++){
36+
st = new StringTokenizer(br.readLine());
37+
for (int j=0; j<n; j++){
38+
board[i][j] = Integer.parseInt(st.nextToken());
39+
}
40+
}
41+
42+
while (true) {
43+
Bomb group = find();
44+
if (group == null || group.size < 2) {
45+
break;
46+
}
47+
score += group.size * group.size;
48+
remove(group);
49+
gravity();
50+
board = rotate(board);
51+
gravity();
52+
}
53+
54+
System.out.println(score);
55+
}
56+
57+
static Bomb find() {
58+
List<Bomb> candidates = new ArrayList<>();
59+
60+
for (int i=0; i<n; i++){
61+
for (int j=0; j<n; j++){
62+
int color = board[i][j];
63+
if (0 < color && color <= m) {
64+
Bomb bg = bfs(i, j, color);
65+
if (bg != null && 2 <=bg.size) {
66+
candidates.add(bg);
67+
}
68+
}
69+
}
70+
}
71+
if (candidates.isEmpty()){
72+
return null;
73+
}
74+
// 우선순위 정렬
75+
candidates.sort((a,b)-> {
76+
if (a.size != b.size) // 1. size 내림차순
77+
return b.size - a.size;
78+
if (a.redCount != b.redCount) // 2. redCount 오름차순
79+
return a.redCount - b.redCount;
80+
if (a.stdX != b.stdX) // 3. 기준 행 내림차순
81+
return b.stdX - a.stdX;
82+
return a.stdY - b.stdY; // 4. 기준 열 오름차순
83+
});
84+
85+
return candidates.get(0);
86+
}
87+
88+
static Bomb bfs(int sx, int sy, int color) {
89+
boolean[][] visited = new boolean[n][n];
90+
Queue<int[]> q = new LinkedList<>();
91+
q.offer(new int[]{sx, sy});
92+
visited[sx][sy] = true;
93+
94+
List<int[]> group = new ArrayList<>();
95+
group.add(new int[]{sx, sy});
96+
int redCount = (board[sx][sy] == 0) ? 1 : 0;
97+
98+
while(!q.isEmpty()){
99+
int[] cur = q.poll();
100+
int x=cur[0], y=cur[1];
101+
102+
for (int k=0; k<4; k++){
103+
int nx = x + dx[k];
104+
int ny = y + dy[k];
105+
if (!isValid(nx, ny)) {
106+
continue;
107+
}
108+
if (board[nx][ny] == -1) { // 검은 돌 제외
109+
continue;
110+
}
111+
if (!visited[nx][ny]) {
112+
if (board[nx][ny] == color || board[nx][ny] == 0) {
113+
visited[nx][ny] = true;
114+
q.offer(new int[]{nx,ny});
115+
group.add(new int[]{nx,ny});
116+
if (board[nx][ny] == 0) {
117+
redCount++;
118+
}
119+
}
120+
}
121+
}
122+
}
123+
124+
if (group.size()<2) return null;
125+
126+
127+
int targetX=-1;
128+
int targetY=-1;
129+
for (int i=0; i<group.size(); i++) {
130+
int gx = group.get(i)[0];
131+
int gy = group.get(i)[1];
132+
if (board[gx][gy]!=0) { // 기준점 찾기: 빨간 아닌 폭탄 중 x 최대, x 같다면 y 최소
133+
if (gx > targetX || (gx==targetX && (targetY==-1 || gy < targetY))) {
134+
targetX = gx;
135+
targetY = gy;
136+
}
137+
}
138+
}
139+
return new Bomb(group.size(), redCount, targetX, targetY, group);
140+
}
141+
142+
static void remove(Bomb b) {
143+
for (int i=0; i<b.blocks.size(); i++) {
144+
int x=b.blocks.get(i)[0];
145+
int y=b.blocks.get(i)[1];
146+
board[x][y] = -2; // 빈칸 처리
147+
}
148+
}
149+
150+
static void gravity() {
151+
for (int y=0; y<n; y++){
152+
for (int x=n-1; x>=0; x--){
153+
if (board[x][y] > -1) {
154+
int nx = x;
155+
while(true) {
156+
int down = nx+1;
157+
if (down>=n) break;
158+
if (board[down][y]!=-2) break;
159+
board[down][y]=board[nx][y];
160+
board[nx][y]=-2;
161+
nx=down;
162+
}
163+
}
164+
}
165+
}
166+
}
167+
static int[][] rotate(int[][] arr) {
168+
int[][] newBoard = new int[n][n];
169+
for (int x=0; x<n; x++){
170+
for (int y=0; y<n; y++){
171+
newBoard[n-1-y][x]=arr[x][y];
172+
}
173+
}
174+
return newBoard;
175+
}
176+
static boolean isValid(int nx, int ny){
177+
return 0 <= nx && nx < n && 0 <= ny && ny < n;
178+
}
179+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /