[c++] BOJ 17244 :: ์๋ง๋ค์ฐ์ฐ
๋ฌธ์
์๋ง๋ค ์ฐ์ฐ ๋ฌธ์ ๋ฐ๋ก๊ฐ๊ธฐ
๊ฒฝ์ฌ์จ๋ ์ ๋ ์ฝ์์ ๊ฐ๊ธฐ ์ ์ฑ๊ธฐ์ง ์์ ๋ฌผ๊ฑด๋ค์ด ์๋ ์ง ํ์ธํ๊ณ ์๋ค. ํ์ํ ๋ฌผ๊ฑด์ ์ ๋ถ ์ฑ๊ธด ๊ฒ ๊ฐ์๊ณ ์ธ์ถ ํ ๋์์ค๋ ๊ธธ์ ๊ฒฝ์ฌ์จ๋ ์ธ์ณค๋ค.
"์ ๋ง๋ค ์ฐ์ฐ!!!"
๊ฒฝ์ฌ ์จ๋ ๋งค๋ฒ ์ธ์ถํ๊ณ ๋์์ผ ์ด๋ค ๋ฌผ๊ฑด์ ์ง์ ๋๊ณ ์๋ค๋ ๊ฒ์ ๋ ์ฌ๋ฆด ๋๋ง๋ค ์์ฑ ๊ฐ์ ์๋ฌ๋ฆฌ๋ ๊ฒ์ด ๋๋ฌด ์ซ์๋ค.
์ธ์ถ์ด ์ฆ์ ๊ฒฝ์ฌ ์จ๋ ๋ฐ๋ณต๋๋ ์ผ์ ๊ทผ์ ํ๊ธฐ ์ํด ๊ผญ ์ฑ๊ฒจ์ผ ํ ๋ฌผ๊ฑด๋ค์ ์ ๋ฆฌํด๋ณด์๋ค. ํ์ง๋ง ์ง๊ฐ, ์ค๋งํธํฐ, ์ฐ์ฐ, ์ฐจ ํค, ์ด์ดํฐ, ์๊ณ, ๋ณด์กฐ ๋ฐฐํฐ๋ฆฌ ๋ฑ ์ข ๋ฅ์ ๊ฐ์๊ฐ ๋๋ฌด ๋ง์๋ค.
ํ์ ๋ถํ์ํ ์์ง์์ ์์ฃผ ์ซ์ดํ๋ ๊ฒฝ์ฌ ์จ๋ ์ด ๋ฌผ๊ฑด๋ค์ ์ต๋ํ ๋น ๋ฅด๊ฒ ์ฑ๊ฒจ์ ์ธ์ถํ๋ ์ด๋ ๊ฒฝ๋ก๋ฅผ ์๊ณ ์ถ์๋ค.
๊ฒฝ์ฌ ์จ๋ ํ ๊ฑธ์์ ์ํ์ข์ฐ์ ์ธ์ ํ ์นธ์ผ๋ก๋ง ์์ง์ผ ์ ์๋ค.
๊ฒฝ์ฌ ์จ๋ฅผ ์ํด ์ง์ ์์์ ๋ฐ๋ผ๋ณธ ๋ชจ์ต๊ณผ ์ฑ๊ฒจ์ผ ํ ๋ฌผ๊ฑด๋ค์ ์์น๋ค์ ์๊ณ ์์ ๋, ๋ฌผ๊ฑด์ ๋ชจ๋ ์ฑ๊ฒจ์ ์ธ์ถํ๊ธฐ๊น์ง ์ต์ ๋ช ๊ฑธ์์ด ํ์ํ์ง ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์.
ํ์ด
- BFS ์ํ ๋ฌธ์
- ๋ฉ๋ชจ์ด์ ์ด์
์ ์ด๋ป๊ฒ ํ ๊ฒ์ธ๊ฐ๊ฐ ๊ด๊ฑด
- ์ต๋ 5๊ฐ์ ๋ฌผ๊ฑด์ด๋ bitmap ์ฌ์ฉ
์ฝ๋
#include <iostream>
#include <string>
#include <queue>
#include <unordered_set>
using namespace std;
int map[51][51];
int direction[4][2] = { {1, 0}, {0, -1}, {-1, 0}, {0, 1} };
int visitedPath[51][51];
unordered_set<int> visitedProduct[51][51];
int POW[5] = { 1, 10, 100, 1000, 10000 };
vector<pair<int, int>> productList;
struct Node {
int row;
int col;
int path;
int productList = 0;
Node(int r, int c, int p) {
row = r; col = c; path = p;
}
};
int main() {
// S์์ ๋ชจ๋ X๋ฅผ ๊ฑฐ์ณ E
fill(&visitedPath[0][0], &visitedPath[50][51], 1e9);
int N, M;
cin >> N >> M;
pair<int,int> start; pair<int, int> end;
for (int i = 0; i < M; i++) {
string str; cin >> str;
for (int j = 0; j < str.size(); j++) {
map[i][j] = str[j];
if (map[i][j] == 'S') {
start = make_pair(i, j);
}
else if (map[i][j] == 'E') {
end = make_pair(i, j);
}
else if (map[i][j] == 'X') {
productList.push_back(make_pair(i, j)); // X๋ฅผ 0~4๋ก ๋งคํ
}
}
}
int result = 0;
for (int i = 0; i < productList.size(); i++) {
// ๋ฏธ๋ฆฌ ์ ์ฅํด๋๊ธฐ (product ๋ค ์ฑ์ ์ ๋)
result += POW[i];
}
queue<Node*> que;
que.push(new Node(start.first, start.second, 0));
visitedPath[start.first][start.second] = 0;
while (!que.empty()) {
auto pos = que.front(); que.pop();
if (pos->row == end.first && pos->col == end.second) {
if (pos->productList == result) {
cout << pos->path;
return 0;
}
}
for (int i = 0; i < 4; i++) {
int r = pos->row + direction[i][0];
int c = pos->col + direction[i][1];
if (r < 0 || r >= M || c < 0 || c >= N) {
continue;
}
if (map[r][c] == '#') {
continue;
}
Node* tmp = new Node(r, c, pos->path+1);
tmp->productList = pos->productList;
if (map[r][c] == 'X') {
int idx = 0;
for (int i = 0; i < productList.size(); i++) {
if (r == productList[i].first && c == productList[i].second) {
idx = i;
break;
}
}
int haveProduct = (tmp->productList / POW[idx]) % 10;
if (haveProduct == 0) {
tmp->productList += POW[idx];
}
}
auto sameProductAgain = (visitedProduct[r][c].find(pos->productList) != visitedProduct[r][c].end());
if ((visitedPath[r][c] <= pos->path+1) && sameProductAgain) {
// ๋ ํฐ path๋ก ๋ค๋ฅด๋๋ฐ, ๋ค๋ฅธ product๊ฐ ๊ฐ์ผ๋ฉด ๊ฑด๋๋ฐ๊ธฐ
continue;
}
que.push(tmp);
visitedPath[r][c] = tmp->path;
visitedProduct[r][c].insert(tmp->productList);
}
}
}
๊ฐ์
- pow๋ ๋ฏธ๋ฆฌ ๊ณ์ฐํ ๋ค ์ฌ์ฉ
- ์ฝ๋ ๋ ๊ธธ์ด์ ๊ฐ๋จํ๊ฒ ํด์ผํจ
๊ฒฐ๊ณผ
์์ด๋ | ๋ฉ๋ชจ๋ฆฌ | ์๊ฐ | ์ธ์ด |
---|---|---|---|
gmldms784 | 11172 | 40 | C++17 / ์์ |