https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5-BEE6AK0DFAVl
SW Expert Academy
SW ํ๋ก๊ทธ๋๋ฐ ์ญ๋ ๊ฐํ์ ๋์์ด ๋๋ ๋ค์ํ ํ์ต ์ปจํ ์ธ ๋ฅผ ํ์ธํ์ธ์!
swexpertacademy.com
1. ๋ฌธ์ ์์ฝ
๋ชจ๋ ์ฌ๋๋ค์ด ๊ณ๋จ์ ๋ด๋ ค๊ฐ ์ด๋ํ๋ ์ต์ ์๊ฐ์ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.๊ณ๋จ๊น์ง ์ด๋ํ๋์๊ฐ + ๊ณ๋จ์ ํ๊ณ ๋ด๋ ค๊ฐ๋ ์๊ฐ ์ ์ต์๊ฐ์ ๊ตฌํ๋ค.์ ์ฝ์ฌํญ์ผ๋ก๋ ์ด๋ง๋ค ๊ณ๋จ์ 3๋ช ์ด ์์ผ๋ฉด 1์ด ๋๊ธฐ ํ ์ด๋ํด์ผ ํ๋ค.๊ณ๋จ์ ์ด 2๊ฐ์ด๋ฉฐ, ๊ณ๋จ์ ํ๊ณ ๋ด๋ ค๊ฐ๋ ์๊ฐ์ ๊ณ๋จ์ ๊ธธ์ด์ ๋์ผํ๋ค.๊ณ๋จ๊น์ง ์ด๋ํ๋ ์๊ฐ์ ๋งจํํ๊ณต์์ ์ด์ฉ
2. ํ์ด
๋ถ๋ถ์งํฉ์ ์ฌ์ฉํ์ฌ ์์ ํ์์ผ๋ก ๊ฐ๋ฅํ ๋ชจ๋ ์กฐํฉ์ ๊ณ์ฐํ๋ค.1. ๋ถ๋ถ์งํฉ์ผ๋ก ์ฌ๋๋ง๋ค ์ฌ์ฉํ ๊ณ๋จ์ ์ ํํ๋ค.
2. ๊ธฐ์ ์กฐ๊ฑด์์, ๊ฐ ๊ณ๋จ์ ์ฌ์ฉํ๋ ์ฌ๋๋ค์ ์ฐ์ ์์ํ์ ๋ฃ๋๋ค. ์ด ๋, ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์์๋ก ์ ๋ ฌ๋๋ค.
1) ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์ฌ๋๋ถํฐ ํ์์ ๊บผ๋ด, ์์์๊ฐ ~ ๋์ฐฉ์๊ฐ ๋ฐ๋ณต๋ฌธ์ ๋๋ ค ๋๊ธฐ์์๋ฅผ ๊ณ ๋ คํ์ฌ ๊ณ์ฐํ๋ค
์์์๊ฐ : ๊ณ๋จ ์ด๋์๊ฐ(๋งจํํ๊ฑฐ๋ฆฌ ๊ณต์) + 1(๋์ฐฉ ํ ๋๊ธฐ์๊ฐ)
๋์ฐฉ์๊ฐ : ํ๊ณ ๋ด๋ ค๊ฐ๋์๊ฐ(๊ณ๋จ์ ๊ธธ์ด)์ ๊ณ์ฐํ๋ค.
๋๊ธฐ : time[๋งค ์ด] => ๋งค ์ด์ ๊ณ๋จ์ ์๋ ์ฌ๋์ ์๋ฅผ ์ ์ฅ, 3์ด๋ฉด ๋์ฐฉ์๊ฐ+1 ํ๊ณ ๋ค์์ด๋ถํฐ ๊ณ์ฐ(continue)
2) 0๋ฒ ๊ณ๋จ๊ณผ 1๋ฒ๊ณ๋จ์ end ์ค ํฐ ๊ฐ์ด ์ต์ข ์๊ฐ์ด ๋๋ค.
3. ๋ชจ๋ ์กฐํฉ์ ์ต์ข ์๊ฐ ์ค ์ต์๊ฐ์ ์ฐพ๊ธฐ ์ํด min์ ๊ฐฑ์ ํด์ค๋ค.
3. ์ฝ๋
import java.util.*;
import java.io.*;
public class SW_2383_์ ์ฌ์์ฌ์๊ฐ {
static int N, perIdx, min;
static int[][] map;
static Stair[] stair;
static Person[] per;
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++) {
N = Integer.parseInt(br.readLine());
map = new int[N][N];
stair = new Stair[2];
per = new Person[N*N];
int idx = 0; //๊ณ๋จ๋ฒํธ 0,1
perIdx = 0;
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < N; j++) {
map[i][j] = Integer.parseInt(st.nextToken());
if (map[i][j] == 1) { //์ฌ๋์ด๋ฉด ์ขํ์ ์ฅ
per[perIdx++] = new Person(i, j);
}
if (map[i][j] >= 2) { //๊ณ๋จ๊ธธ์ด
stair[idx++] = new Stair(i, j,map[i][j] ); //i:๊ณ๋จy์ขํ,j:๊ณ๋จx์ขํ,len:๊ณ๋จ๊ธธ์ด
}
}
}
min = Integer.MAX_VALUE;
subset(0);
System.out.println("#"+tc+" "+min);
}
}
static void subset(int cnt) {
if (cnt == perIdx) { //๊ฐ์ ๊ณ๋จ ์ ํ ์๋ฃ => ์ด์ ์๊ฐ ์ต์๊ฐ์ ์ฐพ์
int totalTime = 0;
for (int i = 0; i < 2; i++) {
//1. ์ฐ์ ์์ ํ์ ํด๋น ๊ณ๋จ์ธ ์ฌ๋๋ค๋ง ๊ฐ๊น์ด ์์ผ๋ก ๋ฃ์ด์ค
PriorityQueue<Person> pq = new PriorityQueue<> ();
int time[] = new int[100]; // ๋งค ์ด๋ง๋ค ์ฌ๋ ์ ์ ์ฅ
for (int j = 0; j < perIdx; j++) {
if (per[j].stair == i) {
pq.add(per[j]); //์ ์ฅํ๋ฉด ํด๋น ๊ณ๋จ๊ณผ ๊ฑฐ๋ฆฌ๊ฐ ์งง์ ์ฌ๋ ์์ผ๋ก ์ ๋ ฌ๋จ
}
}
//2. ๊ฐ๊น์ด์์๋ถํฐ ๊ณ๋จํ๊ณ ๋ด๋ ค๊ฐ๋ ์๊ฐ ๊ณ์ฐ
while (!pq.isEmpty()) {
Person front = pq.poll();
int start = front.moveTime; //๊ณ๋จ๊น์ง ์ด๋๊ฑฐ๋ฆฌ(=์ด๋์๊ฐ)
int end = start+stair[i].len; //๊ณ๋จ ๋ค๋ด๋ ค๊ฐ ์๊ฐ
for (int j = start; j < end; j++) {
if (time[j] == 3) {
end++;
continue;
}
time[j]++;
}
totalTime = Math.max(totalTime, end); //์ต์ข
์๊ฐ์ end์ ๋ง์ง๋ง ๊ฐ, 0๊ณ๋จ end์ 1๊ณ๋จend ์ค ํฐ๊ฒ ์ต์ข
์๊ฐ์
}
}
min = Math.min(totalTime, min); //๊ฐ ์กฐํฉ๋ค์ ๊ฑธ๋ฆฐ์๊ฐ ์ค ์ต์์๊ฐ์ ์ฐพ์
return;
}
//1๋ฒ๊ณ๋จ ์ ํ
per[cnt].stair=0;
per[cnt].moveTime = 1 + Math.abs( per[cnt].y-stair[0].y ) + Math.abs( per[cnt].x-stair[0].x );
subset(cnt+1);
//2๋ฒ๊ณ๋จ ์ ํ
per[cnt].stair=1;
per[cnt].moveTime = 1 + Math.abs( per[cnt].y-stair[1].y ) + Math.abs( per[cnt].x-stair[1].x );
subset(cnt+1);
}
static class Person implements Comparable<Person>{
int y, x, moveTime, stair; //dist:๊ณ๋จ๊ณผ์๊ฑฐ๋ฆฌ, t:์ ํํ ๊ณ๋จ
public Person(int y, int x) {
this.y = y;
this.x = x;
}
@Override
public int compareTo(Person o) {
return this.moveTime - o.moveTime; //d์์์์ผ๋ก ์ ๋ ฌ
}
}
static class Stair {
int y, x, len;
public Stair(int y, int x, int len) {
this.y = y;
this.x = x;
this.len = len;
}
}
}
'Algorithm > SWEA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SWEA] 14510. ๋๋ฌด ๋์ด - Java (0) | 2022.10.26 |
---|---|
[SWEA] 2115. [๋ชจ์ SW ์ญ๋ํ ์คํธ] ๋ฒ๊ฟ์ฑ์ทจ - Java (0) | 2022.10.24 |
[SWEA] 5650. [๋ชจ์ SW ์ญ๋ํ ์คํธ] ํ๋ณผ ๊ฒ์ Java (0) | 2022.10.22 |
[SWEA] 2112. [๋ชจ์ SW ์ญ๋ํ ์คํธ] ๋ณดํธ ํ๋ฆ - Java (0) | 2022.10.18 |
[SWEA] 5644. [๋ชจ์ SW ์ญ๋ํ ์คํธ] ๋ฌด์ ์ถฉ์ Java (0) | 2022.10.01 |