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 cd474fc

Browse files
authored
Merge pull request #195 from yeahdy/main
[14์ฃผ์ฐจ] ์ด์˜ˆ์ง„
2 parents 6748c47 + 6ae73f7 commit cd474fc

File tree

8 files changed

+597
-0
lines changed

8 files changed

+597
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
//ํˆฌํฌ์ธํ„ฐ+dp : 1<=N<=100,000 > O(N)
5+
public class YJ_20181 {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
String[] data = br.readLine().split("\\s");
9+
int n = Integer.parseInt(data[0]);
10+
int k = Integer.parseInt(data[1]);
11+
StringTokenizer st = new StringTokenizer(br.readLine());
12+
int[] branch = new int[n+1];
13+
for(int i=0; i<n; i++){
14+
branch[i] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
int start = 1;
18+
int end = 0;
19+
int satisfaction =branch[0];
20+
long[] dp = new long[n+1]; // dp[ํ˜„์žฌ์ง€์ ] = ์ตœ๋Œ€ ๋ˆ„์  ํƒˆํ”ผ ์—๋„ˆ์ง€
21+
22+
//k ๋ณด๋‹ค ์ž‘์œผ๋ฉด start๋ฅผ ์›€์ง์ด๊ณ , k ๋ฅผ ๋„˜์œผ๋ฉด end ๋ฅผ ์›€์ง์ด๊ธฐ ๋•Œ๋ฌธ์— start ์™€ end ๋ฒ”์œ„๊ฐ€ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํ˜•ํƒœ๋กœ ์ด๋™๋จ
23+
while(start < n+1){
24+
if(satisfaction < k){
25+
dp[start] = Math.max(dp[start],dp[start-1]); //ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ๋Œ€ ์—๋„ˆ์ง€ ๊ฐฑ์‹ 
26+
if(start < n+1){
27+
satisfaction += branch[start++]; //k ๋ณด๋‹ค ์ž‘์œผ๋ฉด start ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™
28+
}
29+
}else{
30+
while(satisfaction >= k){
31+
//๊ธฐ์กด ๋ˆ„์  ์ตœ๋Œ€์—๋„ˆ์ง€ vs ์ด์ „ ์ƒํƒœ๋กœ ๋Œ์•„๊ฐ€์„œ ํ˜„์žฌ ๊ตฌ๊ฐ„์— ๋Œ€ํ•œ ์—๋„ˆ์ง€
32+
//start ๊ฐ€ 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— end ๊ฐ€ ํˆฌํฌ์ธํ„ฐ ๋ฒ”์œ„์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์ด์ „ ์ƒํƒœ๊ฐ€ ๋จ
33+
dp[start] = Math.max(dp[start], dp[end] + satisfaction - k);
34+
satisfaction -= branch[end++]; //end ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™ ์‹œ์ผœ์„œ ๋ฒ”์œ„ ์ขํžˆ๊ธฐ
35+
}
36+
}
37+
}
38+
System.out.println(dp[n]);
39+
}
40+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.util.Map.Entry;
4+
5+
public class YJ_20291 {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int n = Integer.parseInt(br.readLine());
9+
TreeMap<String,Integer> fileMap = new TreeMap<>();
10+
for(int i=0; i<n; i++){
11+
String extension = br.readLine().split("\\.")[1];
12+
fileMap.put(extension,fileMap.getOrDefault(extension, 0)+1);
13+
}
14+
15+
for(Entry<String,Integer> entry: fileMap.entrySet()){
16+
System.out.printf("%s %d%n",entry.getKey(),entry.getValue());
17+
}
18+
}
19+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import java.util.*;
2+
import java.io.*;
3+
4+
//BFS
5+
public class YJ_22944 {
6+
static class Pos {
7+
int x;
8+
int y;
9+
int hp;
10+
int distance;
11+
int umbrella;
12+
int umbrellaCount;
13+
14+
public Pos(int x, int y, int hp, int distance, int umbrella, int umbrellaCount) {
15+
this.x = x;
16+
this.y = y;
17+
this.hp = hp;
18+
this.distance = distance;
19+
this.umbrella = umbrella;
20+
this.umbrellaCount = umbrellaCount;
21+
}
22+
}
23+
24+
static final char S = 'S';
25+
static final char U = 'U';
26+
static final char E = 'E';
27+
28+
static int[] current;
29+
static char[][] space;
30+
31+
public static void main(String[] args) throws IOException {
32+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
33+
StringTokenizer st = new StringTokenizer(br.readLine());
34+
int n = Integer.parseInt(st.nextToken());
35+
int h = Integer.parseInt(st.nextToken());
36+
int d = Integer.parseInt(st.nextToken());
37+
space = new char[n][n];
38+
39+
for(int i=0; i<n; i++){
40+
String data = br.readLine();
41+
for(int j=0; j<n; j++){
42+
if(S == data.charAt(j)){
43+
current = new int[] {i,j};
44+
}
45+
space[i][j] = data.charAt(j);
46+
}
47+
}
48+
visited = new boolean[n][n][11];
49+
50+
System.out.println(bfs(h,d));
51+
}
52+
53+
static boolean[][][] visited;
54+
static int[] nx = {1,0,-1,0};
55+
static int[] ny = {0,1,0,-1};
56+
static int bfs(int h, int d) {
57+
Deque<Pos> deque = new ArrayDeque<>();
58+
int uc = 0;
59+
deque.offer(new Pos(current[0],current[1],h,0,0,0));
60+
visited[current[0]][current[1]][0] = true;
61+
62+
while(!deque.isEmpty()){
63+
Pos pos = deque.poll();
64+
65+
for(int i=0; i<4; i++){
66+
int x = pos.x + nx[i];
67+
int y = pos.y + ny[i];
68+
if(stop(x,y,pos.umbrellaCount)){
69+
continue;
70+
}
71+
72+
int hp = pos.hp;
73+
int umbrella = pos.umbrella;
74+
char current = space[x][y];
75+
76+
if(current == E){
77+
return ++pos.distance;
78+
}
79+
//์šฐ์‚ฐํš๋“ ์‹œ ๋‚ด๊ตฌ๋„ ๊ฐ์†Œ+์šฐ์‚ฐ๊ฐฏ์ˆ˜ ์นด์šดํŒ…, ์•„๋‹ˆ๋ฉด ์ฃฝ์Œ๋น„ ๋งž๊ธฐ
80+
if(current == U){
81+
umbrella = d-1;
82+
pos.umbrellaCount = ++uc;
83+
space[x][y] = '.';
84+
}else{
85+
if(umbrella > 0){
86+
umbrella--;
87+
}else{
88+
hp--;
89+
}
90+
}
91+
92+
if(hp == 0){
93+
continue;
94+
}
95+
visited[x][y][pos.umbrellaCount] = true;
96+
deque.offer(new Pos(x,y,hp,pos.distance+1,umbrella,pos.umbrellaCount));
97+
}
98+
}
99+
100+
return -1;
101+
}
102+
103+
private static boolean stop(int x, int y, int count){
104+
return x < 0 || y < 0 || x >= space.length || y >= space.length || visited[x][y][count];
105+
}
106+
}
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
import java.util.ArrayDeque;
5+
import java.util.ArrayList;
6+
import java.util.Collections;
7+
import java.util.Deque;
8+
import java.util.List;
9+
import java.util.StringTokenizer;
10+
11+
public class YJ_์ƒ‰๊น”_ํญํƒ„ {
12+
static class Bundle implements Comparable<Bundle>{
13+
List<int[]> bombs;
14+
int red;
15+
int rows;
16+
int columns;
17+
18+
Bundle(List<int[]> bombs, int red, int rows, int columns){
19+
this.bombs = bombs;
20+
this.red = red;
21+
this.rows = rows;
22+
this.columns = columns;
23+
}
24+
25+
@Override
26+
public int compareTo(Bundle b){
27+
if(this.bombs.size() == b.bombs.size()){
28+
if(this.red == b.red){
29+
if(this.rows == b.rows){
30+
//3. ๊ฐ€์žฅ ์—ด์ด ์ž‘์€ ํญํƒ„ ๋ฌถ์Œ
31+
return this.columns - b.columns;
32+
}
33+
//2. ํ–‰์ด ๊ฐ€์žฅ ํฐ ํญํƒ„ ๋ฌถ์Œ (ํ–‰์ด ๋นจ๊ฐ„์ƒ‰์ด ์•„๋‹ˆ๋ฉด์„œ ๊ฐ€์žฅ ํฐ ์นธ)
34+
return b.rows - this.rows;
35+
}
36+
//1. ๋นจ๊ฐ„์ƒ‰ ํญํƒ„์ด ๊ฐ€์žฅ ์ ์€ ๋ฌถ์Œ
37+
return this.red - b.red;
38+
}
39+
//์‚ฌ์ด์ฆˆ๊ฐ€ ๊ฐ€์žฅ ํฐ ์ˆœ์„œ
40+
return b.bombs.size() - this.bombs.size();
41+
}
42+
}
43+
44+
static int n;
45+
static int m;
46+
static int[][] bombs;
47+
static List<Bundle> bundles = new ArrayList<>(); //๋ฌถ์Œ ํญํƒ„ ์ €์žฅ์†Œ
48+
static final int BLACK = -1;
49+
static final int RED = 0;
50+
static final int NULL = -2;
51+
52+
public static void main(String[] args) throws IOException {
53+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
54+
String[] data = br.readLine().split("\\s");
55+
n = Integer.parseInt(data[0]);
56+
m = Integer.parseInt(data[1]);
57+
bombs = new int[n][n];
58+
59+
for(int i=0; i<n; i++){
60+
StringTokenizer st = new StringTokenizer(br.readLine());
61+
for(int j=0; j<n; j++){
62+
bombs[i][j] = Integer.parseInt(st.nextToken());
63+
}
64+
}
65+
66+
int score = 0;
67+
do{
68+
find();
69+
if(bundles.isEmpty()){
70+
break;
71+
}
72+
73+
Bundle bombBundle = bundles.get(0);
74+
if(bundles.size() > 1){
75+
bombBundle = filter();
76+
}
77+
78+
score += remove(bombBundle);
79+
move();
80+
81+
bundles.clear();
82+
}while(true);
83+
84+
System.out.println(score);
85+
}
86+
87+
//๊ฐ€์žฅ ํฐ ํญํƒ„ ๋ฌถ์Œ ์ฐพ๊ธฐ (์—ฐ๊ฒฐ๋˜์–ด์žˆ์–ด์•ผํ•จ)
88+
static void find(){
89+
for(int i=0; i<n; i++){
90+
for(int j=0; j<n; j++){
91+
if(!stop(i,j)){
92+
bfs(i,j);
93+
}
94+
}
95+
}
96+
}
97+
98+
static int[] nx = {1,0,-1,0};
99+
static int[] ny = {0,1,0,-1};
100+
static boolean[][] visited;
101+
private static void bfs(int x, int y){
102+
Deque<int[]> deque = new ArrayDeque<>();
103+
visited = new boolean[n][n];
104+
105+
deque.offer(new int[]{x,y});
106+
visited[x][y] = true;
107+
int color = bombs[x][y];
108+
109+
//ํญํƒ„ ๋ฌถ์Œ ์ดˆ๊ธฐํ™”
110+
List<int[]> bombList = new ArrayList<>();
111+
bombList.add(new int[]{x,y});
112+
int red = color == RED? 1 : 0;
113+
int rows = x;
114+
int columns = y;
115+
116+
while(!deque.isEmpty()) {
117+
int[] current = deque.poll();
118+
for (int d = 0; d < 4; d++) {
119+
int dx = current[0] + nx[d];
120+
int dy = current[1] + ny[d];
121+
122+
if (stop(dx, dy) || visited[dx][dy]) {
123+
continue;
124+
}
125+
126+
int bomb = bombs[dx][dy];
127+
//๋นจ๊ฐ„์ƒ‰ ํญํƒ„ ๋˜๋Š” ํ•œ๊ฐ€์ง€์ƒ‰ ๋งŒ ์˜ฌ ์ˆ˜ ์žˆ์Œ
128+
if(bomb == color || bomb == RED){
129+
if(bomb == RED){
130+
red++;
131+
}
132+
//๋นจ๊ฐ„์ƒ‰์ด ์•„๋‹ˆ๋ฉด์„œ ๊ฐ€์žฅ ํ–‰์ด ํฐ ํญํƒ„ ๋ฌถ์Œ
133+
if(dx > rows && bomb != RED){
134+
rows = dx;
135+
}
136+
//๊ฐ€์žฅ ์ž‘์€ ์—ด
137+
if(dy < columns){
138+
columns = dy;
139+
}
140+
141+
deque.offer(new int[]{dx,dy});
142+
bombList.add(new int[]{dx,dy});
143+
visited[dx][dy] = true;
144+
}
145+
}
146+
}
147+
148+
//ํญํƒ„ ๋ฌถ์Œ์ด๋ž€ 2๊ฐœ ์ด์ƒ์˜ ํญํƒ„์œผ๋กœ ๊ตฌ์„ฑ
149+
if(bombList.size() > 1){
150+
bundles.add(new Bundle(bombList,red,rows,columns));
151+
}
152+
}
153+
154+
private static boolean stop(int x, int y){
155+
return x < 0 || y < 0 || x >= n || y >= n || bombs[x][y] == BLACK || bombs[x][y] == NULL;
156+
}
157+
158+
//๋™์ผํ•œ ํญํƒ„๋ฌถ์Œ ํ•„ํ„ฐ๋ง
159+
static Bundle filter(){
160+
Collections.sort(bundles);
161+
return bundles.get(0);
162+
}
163+
164+
//ํญํƒ„ ๋ฌถ์Œ ์ œ๊ฑฐ > ํญํƒ„ ๊ฐฏ์ˆ˜ ์ ์ˆ˜ ๊ตฌํ•˜๊ธฐ(C*C)
165+
static int remove (Bundle bombBundle){
166+
//์ œ๊ฑฐํ•˜๋ ค๋ฉด ์œ„์น˜๋ฅผ ์•Œ์•„์•ผํ•จ
167+
List<int[]> bombList = bombBundle.bombs;
168+
for(int[] pos : bombList){
169+
bombs[pos[0]][pos[1]] = NULL;
170+
}
171+
172+
int count = bombList.size();
173+
return count*count;
174+
}
175+
176+
//ํญํƒ„๋“ค ์›€์ง์ด๊ธฐ (์ค‘๋ ฅ + ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ 90 ๋„ ํšŒ์ „)
177+
static void move(){
178+
moveDown();
179+
rotate();
180+
moveDown();
181+
}
182+
183+
//์ค‘๋ ฅ ์ž‘์šฉ
184+
private static void moveDown(){
185+
for(int i=n-2; i>=0; i--){
186+
for(int j=0; j<n; j++){
187+
if(bombs[i][j] == BLACK || bombs[i][j] == NULL){
188+
continue;
189+
}
190+
//๊ฐ€์žฅ ๋ ๋นˆ๊ณต๊ฐ„๊นŒ์ง€ ์ธ๋ฑ์Šค๋ฅผ ์ด๋™
191+
int index = i+1;
192+
while(index < n && bombs[index][j] == NULL){
193+
index++;
194+
}
195+
//ํ•ด๋‹น ์ธ๋ฑ์Šค ์œ„์น˜๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ while ๋‚ด์—์„œ +1 ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— i+2 ๊ฐ€ ๋œ ์ƒํƒœ
196+
if(index-1 != i){
197+
bombs[index-1][j] = bombs[i][j];
198+
bombs[i][j] = NULL;
199+
}
200+
}
201+
}
202+
}
203+
204+
//๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ 90๋„ ํšŒ์ „
205+
private static void rotate(){
206+
int[][] temp = new int[n][n];
207+
for(int i=0; i<n; i++){
208+
for(int j=0; j<n; j++){
209+
temp[i][j] = bombs[j][n-1-i];
210+
}
211+
}
212+
bombs = temp;
213+
}
214+
}

0 commit comments

Comments
(0)

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /