※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 24622번 문제인 Blocks이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/24622
24622번: Blocks
In this example, Bessie can spell COW, ZOO, and MOVE. Sadly, she cannot spell MOO, since the only block with an M cannot also be used for an O. She cannot spell FARM since there is no block with a letter R. She cannot spell CODE since the C, D, and E all b
www.acmicpc.net
주어지는 네 주사위로 만들 수 있는 모든 단어를 저장한 set을 만든 뒤, 각 단어가 set에 들어있는지를 확인하는 것으로 문제를 해결하자.
생성할 수 있는 모든 단어의 set은 (1) 주사위를 먼저 나열한 뒤 (2) 각 주사위에서 사용할 글자를 고르는 것을 반복하는 것으로 얻을 수 있다. (1)은 algorithm 헤더의 next_permutation을, (2)는 반복문을 이용해 간단하게 구현 가능하다.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
int Q;
vector<string> block;
set<string> words;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> Q;
for (int i = 0; i < 4; i++) {
string s; cin >> s;
block.emplace_back(s);
}
for (int k = 0; k < 24; k++) {
next_permutation(block.begin(), block.end());
string tmp = "";
for (int a = 0; a < 6; a++) {
tmp += block[0][a];
words.insert(tmp);
for (int b = 0; b < 6; b++) {
tmp += block[1][b];
words.insert(tmp);
for (int c = 0; c < 6; c++) {
tmp += block[2][c];
words.insert(tmp);
for (int d = 0; d < 6; d++) {
tmp += block[3][d];
words.insert(tmp);
tmp.pop_back();
}
tmp.pop_back();
}
tmp.pop_back();
}
tmp.pop_back();
}
}
while (Q--) {
string s; cin >> s;
if (words.count(s)) cout << "YES\n";
else cout << "NO\n";
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 13939 // C++] Imena (0) | 2023.02.04 |
---|---|
[BOJ 9776 // C++] Max Volume (0) | 2023.02.03 |
[BOJ 22937 // C++] 교수님 계산기가 고장났어요! (0) | 2023.02.03 |
[BOJ 27297 // C++] 맨해튼에서의 모임 (0) | 2023.02.02 |
[BOJ 9780 // C++] Range Sum Query (0) | 2023.02.02 |