Notice
Recent Posts
Recent Comments
Link
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- acm
- 분리집합
- DFS
- 유니온파인드
- 너비우선탐색
- 종만북
- Algospot
- 세그먼트트리
- 알고리즘문제해결전략
- 다이나믹프로그래밍
- priority_queue
- 알고스팟
- 동적계획법
- 분할정복
- BOJ
- 문자열
- 백준
- backtracking
- union-find
- Greedy
- 완전탐색
- DP
- 이분탐색
- 스택
- stack
- 그리디
- 백트래킹
- 재귀
- 누적합
- BFS
Archives
- Today
- Total
DAMPER's 낙서장
13140. Hello World! 본문
728x90
https://www.acmicpc.net/problem/13140
문제 설명
복면산 문제와 같은 문제이다.
hello + world = N을 만족하는 d, e, h, l, o, r, w 7글자에 서로다른 숫자를 대입해서 N이 나오는가를 묻는 문제이다.
재귀 완전탐색으로 7글자에 대한 수를 0부터 9까지 넣는 과정을 진행했는데, w와 h에는 0이 들어갈 수 없으므로 이 부분을 처리해주면 된다.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include <bits/stdc++.h>
using namespace std;
#define swap(a,b) (a)^=(b)^=(a)^=(b)
#define endl '\n'
#define int long long
bool flag = false;
int n;
void printAns(int hello, int world)
{
cout << " " << hello << endl;
cout <<"+ " << world << endl;
cout << "-------\n";
if(hello + world < 99999) cout << " ";
cout << " " << hello + world;
}
void f(int pos, vector<int>& v, vector<bool>& check)
{
//if(v[1]*10000 + v[0]*10000 > n) return;
if(pos == v.size())
{
int hello = v[1]*10000 + v[2]*1000 + v[3]*100 + v[3]*10 + v[4];
int world = v[0]*10000 + v[4]*1000 + v[5]*100 + v[3]*10 + v[6];
if(hello + world == n)
{
flag = true;
printAns(hello, world);
}
return;
}
for(int i=0;i<10;i++)
{
if(check[i]) continue;
if(i == 0 && (pos == 1 || pos == 0)) continue;
check[i] = true;
v[pos] = i;
f(pos+1, v, check);
check[i] = false;
if(flag) return;
}
}
int32_t main()
{
cin.sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vector<int> v(7, 0);
vector<bool> check(10, false);
f(0, v, check);
if(flag == false) cout << "No Answer";
return 0;
}
|
cs |
728x90
'Problem Solving > BOJ 문제풀이' 카테고리의 다른 글
1509. 팰린드롬 분할 (0) | 2022.07.04 |
---|---|
1007. 벡터 매칭 (0) | 2022.07.04 |
2162. 선분 그룹 (0) | 2022.01.09 |
16724. 피리 부는 사나이 (0) | 2022.01.08 |
9527. 1의 개수 세기 (0) | 2022.01.05 |