Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 알고리즘
- Algorithm
- Arrays
- 프로그래머스 네트워크 java
- Codility
- 백준 16927
- 코딩테스트
- 프로그래머스 연속된 수의 합 java
- 백준 18290
- 네트워크
- 프로그래머스 옹알이 java
- 백준 15661
- java 반올림
- Math.ceil()
- 자바
- 프로그래머스 숫자의 표현 java
- 백준 16935
- time complexity
- java
- java 올림
- 백준 11723
- 백준 17425
- 백준 14391
- 0으로 채우기
- Math.floor()
- 백준 4375
- mysql
- java 내림
- 프로그래머스 도둑질 java
- sort
Archives
- Today
- Total
취미처럼
[백준] 14226번 이모티콘 본문
https://www.acmicpc.net/problem/14226
14226번: 이모티콘
영선이는 매우 기쁘기 때문에, 효빈이에게 스마일 이모티콘을 S개 보내려고 한다. 영선이는 이미 화면에 이모티콘 1개를 입력했다. 이제, 다음과 같은 3가지 연산만 사용해서 이모티콘을 S개 만
www.acmicpc.net
화면에있는이모티콘수, 클립보드에 있는 이모티콘수 를 이차원 배열로 상태관리
import java.util.*;
import java.io.*;
public class Main {
static int max = 1001;
static int S;
static boolean[][] visit = new boolean[max][max];
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
S = sc.nextInt();
int result = bfs(S);
System.out.println(result);
}
public static int bfs(int s) {
Queue<Emo> q = new LinkedList<>();
q.offer(new Emo(1, 0, 0));
// 화면갯수, 클립보드 갯수
visit[1][0] = true;
int retVal = -1;
while (!q.isEmpty()) {
Emo curEmo = q.poll();
int scrCount = curEmo.getScrCount();
int clipCount = curEmo.getClipCount();
int time = curEmo.getTime();
if (s == scrCount) {
retVal = time;
break;
}
if (scrCount > 0 && scrCount < max) {
// 화면에 있는 이모티콘 복사 후 클립보드 저장
if (!visit[scrCount][scrCount]) {
visit[scrCount][scrCount] = true;
q.offer(new Emo(scrCount, scrCount, time + 1));
}
// 클립보드에 있는 이모티콘 복사 후 화면에 플러스
if (clipCount > 0 && (scrCount + clipCount) < max) {
if (!visit[scrCount + clipCount][clipCount]) {
visit[scrCount + clipCount][clipCount] = true;
q.offer(new Emo(scrCount + clipCount, clipCount, time + 1));
}
}
// 화면에서 1개 삭제
if (!visit[scrCount - 1][clipCount]) {
visit[scrCount - 1][clipCount] = true;
q.offer(new Emo(scrCount-1 , clipCount, time + 1));
}
}
}
return retVal;
}
private static class Emo {
int scrCount;
int clipCount;
int time;
public Emo(int scrCount, int clipCount, int time) {
this.scrCount = scrCount;
this.clipCount = clipCount;
this.time = time;
}
public int getScrCount() {
return scrCount;
}
public int getClipCount() {
return clipCount;
}
public int getTime() {
return time;
}
}
}
'Algorithm > 백준' 카테고리의 다른 글
[백준] 1261번 알고스팟 (0) | 2021.03.15 |
---|---|
[백준] 13549번 숨바꼭질3 (0) | 2021.03.11 |
[백준] 13913번 숨바꼭질 4 (0) | 2021.03.11 |
[백준] 1697번 숨바꼭질 (0) | 2021.03.11 |
[백준] 7562번 나이트의 이동 (0) | 2021.03.11 |
Comments