๐๏ธ ๊ฒช์ ๋ฌธ์
- ํ ๋ฒ ์ง๋๊ฐ ๊ธธ์ ๋ค์ ์๊ฐ๋ ๋ฌธ์ ๊ฐ ์์๋ค. visited[๋ฐฉํฅ][x][y] ๋ฐฐ์ด ๋๋ฌธ์ด์๋ค.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
- ์ต๋จ๊ฑฐ๋ฆฌ๊ฐ ์๋๋ฏ๋ก, visited ๋ฐฐ์ด์ด ํ์๊ฐ ์๋ค. bfs๋ก ๋ชจ๋ ๋ฃจํธ๋ฅผ ๋๋ฉด์ ๋๋ฌํ๋ cnt๋ง ์ฌ๋ฉด ๋์๋ ๋ฌธ์ ๋ค.
๐ ์ฝ๋
#include <iostream>
#include <queue>
using namespace std;
int N, cnt = 0;
bool board[16][16];
enum DIR {
HOR,
VER,
DIAG
};
enum MAP {
EMPTY,
WALL
};
struct Pipe {
int x;
int y;
int dir;
};
bool checkEnd(Pipe p) {
if (p.dir == HOR && p.x == N-1 && p.y == N-2) return true;
else if (p.dir == VER && p.x == N-2 && p.y == N-1) return true;
else if (p.dir == DIAG && p.x == N-2 && p.y == N-2) return true;
return false;
}
void bfs() {
queue<Pipe> q;
q.push({0, 0, HOR});
while (!q.empty()) {
Pipe now = q.front();
q.pop();
if (checkEnd(now)) {
cnt++;
continue;
}
if (now.dir == HOR) { // ๊ฐ๋ก
// ๊ฐ๋ก ์ด๋
if (now.y+2 < N && board[now.x][now.y+2] == EMPTY) {
q.push({now.x, now.y+1, HOR});
}
// ๋๊ฐ ์ด๋
if (now.y+2 < N && now.x+1 < N &&
board[now.x][now.y+2] == EMPTY && board[now.x+1][now.y+1] == EMPTY &&
board[now.x+1][now.y+2] == EMPTY) {
q.push({now.x, now.y+1, DIAG});
}
}
else if (now.dir == VER) { // ์ธ๋ก
// ์ธ๋ก ์ด๋
if (now.x+2 < N && board[now.x+2][now.y] == EMPTY) {
q.push({now.x+1, now.y, VER});
}
// ๋๊ฐ ์ด๋
if (now.x+2 < N && now.y+1 < N &&
board[now.x+2][now.y] == EMPTY && board[now.x+1][now.y+1] == EMPTY &&
board[now.x+2][now.y+1] == EMPTY) {
q.push({now.x+1, now.y, DIAG});
}
}
else if (now.dir == DIAG){ // ๋๊ฐ์
// ๊ฐ๋ก ์ด๋
if (now.x+1 < N && now.y+2 < N &&
board[now.x+1][now.y+2] == EMPTY) {
q.push({now.x+1, now.y+1, HOR});
}
// ์ธ๋ก ์ด๋
if (now.x+2 < N && now.y+1 < N &&
board[now.x+2][now.y+1] == EMPTY) {
q.push({now.x+1, now.y+1, VER});
}
// ๋๊ฐ ์ด๋
if (now.x+2 < N && now.y+2 < N &&
board[now.x+1][now.y+2] == EMPTY && board[now.x+2][now.y+1] == EMPTY &&
board[now.x+2][now.y+2] == EMPTY) {
q.push({now.x+1, now.y+1, DIAG});
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N;
for (int i=0; i<N; i++) {
for (int j=0; j<N; j++) {
cin >> board[i][j];
}
}
bfs();
cout << cnt << '\n';
return 0;
}
'Algorithm > PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++] 9935: ๋ฌธ์์ด ํญ๋ฐ (0) | 2025.05.13 |
---|---|
[C++] 5639: ์ด์ง ๊ฒ์ ํธ๋ฆฌ (0) | 2025.04.30 |
[C++] 12865: ํ๋ฒํ ๋ฐฐ๋ญ (0) | 2025.04.15 |
[C++] 9251: LCS (0) | 2025.04.15 |
[C++] 2096: ๋ด๋ ค๊ฐ๊ธฐ (0) | 2025.04.11 |