백준 1107번 (리모컨, C++, Brute Force) / 추가 반례 [BAEKJOON]

리모컨

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

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초256 MB82926200111391022.624%

문제

수빈이는 TV를 보고 있다. 수빈이는 채널을 돌리려고 했지만, 버튼을 너무 세게 누르는 바람에, 일부 숫자 버튼이 고장났다.

리모컨에는 버튼이 0부터 9까지 숫자, +와 -가 있다. +를 누르면 현재 보고있는 채널에서 +1된 채널로 이동하고,

-를 누르면 -1된 채널로 이동한다. 채널 0에서 -를 누른 경우에는 채널이 변하지 않고, 채널은 무한대 만큼 있다.

수빈이가 지금 이동하려고 하는 채널은 N이다. 어떤 버튼이 고장났는지 주어졌을 때,

채널 N으로 이동하기 위해서 버튼을 최소 몇 번 눌러야하는지 구하는 프로그램을 작성하시오. 

수빈이가 지금 보고 있는 채널은 100번이다.

입력

첫째 줄에 수빈이가 이동하려고 하는 채널 N (0 ≤ N ≤ 500,000)이 주어진다.  

둘째 줄에는 고장난 버튼의 개수 M (0 ≤ M ≤ 10)이 주어진다.

고장난 버튼이 있는 경우에는 셋째 줄에는 고장난 버튼이 주어지며, 같은 버튼이 여러 번 주어지는 경우는 없다.

출력

첫째 줄에 채널 N으로 이동하기 위해 버튼을 최소 몇 번 눌러야 하는지를 출력한다.

예제 입력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5457
3
6 7 8
5457 3 6 7 8
5457
3
6 7 8

예제 출력 1

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
6
6
6

예제 입력 2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
100
5
0 1 2 3 4
100 5 0 1 2 3 4
100
5
0 1 2 3 4

예제 출력 2

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

예제 입력 3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
500000
8
0 2 3 4 6 7 8 9
500000 8 0 2 3 4 6 7 8 9
500000
8
0 2 3 4 6 7 8 9

예제 출력 3

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
11117
11117
11117

예제 입력 4

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
100
3
1 0 5
100 3 1 0 5
100
3
1 0 5

예제 출력 4

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0

예제 입력 5

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
14124
0
14124 0
14124
0

예제 출력 5

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
5
5
5

예제 입력 6

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
9
1 2 3 4 5 6 7 8 9
1 9 1 2 3 4 5 6 7 8 9
1
9
1 2 3 4 5 6 7 8 9

예제 출력 6

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2
2
2

예제 입력 7

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
80000
2
8 9
80000 2 8 9
80000
2
8 9

예제 출력 7

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
2228
2228
2228

예제 입력 A

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
9
1 2 3 4 5 6 7 8 9
0 9 1 2 3 4 5 6 7 8 9
0
9
1 2 3 4 5 6 7 8 9

예제 출력 A

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
1
1

예제 입력 B

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
140200
3
4 5 6
140200 3 4 5 6
140200
3
4 5 6

예제 출력 B

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
207
207
207

예제 입력 C

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1555
3
0 1 9
1555 3 0 1 9
1555
3
0 1 9

예제 출력 C

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
670
670
670

예제 입력 D

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
99999
9
0 2 3 4 5 6 7 8 9
99999 9 0 2 3 4 5 6 7 8 9
99999
9
0 2 3 4 5 6 7 8 9

예제 출력 D

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
11118
11118
11118

예제 입력 E

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
50000
9
0 1 2 3 4 5 6 7 8
50000 9 0 1 2 3 4 5 6 7 8
50000
9
0 1 2 3 4 5 6 7 8

예제 출력 E

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
40005
40005
40005

예제 입력 F

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
139133
7
9 6 8 2 1 5 7
139133 7 9 6 8 2 1 5 7
139133
7
9 6 8 2 1 5 7

예제 출력 F

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
94694
94694
94694

예제 입력 G

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0
0
0 0
0
0

예제 출력 G

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1
1
1

힌트

예제 1의 경우 5455++ 또는 5459–

출처

  • 데이터를 추가한 사람: applyoo
  • 문제를 번역한 사람: baekjoon
  • 잘못된 조건을 찾은 사람: jh05013

알고리즘 분류


아래에서 위로 올라가는 방법도 있지만 위에서 아래로 내려가는 방법도 같이 생각해야한다.

통과된 코드

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <iostream>
#include <cmath>
using namespace std;
bool arr[10];
string str, strTemp;
int N, M, temp, cnt, target, result = INT32_MAX;
void dfs(string st, char ch)
{
if (st.length() == str.length() + 1) return;
st.push_back(ch);
cnt = abs(target - atoi(st.c_str())) + st.length();
result = min(cnt, result);
for (int i = 0; i < 10; i++) {
if (arr[i] == true) continue;
dfs(st, i + '0');
}
}
int main()
{
cin >> str >> M;
target = atoi(str.c_str());
for (int i = 0; i < M; i++) {
cin >> temp;
arr[temp] = true;
}
for (int i = 0; i < 10; i++) {
if (arr[i] == true) continue;
dfs(strTemp, i + '0');
}
result = min(abs(target - 100), result);
cout << result;
return 0;
}
#include <iostream> #include <cmath> using namespace std; bool arr[10]; string str, strTemp; int N, M, temp, cnt, target, result = INT32_MAX; void dfs(string st, char ch) { if (st.length() == str.length() + 1) return; st.push_back(ch); cnt = abs(target - atoi(st.c_str())) + st.length(); result = min(cnt, result); for (int i = 0; i < 10; i++) { if (arr[i] == true) continue; dfs(st, i + '0'); } } int main() { cin >> str >> M; target = atoi(str.c_str()); for (int i = 0; i < M; i++) { cin >> temp; arr[temp] = true; } for (int i = 0; i < 10; i++) { if (arr[i] == true) continue; dfs(strTemp, i + '0'); } result = min(abs(target - 100), result); cout << result; return 0; }
#include <iostream>
#include <cmath>

using namespace std;

bool arr[10];

string str, strTemp;

int N, M, temp, cnt, target, result = INT32_MAX;

void dfs(string st, char ch)
{
	if (st.length() == str.length() + 1) return;
	st.push_back(ch);
	cnt = abs(target - atoi(st.c_str())) + st.length();
	result = min(cnt, result);

	for (int i = 0; i < 10; i++) {
		if (arr[i] == true) continue;
		dfs(st, i + '0');
	}
}

int main()
{
	cin >> str >> M;

	target = atoi(str.c_str());

	for (int i = 0; i < M; i++) {
		cin >> temp;
		arr[temp] = true;
	}
	
	for (int i = 0; i < 10; i++) {
		if (arr[i] == true) continue;
		dfs(strTemp, i + '0');
	}

	result = min(abs(target - 100), result);

	cout << result;

	return 0;
}

문자열로 접근하니까 속도가 너무 느리다….

댓글 달기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다