Notice
Recent Posts
Recent Comments
Link
Tags
- ์น๋ฆฐ์ด
- DFS
- LCs
- ์ํฐ๋
- ์ฌ๊ท
- golang
- ์๊ณ ๋ฆฌ์ฆ
- ํธ๋ฆฌ
- BFS
- DP
- go
- ํ๋ฆฌ์จ๋ณด๋ฉ
- C++
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋นํธ๋ง์คํน
- ๋นํธ๋งต
- ์ฌ๋ผ์ด๋ฉ ์๋์ฐ
- nestjs
- ์นด์นด์ค2021
- ๋ฐฑ์ค
- js
- ์ด๋ถํ์
- ๊ฐ์ฅ๊ฐ๊น์ด๊ณตํต์กฐ์
- ๋์ ํ๋ก๊ทธ๋๋ฐ
- Union-Find
- ์์ฝ๋
- ๋ค์ต์คํธ๋ผ
- ์นด์นด์ค ์ฝํ
- Python
- ๋ฐฑ์๋ ํ๋ฆฌ์จ๋ณด๋ฉ
Archives
- Today
- Total
Hello Ocean! ๐ผ
[C++/๋ฐฑ์ค] 17244. ์๋ง๋ค์ฐ์ฐ ๋ณธ๋ฌธ
๋ฐ์ํ
๋ฌธ์
https://www.acmicpc.net/problem/17244
2์ฐจ์ ๋งต์์, ์ถ๋ฐ์์ ๋์ฐฉ์ ์ผ๋ก ๊ฐ๋๋ฐ, X๋ก ํ์๋ ํ์ํ ๋ฌผ๊ฑด๋ค์ ๋ชจ๋ ์ฑ๊ฒจ์ ๋๊ฐ๋ ์ต์ ์ด๋๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๋ค.
ํ์ด
๊ทธ๋ํ์์์ ์ต์ ์ด๋ ํ์๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ์ด๊ธฐ ๋๋ฌธ์ BFS๋ฅผ ์ด์ฉํ๋ค.
๊ทธ๋ฆฌ๊ณ , ๊ทธ๋ํ์์ X๊ฐ ์๋ ์ง์ ์ ๋ชจ๋ ๋ฐฉ๋ฌธํ๋๊ฒ ์กฐ๊ฑด์ด๊ธฐ ๋๋ฌธ์ X๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ์ด๋ป๊ฒ ํ ์ง ๊ณ ๋ฏผํ๋ค๊ฐ, X๊ฐ ์ต๋ 5๊ฐ๋ผ๊ณ ํด์ ๊ธธ์ด 5์ง๋ฆฌ ๋นํธ๋ง์คํฌ๋ฅผ ์ด์ฉํด์ ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํด์ฃผ์๋ค.
๊ฐ X๋ค์ ๊ตฌ๋ถํ๊ธฐ ์ํด, ์ ๋ ฅ๋ฐ์ ๋ X๋ฅผ ๊ฐ๊ธฐ ๋ค๋ฅธ ์ซ์(0~5)๋ก ๋ฐ๊ฟ์ฃผ์๋ค.
์ฝ๋
#include <iostream>
#include <queue>
#define sc scanf_s
using namespace std;
int N, M;
char table[51][51];
bool visited[51][51][1 << 5];
int dr[4] = { -1,1,0,0 };
int dc[4] = { 0,0,-1,1 };
int x = 0; //X๊ฐ์
struct Node {
int r; int c; int bitmap; int count;
};
int BFS(int startR, int startC) {
queue<Node> q;
q.push({ startR, startC, 0, 0 });
while (!q.empty()) {
int r = q.front().r;
int c = q.front().c;
int bitmap = q.front().bitmap;
int count = q.front().count;
q.pop();
for (int i = 0;i < 4;i++) {
int newR = r + dr[i]; int newC = c + dc[i];
if (newR < 0 || newR >= M || newC < 0 || newC >= N) continue;
if (table[newR][newC] == '#') continue;
else if (table[newR][newC] == 'E') {
if (bitmap == (1 << x)-1) {
return count + 1;
}
if (!visited[newR][newC][bitmap]) {
q.push({ newR, newC, bitmap, count + 1 });
visited[newR][newC][bitmap] = 1;
}
}
else if (table[newR][newC] == '.') { // ํ๋ฒ
if (!visited[newR][newC][bitmap]) {
q.push({ newR, newC, bitmap, count + 1 });
visited[newR][newC][bitmap] = 1;
}
}
else { // X
int temp = table[newR][newC] - '0';
int newBitmap = bitmap | (1 << temp);
if (!visited[newR][newC][newBitmap]) {
q.push({ newR,newC,newBitmap,count + 1 });
visited[newR][newC][newBitmap] = 1;
}
}
}
}
return -1;
}
int main() {
sc("%d %d", &N, &M); // N : col, M : row
int startR, startC;
for (int m = 0;m < M;m++) {
for (int n = 0;n < N;n++) {
sc("%c", &table[m][n]); // %c ์์ ๊ณต๋ฐฑ ๋ฃ์ด์ฃผ๊ธฐ ๋ณํ๋ค์ฏ๊ฐ
if (table[m][n] == 'X') {
//table[m][n] = x + '0';
x++;
}
else if (table[m][n] == 'S') {
startR = m;
startC = n;
table[m][n] = '.';
}
}
}
int result = BFS(startR, startC);
printf("%d\n", result);
return 0;
}
๊ฒฐ๊ณผ
๋ฐ์ํ
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค/C++] 1062. ๊ฐ๋ฅด์นจ (0) | 2021.08.10 |
---|---|
[๋ฐฑ์ค/C++] 18119. ๋จ์ด ์๊ธฐ (0) | 2021.08.10 |
[๋ฐฑ์ค/C++] 13701. ์ค๋ณต ์ ๊ฑฐ (0) | 2021.08.03 |
[C/C++] scanf, scanf_s๋ก ์ ๋ ฅ๋ฐ์ ๋ "\n" ์ด ์์ฌ ๋ค์ด๊ฐ๋ ๋ฌธ์ (0) | 2021.07.30 |
[๋ฐฑ์ค/C++] 11437, 11438. LCA 1,2 (0) | 2021.07.27 |