백준 14226 이모티콘 (Java,자바)

jonghyukLee·2022년 1월 6일
0

이번에 풀어본 문제는
백준 14226번 이모티콘 입니다.

📕 문제 링크

❗️코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

class Emo
{
    int chat,clip,time;

    public Emo(int chat,int clip, int time)
    {
        this.chat = chat;
        this.clip = clip;
        this.time = time;
    }
}
public class Main {
    static int S;
    static Queue<Emo> q;
    static boolean [][] visited;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        S = Integer.parseInt(br.readLine());

        q = new LinkedList<>();
        visited = new boolean[2000][2000];
        q.add(new Emo(1,0,0));
        visited[1][0] = true;
        int answer = 0;
        while(!q.isEmpty())
        {
            Emo cur = q.poll();
            if(cur.chat < 1) continue;
            if(cur.chat == S)
            {
                answer = cur.time;
                break;
            }

            for(int i = 0; i < 3; ++i)
            {
                Emo tmp = run(i,cur);
                if(tmp.chat + tmp.clip >= 2000) continue;
                if(!visited[tmp.chat][tmp.clip])
                {
                    visited[tmp.chat][tmp.clip] = true;
                    q.add(tmp);
                }
            }
        }
        System.out.print(answer);
    }
    static Emo run(int flag, Emo cur)
    {
        if(flag == 0) return new Emo(cur.chat,cur.chat,cur.time+1);
        else if(flag == 1) return new Emo(cur.chat+cur.clip,cur.clip,cur.time+1);
        else return new Emo(cur.chat-1,cur.clip,cur.time+1);
    }
}

📝 풀이

입력받은 S개의 이모티콘을 화면에 출력하기 위해서 필요한 최소 연산횟수(초)를 구하는 문제입니다.
연산은 복사,붙여넣기,삭제 3가지가 있습니다.
현재 화면에 출력된 이모티콘이 0이하로 내려가거나, 붙여넣기 연산을 했을 때 범위를 초과하게되면 지나치게 되고, 나머지 경우의 연산을 통해 가장 먼저 S에 도달하는 time값을 출력해주면 해결됩니다.

📜 후기

방문처리가 가장 까다로웠던 문제입니다. S 입력의 최댓값이 1000이고, 시작은 무조건 1개인 상태로 시작하기 때문에 정답과 과정은 변하지 않습니다.
S가 최댓값으로 1000일때, 연산 과정에서 범위가 1000을 넘어가지 않으므로 사실 방문배열크기를 2000까지 사용할 필요는 없었지만, 풀때는 1000을 벗어나서 삭제연산으로 내려와야 할 경우까지 고려하다보니 2000으로 풀었습니다.

profile
머무르지 않기!

0개의 댓글