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 bc13faa

Browse files
Merge pull request #197 from yeongleej/main
[14์ฃผ์ฐจ] ์ด์ง€์˜
2 parents 3a9bedc + 68b720b commit bc13faa

File tree

8 files changed

+628
-0
lines changed

8 files changed

+628
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class JY_20181 {
5+
6+
public static void main(String[] args) throws IOException{
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+
int N = Integer.parseInt(st.nextToken());
11+
int K = Integer.parseInt(st.nextToken());
12+
13+
long[] arr = new long[N+1];
14+
st = new StringTokenizer(br.readLine());
15+
for(int i=1; i<N+1; i++) {
16+
arr[i] = Long.parseLong(st.nextToken());
17+
}
18+
19+
long[] dp = new long[N+1];
20+
int s = 1;
21+
int e = 1;
22+
int sum = 0;
23+
24+
while(e <= N) {
25+
sum += arr[e];
26+
dp[e] = dp[e-1]; // ์ด์ „ ๊ฐ’์„ ์ตœ๋Œ€๊ฐ’์œผ๋กœ ๊ฐ€์ ธ์˜ด
27+
28+
// K๋ณด๋‹ค ์ž‘์•„์งˆ ๋–„๊นŒ์ง€ s์ฆ๊ฐ€
29+
while(sum >= K) {
30+
// dp[s-1]+sum-K : ์‹œ์ž‘ ์œ„์น˜
31+
dp[e] = Math.max(dp[e], dp[s-1]+sum-K);
32+
sum -= arr[s];
33+
s++;
34+
}
35+
e++;
36+
}
37+
System.out.println(dp[N]);
38+
39+
}
40+
41+
}
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 JY_20291 {
5+
6+
public static void main(String[] args) throws IOException{
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
10+
Map<String, Integer> fMap = new TreeMap<>();
11+
12+
int N = Integer.parseInt(st.nextToken());
13+
for(int i=0; i<N; i++) {
14+
String[] fName = br.readLine().split("\\.");
15+
fMap.put(fName[1], fMap.getOrDefault(fName[1], 0)+1);
16+
}
17+
18+
for(String k : fMap.keySet()) {
19+
System.out.println(k+" "+fMap.get(k));
20+
}
21+
}
22+
23+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class JY_22944 {
5+
6+
static final int INF = Integer.MAX_VALUE;
7+
static int N, H, D;
8+
static char[][] g;
9+
static int[] dx = {-1, 1, 0, 0};
10+
static int[] dy = {0, 0, -1, 1};
11+
static boolean[][] visited;
12+
static List<int[]> uList;
13+
static int K;
14+
static int ans;
15+
16+
public static void main(String[] args) throws IOException {
17+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
20+
N = Integer.parseInt(st.nextToken());
21+
H = Integer.parseInt(st.nextToken());
22+
D = Integer.parseInt(st.nextToken());
23+
24+
g = new char[N][N];
25+
int sx = -1;
26+
int sy = -1;
27+
int ex = -1;
28+
int ey = -1;
29+
K = 0;
30+
uList = new ArrayList<>();
31+
for(int i=0; i<N; i++) {
32+
String line = br.readLine();
33+
for(int j=0; j<N; j++) {
34+
g[i][j] = line.charAt(j);
35+
if(g[i][j] == 'S') {
36+
sx = i;
37+
sy = j;
38+
}
39+
if(g[i][j] == 'U') {
40+
uList.add(new int[] {i, j});
41+
K++;
42+
}
43+
if(g[i][j] == 'E') {
44+
ex = i;
45+
ey = j;
46+
}
47+
}
48+
}
49+
uList.add(new int[] {ex, ey});
50+
51+
ans = INF;
52+
53+
// DFS ํƒ์ƒ‰ ์ง„ํ–‰
54+
visited = new boolean[N][N];
55+
visited[sx][sy] = true;
56+
dfs(sx, sy, 0, H, 0);
57+
58+
if(ans == INF) System.out.println(-1);
59+
else System.out.println(ans);
60+
61+
62+
}
63+
public static int calDist(int sx, int sy, int ex, int ey) {
64+
return Math.abs(sx-ex) + Math.abs(sy-ey);
65+
}
66+
public static void dfs(int x, int y, int depth, int hp, int up) {
67+
if(g[x][y] == 'E') {
68+
ans = Math.min(ans, depth);
69+
return;
70+
}
71+
if(depth >= ans) return;
72+
73+
for(int i=0; i<K+1; i++) {
74+
int[] u = uList.get(i);
75+
if(visited[u[0]][u[1]]) continue;
76+
77+
// ํ˜„์žฌ ์œ„์น˜ -> ๋‹ค์Œ ์šฐ์‚ฐ ์œ„์น˜์˜ ๊ฑฐ๋ฆฌ
78+
int nDist = calDist(x, y, u[0], u[1]);
79+
if(nDist > hp+up) continue;
80+
81+
int nhp = hp;
82+
// ์ฒด๋ ฅ์ด ์†Œ๋ชจ ๋˜๋Š” ๊ฒฝ์šฐ : ๋‚จ์€ ์šฐ์‚ฐ ๋‚ด๊ตฌ์„ฑ - ์ด๋™ํ•  ๊ฑฐ๋ฆฌ + 1(๋‹ค์Œ์œ„์น˜๋„ ์šฐ์‚ฐ์ด๋ฏ€๋กœ)
83+
int tmp = up - nDist + 1;
84+
if(tmp < 0) {
85+
nhp += tmp;
86+
}
87+
visited[u[0]][u[1]] = true;
88+
dfs(u[0], u[1], depth+nDist, nhp, D-1);
89+
visited[u[0]][u[1]] = false;
90+
}
91+
}
92+
93+
}
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
static int N, M;
7+
static int[][] g;
8+
static int[] dx = {-1, 1, 0, 0};
9+
static int[] dy = {0, 0, -1, 1};
10+
static boolean[][] visited;
11+
static int rCnt;
12+
static class Bomb implements Comparable<Bomb> {
13+
int x, y, color;
14+
15+
public Bomb(int x, int y, int color) {
16+
super();
17+
this.x = x;
18+
this.y = y;
19+
this.color = color;
20+
}
21+
22+
@Override
23+
public int compareTo(Bomb other) {
24+
if(this.x == other.x) {
25+
// 2) ์—ด์ด ์ž‘์€ ์ˆœ
26+
return this.y - other.y;
27+
}
28+
// 1) ํ–‰์ด ํฐ ์ˆœ
29+
return other.x - this.x;
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return "Bomb [x=" + x + ", y=" + y + ", color=" + color + "]";
35+
}
36+
37+
}
38+
39+
public static void main(String[] args) throws IOException {
40+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
41+
StringTokenizer st = new StringTokenizer(br.readLine());
42+
43+
N = Integer.parseInt(st.nextToken());
44+
M = Integer.parseInt(st.nextToken());
45+
46+
g = new int[N][N];
47+
for(int i=0; i<N; i++) {
48+
st = new StringTokenizer(br.readLine());
49+
for(int j=0; j<N; j++) {
50+
g[i][j] = Integer.parseInt(st.nextToken());
51+
}
52+
}
53+
54+
int ans = 0;
55+
while(true) {
56+
// 1) ํญํƒ„ ๋ฌถ์Œ ์ฐพ๊ธฐ
57+
List<Bomb> bList = findBundle();
58+
59+
// ๋”์ด์ƒ ํญํƒ„ ๋ฌถ์Œ์ด ์—†์œผ๋ฉด ์ข…๋ฃŒ
60+
if(bList.size() == 0) break;
61+
62+
// ํญํƒ„ ๋ฌถ์Œ ์ ์ˆ˜ ๊ณ„์‚ฐ
63+
int c = bList.size();
64+
ans += (c*c);
65+
66+
// 2) ํญํƒ„ ์ œ๊ฑฐ & ์ค‘๋ ฅ ์ž‘์šฉ
67+
removeBomb(bList);
68+
69+
setGravity();
70+
71+
// 3) ๋ฐ˜์‹œ๊ณ„ 90 ํšŒ์ „
72+
rotate();
73+
74+
// 4) ์ค‘๋ ฅ ์ž‘์šฉ
75+
setGravity();
76+
77+
}
78+
System.out.println(ans);
79+
80+
}
81+
public static void print(int[][] a) {
82+
for(int i=0; i<g.length; i++) {
83+
System.out.println(Arrays.toString(g[i]));
84+
}
85+
System.out.println();
86+
}
87+
public static boolean inRange(int x, int y) {
88+
return x>=0 && x<N && y>=0 && y<N;
89+
}
90+
public static List<Bomb> bfs(Bomb start) {
91+
Queue<Bomb> q = new LinkedList<>();
92+
List<Bomb> aList = new ArrayList<>();
93+
94+
visited[start.x][start.y] = true;
95+
q.add(start);
96+
aList.add(start);
97+
98+
// ๋นจ๊ฐ„ ํญํƒ„ ๋ฆฌ์ŠคํŠธ
99+
List<Bomb> rList = new ArrayList<>();
100+
101+
while(!q.isEmpty()) {
102+
Bomb now = q.poll();
103+
104+
for(int i=0; i<4; i++) {
105+
int nx = now.x + dx[i];
106+
int ny = now.y + dy[i];
107+
108+
if(!inRange(nx, ny)) continue;
109+
if(visited[nx][ny]) continue;
110+
if(g[nx][ny] != 0 && g[nx][ny] != start.color) continue;
111+
112+
Bomb next = new Bomb(nx, ny, g[nx][ny]);
113+
if(g[nx][ny] == 0) {
114+
rList.add(next);
115+
}
116+
117+
visited[nx][ny] = true;
118+
q.add(next);
119+
aList.add(next);
120+
}
121+
}
122+
123+
rCnt = rList.size();
124+
// ๋นจ๊ฐ„ ํญํƒ„๋“ค visited ์›์ƒ๋ณต๊ท€
125+
for(Bomb r: rList) {
126+
visited[r.x][r.y] = false;
127+
}
128+
129+
if(aList.size() <= 1) {
130+
return new ArrayList<>();
131+
}
132+
// ์šฐ์„ ์ˆœ์œ„์— ๋งž๊ฒŒ ์ •๋ ฌ ํ›„ ๋ฐ˜ํ™˜
133+
Collections.sort(aList);
134+
return aList;
135+
136+
}
137+
public static List<Bomb> findBundle() {
138+
// ๊ธฐ์ค€์  ์šฐ์„ ์ˆœ์œ„ ํ (ํฌ๊ธฐ->๋นจ๊ฐ„ํญํƒ„->ํ–‰->์—ด)
139+
PriorityQueue<int[]> pq = new PriorityQueue<>(new Comparator<int[]>() {
140+
@Override
141+
public int compare(int[] o1, int[] o2) {
142+
if(o1[0] == o2[0]) {
143+
if(o1[1] == o2[1]) {
144+
if(o1[2] == o2[2]) {
145+
// 4) ์—ด์ด ์ž‘์€ ์ˆœ
146+
return o1[3] - o2[3];
147+
}
148+
// 3) ํ–‰์ด ํฐ ์ˆœ
149+
return o2[2] - o1[2];
150+
}
151+
// 2) ๋นจ๊ฐ„ ํญํƒ„์˜ ๊ฐœ์ˆ˜๊ฐ€ ์ ์€ ์ˆœ
152+
return o1[1] - o2[1];
153+
}
154+
// 1) ๋ฌถ์Œ ํฌ๊ธฐ๊ฐ€ ํฐ ์ˆœ
155+
return o2[0] - o1[0];
156+
}
157+
});
158+
// ๊ธฐ์ค€์ ์˜ ์œ„์น˜์™€ ํญํƒ„ ๋ฌถ์Œ ์ €์žฅ ํ•ด์‰ฌ๋งต
159+
Map<Integer, List<Bomb>> tMap = new HashMap<>();
160+
visited = new boolean[N][N];
161+
for(int i=0; i<N; i++) {
162+
for(int j=0; j<N; j++) {
163+
// ๋นจ๊ฐ•(0), ๋Œ(-1), ๋นˆ์นธ(-2) ์ œ์™ธ ํญํƒ„๋“ค์ด ๋Œ€์ƒ
164+
if(g[i][j] > 0 && !visited[i][j]) {
165+
rCnt = 0;
166+
List<Bomb> bList = bfs(new Bomb(i, j, g[i][j]));
167+
168+
if(bList.size() == 0) continue;
169+
170+
Bomb center = bList.get(0);
171+
int cNum = center.x*N + center.y;
172+
if(tMap.getOrDefault(cNum, new ArrayList<>()).size() > bList.size()) {
173+
continue;
174+
}
175+
tMap.put(cNum, bList);
176+
pq.add(new int[] {bList.size(), rCnt, center.x, center.y});
177+
}
178+
}
179+
}
180+
// pq๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด ๋งŒ์กฑํ•˜๋Š” ํญํƒ„ ๋ฌถ์Œ๋“ค ์—†์Œ
181+
if(pq.size() == 0) return new ArrayList<>();
182+
// ๊ฐ€์žฅ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ๊ธฐ์ค€์ 
183+
int[] target = pq.poll();
184+
return tMap.get(target[2]*N + target[3]);
185+
186+
}
187+
public static void removeBomb(List<Bomb> bList) {
188+
// -2 : ๋นˆ์นธ
189+
for(Bomb b: bList) {
190+
g[b.x][b.y] = -2;
191+
}
192+
}
193+
public static void setGravity() {
194+
for(int y=0; y<N; y++) {
195+
for(int x=N-1; x>=0; x--) {
196+
if(g[x][y] < 0) continue;
197+
198+
// ํญํƒ„
199+
int color = g[x][y];
200+
g[x][y] = -2;
201+
int t = x;
202+
while(t < N) {
203+
if(t+1 == N || g[t+1][y] != -2) break;
204+
t++;
205+
}
206+
g[t][y] = color;
207+
}
208+
}
209+
}
210+
public static void rotate() {
211+
int[][] t = new int[N][N];
212+
213+
for(int x=0; x<N; x++) {
214+
for(int y=0; y<N; y++) {
215+
int nx = N - y - 1;
216+
int ny = x;
217+
t[nx][ny] = g[x][y];
218+
}
219+
}
220+
221+
g = t;
222+
}
223+
224+
}

0 commit comments

Comments
(0)

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