[BaekJoon] 14226 이모티콘 (Java)

오태호·2022년 11월 5일
0

백준 알고리즘

목록 보기
92/395

1.  문제 링크

https://www.acmicpc.net/problem/14226

2.  문제


요약

  • 영선이는 효빈이에게 스마일 이모티콘을 S개 보내려고 합니다.
  • 영선이는 이미 화면에 이모티콘 1개를 입력했고, 다음과 같은 3가지 연산만 사용해서 이모티콘을 S개 만들어 보려고 합니다.
    1. 화면에 있는 이모티콘을 모두 복사해서 클립보드에 저장합니다.
    2. 클립보드에 있는 모든 이모티콘을 화면에 붙여넣기 합니다.
    3. 화면에 있는 이모티콘 중 하나를 삭제합니다.
  • 모든 연산은 1초가 걸리고, 클립보드에 이모티콘을 복사하면 이전에 클립보드에 있던 내용은 덮어쓰기가 됩니다.
  • 클립보드가 비어있는 상태에는 붙여넣기를 할 수 없으며, 일부만 클립보드에 복사할 수 없습니다.
  • 클립보드에 있는 이모티콘 중 일부를 삭제할 수 없습니다.
  • 화면에 이모티콘을 붙여넣기 하면, 클립보드에 있는 이모티콘의 개수가 화면에 추가됩니다.
  • 영선이가 S개의 이모티콘을 화면에 만드는데 걸리는 시간의 최솟값을 구하는 문제입니다.
  • 입력: 첫 번째 줄에 2보다 크거나 같고 1000보다 작거나 같은 S가 주어집니다.
  • 출력: 첫 번째 줄에 이모티콘을 S개 만들기 위해 필요한 시간의 최솟값을 출력합니다.

3.  소스코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;
import java.util.StringTokenizer;

public class Main {
	static StringBuilder sb = new StringBuilder();
	static int S;
	static void input() {
		Reader scanner = new Reader();
		S = scanner.nextInt();
	}
	
	static void solution() {
		bfs();
		System.out.println(sb);
	}
	
	static void bfs() {
		Queue<Emoticon> length = new LinkedList<Emoticon>();
		length.offer(new Emoticon(1, 0, 0));
		HashMap<Emoticon, Integer> map = new HashMap<>();
		map.put(new Emoticon(1, 0, 0), 0);
		while(!length.isEmpty()) {
			Emoticon cur = length.poll();
			if(cur.length == S) {
				sb.append(cur.time);
				return;
			}
			if(cur.length + cur.clipLength <= S) {
				Emoticon next = new Emoticon(cur.length, cur.length, cur.time + 1);
				if(!map.containsKey(next) || (map.containsKey(next) && map.get(next) > cur.time + 1)) {
					map.put(next, cur.time + 1);
					length.offer(next);
				}
			}
			if(cur.length < S) {
				Emoticon next = new Emoticon(cur.length + cur.clipLength, cur.clipLength, cur.time + 1);
				if(!map.containsKey(next) || (map.containsKey(next) && map.get(next) > cur.time + 1)) {
					map.put(next, cur.time + 1);
					length.offer(next);
				}
			}
			if(cur.length > 1) {
				Emoticon next = new Emoticon(cur.length - 1, cur.clipLength, cur.time + 1);
				if(!map.containsKey(next) || (map.containsKey(next) && map.get(next) > cur.time + 1)) {
					map.put(next, cur.time + 1);
					length.offer(next);
				}
			}
		}
	}
	
	static class Emoticon {
		int length, clipLength, time;
		public Emoticon(int length, int clipLength, int time) {
			this.length = length;
			this.clipLength = clipLength;
			this.time = time;
		}
		@Override
		public boolean equals(Object obj) {
			Emoticon e = (Emoticon)obj;
			return (length == e.length) && (clipLength == e.clipLength);
		}
		@Override
		public int hashCode() {
			return Objects.hash(length, clipLength);
		}
	}
	
	public static void main(String[] args) {
		input();
		solution();
	}
	
	static class Reader {
		BufferedReader br;
		StringTokenizer st;
		public Reader() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while(st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch(IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
	}
}
profile
자바, 웹 개발을 열심히 공부하고 있습니다!

0개의 댓글