-
[백준] 리모컨 - 1107번알고리즘 문제 풀이/백준 2021. 6. 28. 20:31
문제설명
https://www.acmicpc.net/problem/1107
1107번: 리모컨
첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다. 둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다. 고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼
www.acmicpc.net
기본아이디어
0 ~ 9 사이의 버튼 중 있는 버튼으로 최대한 가깝게 간 다음에 +, - 로 조절하는 방식으로 구현했다.
구현코드
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152public static void main(String[] args) throws IOException {N = Integer.parseInt(input.readLine());M = Integer.parseInt(input.readLine());remote_control = new boolean[10];if(M != 0) {StringTokenizer st = new StringTokenizer(input.readLine());for (int i = 0; i < M; i++) {int button = Integer.parseInt(st.nextToken());remote_control[button] = true;}}// +, - 로만 이동int ans = Math.abs(N-100);// 한번에 이동 가능?int once = canMove(N);if(once != -1){ans = Math.min(ans, once);}int count = 1;boolean flag = true;while (flag && count < ans) {int plus = N + count;int minus = N - count;int plus_move = canMove(plus);int minus_move = canMove(minus);if(plus_move != -1){ans = Math.min(ans, plus_move + count);flag = false;}if(minus_move != -1){ans = Math.min(ans, minus_move + count);flag = false;}count++;}System.out.println(ans);}static int N, M;static boolean[] remote_control;static int canMove(int a){if(a < 0)return -1;String num = String.valueOf(a);for(int i = 0; i < num.length(); i++){if(remote_control[num.charAt(i) - '0'])return -1;}return num.length();}cs 정리, 기억할 내용
1. 문제를 꼼꼼히 읽자
'알고리즘 문제 풀이 > 백준' 카테고리의 다른 글
[백준] 주사위 굴리기 - 14499번 (0) 2021.06.29 [백준] 인구 이동 - 16234번 (0) 2021.06.28 [백준] DSLR - 9019번 (0) 2021.06.23 [백준] AC - 5430번 (0) 2021.06.23 [백준] 감시 - 15683번 (0) 2021.04.21