https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV597vbqAH0DFAVl
1. ๋ฌธ์ ์์ฝ
1. ๊ฐ์ฅ์๋ฆฌ ์ ๋์ฐฉ ์
-> ๊ตฐ์ง ๋ด ๋ฏธ์๋ฌผ ์ ๋ฐ์ผ๋ก ๊ฐ์, ์ ๋ฐ์ด 1๋ณด๋ค ์์ ๊ฒฝ์ฐ ์ฌ๋ผ์ง
-> ์ด๋๋ฐฉํฅ ๋ฐ๋
2. ์ฌ๋ฌ ๊ตฐ์ง์ด ํ๊ตฐ๋ฐ ๋ชจ์ผ ๊ฒฝ์ฐ
-> ๋ฏธ์๋ฌผ ์ ํฉ์นจ
-> ์ด๋๋ฐฉํฅ์ ๋ฏธ์๋ฌผ ์๊ฐ ๊ฐ์ฅ ๋ง์ ๊ตฐ์ง์ ๋ฐฉํฅ์ ๋ฐ๋ผ๊ฐ
2. ํ์ด
์๋ฎฌ๋ ์ด์ ๋ฌธ์ ์ด๋ค. ์๊ธฐ์์ด, ๋์์, ๋ฏธ์๋ฌผ ๊ฒฉ๋ฆฌ๋ ์๋ก ๋น์ทํ ๋ฌธ์ ๋ค์ด๋ค.
์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํด์ ํ์๋ค.
์ ๋ ฌ์กฐ๊ฑด์ ๋ฏธ์๋ฌผ ์ ๋ง์ ์์ผ๋ก ์ ๋ ฌํด์ฃผ์๋ค.
์ด๋ํ๊ธฐ ์ , pq์์ ์ขํ๋ฅผ ํ๋์ฉ ๊บผ๋ด์ด ๋ชฉํ ์ขํ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ๊ฐ๋ค์ ์ด๋์ํฌ ํ move_q์ ๋ฃ์ด์ค๋ค.
์ด๋ํ๊ธฐ ์ํด move_q์ ๊ฐ๋ค์ ํ๋์ฉ ๊บผ๋ด์ map์ ์ธํ ํด์ค๋ค.
๋ค์ map์ ๋ณด๋ฉด์ null์ด ์๋ ์ขํ๋ pq์ ๋ฃ์ด์ค๋ค.
์ฐ์ ์์ ํ๋ฅผ 2๊ฐ ์จ์ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋จ๋ค๋ณด๋ค 2๋ฐฐ๋ ๋ ๋ง์ด ๋์๋ค.
๋๋ถ๋ถ list๋ก ๋ง์ด ํ๋๋ผ. ๋๋ list๋ก ๋ค์ ํ์ด๋ด์ผ ๊ฒ ๋ค.
๊ทธ๋ฆฌ๊ณ ์ด๊ฑฐ ํ๋ฉด์ ์๋กญ๊ฒ ์๊ฒ๋ ์ ์ด ์๋ค.
์ฐ์ ์์ํ(PriorityQueue) ์ฐ๋๋ฐ ์ ๋ ฌ ์กฐ๊ฑด ์์ฃผ๋ฉด ๋ฐํ์์๋ฌ๊ฐ ๋๋ค!
3. ์ฝ๋
๋ฆฌ์คํธ ์ฌ์ฉ
์ฐ์ ์์ ํ ์ฌ์ฉ
import java.io.*;
import java.util.*;
public class SW_2382_๋ฏธ์๋ฌผ๊ฒฉ๋ฆฌ {
static int N, M, K, ans;
static Pos[][] map;
static PriorityQueue<Pos> pq; //๋ฏธ์๋ฌผ ์ ๋ง์ ์์ผ๋ก ์ ๋ ฌ
static PriorityQueue<Pos> move_q;
static int[] dy = {-1,1,0,0};
static int[] dx = {0,0,-1,1};
public static void main(String[] args) throws Exception {
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = null;
int T = Integer.parseInt(br.readLine());
for (int tc=1; tc<=T; tc++) {
st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
K = Integer.parseInt(st.nextToken());
map = new Pos[N][N];
pq = new PriorityQueue<> ();
for (int i=0; i<K; i++) {
st = new StringTokenizer(br.readLine());
int y = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int cnt = Integer.parseInt(st.nextToken());
int dir = Integer.parseInt(st.nextToken())-1;
pq.offer(new Pos(y, x, cnt, dir));
map[y][x] = new Pos(y, x, cnt, dir);
} //์
๋ ฅ ๋
ans = 0;
solution();
System.out.println("#"+tc+" "+ans);
}
}
static void solution() {
for (int time=0; time<M; time++) {
if (time>0) getCell(); //null์ด ์๋๋ฉด pq์ ๋ฃ์ด์ค
move(); //์ด๋
}
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (map[i][j] ==null) continue;
ans += map[i][j].cnt;
}
}
}
static void move() {
move_q = new PriorityQueue<> ();
while (!pq.isEmpty()) {
Pos cur = pq.poll();
int y = cur.y;
int x = cur.x;
int cnt = cur.cnt;
int d = cur.dir;
map[y][x] = null;
int ny = cur.y+dy[d];
int nx = cur.x+dx[d];
if (ny==0 || ny==N-1 || nx==0 || nx==N-1) { //๊ฐ์ฅ์๋ฆฌ์ผ ๋
if (cnt/2 >= 1) {
int nd=0;
switch (d) {
case 0: nd=1; break;
case 1: nd=0; break;
case 2: nd=3; break;
case 3: nd=2; break;
}
move_q.offer(new Pos(ny, nx, cnt/2, nd));
}
} else {
move_q.offer(new Pos(ny, nx, cnt, d));
}
}
while (!move_q.isEmpty()) {
Pos cur = move_q.poll();
int y = cur.y;
int x = cur.x;
int cnt = cur.cnt;
int d = cur.dir;
if (map[y][x] != null) {
map[y][x].cnt += cnt; //๋ฏธ์๋ฌผ ์๋ง ๋ํด์ค
} else {
map[y][x] = cur;
}
}
}
static void getCell() {
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
if (map[i][j] ==null) continue;
pq.offer(map[i][j]);
}
}
}
static class Pos implements Comparable<Pos>{
int y, x, cnt, dir;
Pos(int y, int x, int cnt, int dir) {
this.y = y;
this.x = x;
this.cnt = cnt;
this.dir = dir;
}
@Override
public int compareTo(Pos o) {
if (o.cnt - this.cnt == 0) {
if (this.y - o.y == 0) {
return this.x - o.x;
}
else return this.y - o.y;
}
else return o.cnt - this.cnt;
}
}
}
4. ์ฌ๋ด
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SWEA] 5208.์ ๊ธฐ๋ฒ์ค2 - Java (0) | 2022.11.20 |
---|---|
[SWEA] 7733. ์น์ฆ ๋๋ - Java (0) | 2022.11.20 |
[SWEA] 4727. ๊ฒฌ์ฐ์ ์ง๋ (18.8.6 ์์ ) - Java (0) | 2022.11.20 |
[SWEA] 2819. ๊ฒฉ์ํ์ ์ซ์ ์ด์ด ๋ถ์ด๊ธฐ (1) | 2022.11.20 |
[SWEA] 8382. ๋ฐฉํฅ ์ ํ - Java (0) | 2022.11.14 |