๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/14226
๐ฎ ๋ฌธ์ ํด๊ฒฐ ๋ฐฉ๋ฒ
BFS๋ฅผ ์ด์ฉํ ์์ ํ์ ๋ฐฉ์์ผ๋ก ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ค.
- ์ต์ด์ ๊ฒฝ์ฐ์๋ ํ๋ฉด์๋ง ์ด๋ชจํฐ์ฝ์ด ์์ผ๋ฏ๋ก ํ์ ๋ฃ๊ณ ์์ํ๋ค.
- ํ์ฌ ์๊ฐ์๋ง ํ ์ ์๋ ํ๋์ ๊ตฌ๋ถํ๊ธฐ ์ํด์ ํ์ ์ฌ์ด์ฆ๋งํผ๋ง ํ์ฌ ์ด์๋ ๋ฐ๋ณต์ ๋๊ฒ ํ๋ค.
- ๊ฐ๊ฐ์ ํ๋์ ์๋์ ๊ฐ์ ์กฐ๊ฑด์ ๋ฐ๋ผ์ ํ ์ง ๋ง์ง ๊ตฌ๋ถํ ์ ์๋ค.
- ํด๋ฆฝ๋ณด๋์ ์๋ ๋ชจ๋ ์ด๋ชจํฐ์ฝ์ ํ๋ฉด์ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ค.
- ๋ณด๋ด๋ ค๋ ์ด๋ชจํฐ์ฝ ๊ฐ์๋ณด๋ค ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ์ ๊ฐ์๊ฐ ๋ ์ ์ ๊ฒฝ์ฐ์ ์ด๋ชจํฐ์ฝ์ ๋ณต์ฌํ๋ ๊ฒ์ด ์๋ฏธ์๋ค.
- ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ์ ๋ชจ๋ ๋ณต์ฌํด์ ํด๋ฆฝ๋ณด๋์ ์ ์ฅํ๋ค.
- ๋ณด๋ด๋ ค๋ ์ด๋ชจํฐ์ฝ ๊ฐ์๋ณด๋ค ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ์ ๊ฐ์๊ฐ ๋ ์ ์ ๊ฒฝ์ฐ์ ์ด๋ชจํฐ์ฝ์ ํด๋ฆฝ๋ณด๋๋ก ๊ฐ์ ธ๊ฐ๊ณ ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ ๊ฐ์๊ฐ 1๊ฐ ์ด์์ด์ด์ผ ํ๋ค.
- ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ ์ค ํ๋๋ฅผ ์ญ์ ํ๋ค.
- ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ ๊ฐ์๊ฐ 1๊ฐ ์ด์์ด์ด์ผ ํ๋ค.
- ํด๋ฆฝ๋ณด๋์ ์๋ ๋ชจ๋ ์ด๋ชจํฐ์ฝ์ ํ๋ฉด์ ๋ถ์ฌ๋ฃ๊ธฐ ํ๋ค.
- ๋ชจ๋ ํ๋์ [ํ๋ฉด์ ์ด๋ชจํฐ์ฝ ๊ฐ์]์ [ํด๋ฆฝ๋ณด๋ ์ด๋ชจํฐ์ฝ ๊ฐ์]๋ฅผ ์ฒดํฌํด ์ค๋ณต์ธ ๊ฒฝ์ฐ๋ ํ์ ๋ฃ์ง ์๋๋ก ํ๋ค. → 2์ฐจ์ ๋ฐฐ์ด์ ์ด์ฉํ ์ค๋ณต ์ฒดํฌ
- ๋งค ์ด๋ค๋ง ํ ์ฌ์ด์ฆ๋ ํ์ฌ ์ด์ ํํด์ง๋ ํ๋๋ค์ด๊ธฐ ๋๋ฌธ์ ์ด๋ชจํฐ์ฝ ๊ฐ์๊ฐ S๊ฐ ๋๋ ์ต์ด์ ๊ฒฝ์ฐ๊ฐ ์ต์๋ก ๋ง๋ค ์ ์๋ ์๊ฐ์ด๋ค.
โ๏ธ ํ์ ๋ค์ด๊ฐ๋ ์์์ ์ค๋ณต์ ์ ๊ฑฐํด์ฃผ๋ ๋ถ๋ถ์ด ์ค์ํ๋ค.
๐ฉ Java ์ฝ๋
package com.second.algorithm.baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
public class Main_14226_์ด๋ชจํฐ์ฝ {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int S = Integer.parseInt(br.readLine());
int rst = sendEmoticon(S);
System.out.println(rst);
br.close();
}
private static int sendEmoticon(int s) {
boolean[][] check = new boolean[2000][1001]; // ์คํฌ๋ฆฐ์ 999, ํด๋ฆฝ๋ณด๋์ 999์ธ ๊ฒฝ์ฐ ์ต๋
Queue<Emoticon> queue = new LinkedList<>();
queue.add(new Emoticon(1, 0));
int second = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int loop = 0; loop < size; loop++) {
Emoticon emoticon = queue.poll();
int screenEmoticon = emoticon.getScreenEmoticon();
int clipboardEmoticon = emoticon.getClipboardEmoticon();
// ํ๋ฉด์ ์ ์ฅ๋ ์ด๋ชจํฐ์ฝ ๊ฐ์๊ฐ ๋์ผํ ๊ฒฝ์ฐ ์ต์ด ๊ฒฝ์ฐ
if (screenEmoticon == s) return second;
// ํ๋ฉด์ ์๋ ์ด๋ชจํฐ์ฝ์ด ๋ณด๋ด๋ ค๋ ์ด๋ชจํฐ์ฝ๋ณด๋ค ์์ ๊ฒฝ์ฐ์๋ง ํด๋ฆฌ๋ณด๋์์ ๋ณต์ฌํด์ ๋ถํ๊ธฐ
if (screenEmoticon < s && !check[screenEmoticon + clipboardEmoticon][clipboardEmoticon]) {
check[screenEmoticon + clipboardEmoticon][clipboardEmoticon]= true;
queue.add(new Emoticon(screenEmoticon + clipboardEmoticon, clipboardEmoticon));
}
if (screenEmoticon > 0) {
// ํ๋ฉด์์ ํ๊ฐ ์ง์ฐ๊ธฐ
if (!check[screenEmoticon - 1][clipboardEmoticon]) {
check[screenEmoticon - 1][clipboardEmoticon] = true;
queue.add(new Emoticon(screenEmoticon - 1, clipboardEmoticon));
}
// ํ๋ฉด์์ ํด๋ฆฝ๋ณด๋๋ก ๋ณต์ฌํ๊ธฐ (ํ๋ฉด ์ด๋ชจํฐ์ฝ์ด ๋ณด๋ด๋ ค๋ ์ด๋ชจํฐ์ฝ ๊ฐ์ ๋ณด๋ค ์์ ๊ฒฝ์ฐ์๋ง ๋ณต์ฌ)
if (screenEmoticon < s && !check[screenEmoticon][screenEmoticon]) {
check[screenEmoticon][screenEmoticon] = true;
queue.add(new Emoticon(screenEmoticon, screenEmoticon));
}
}
}
second++;
}
return second;
}
static class Emoticon {
private int screenEmoticon;
private int clipboardEmoticon;
public Emoticon(int screenEmoticon, int clipboardEmoticon) {
this.screenEmoticon = screenEmoticon;
this.clipboardEmoticon = clipboardEmoticon;
}
public int getScreenEmoticon() {
return screenEmoticon;
}
public int getClipboardEmoticon() {
return clipboardEmoticon;
}
}
}
๋ฐ์ํ
'Algorithm > BFS & DFS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค, Java] 1039๋ฒ : ๊ตํ (0) | 2022.07.22 |
---|---|
[๋ฐฑ์ค, Java] 19621๋ฒ : ํ์์ค ๋ฐฐ์ 2 (0) | 2022.05.23 |
[๋ฐฑ์ค, Java] 9934๋ฒ : ์์ ์ด์ง ํธ๋ฆฌ (0) | 2022.05.20 |
[๋ฐฑ์ค, Java] 16928๋ฒ : ๋ฑ๊ณผ ์ฌ๋ค๋ฆฌ ๊ฒ์ (0) | 2022.05.09 |
[๋ฐฑ์ค, Java] 1967๋ฒ : ํธ๋ฆฌ์ ์ง๋ฆ (0) | 2022.05.08 |