Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 백준
- BFS
- JUnit 5
- 백트래킹
- 수학
- java
- 후니의 쉽게 쓴 시스코 네트워킹
- swea
- 프로그래머스
- 세그먼트 트리
- CS
- 유니온 파인드
- Network
- 동적계획법
- 플로이드-와샬
- Kotlin
- 에라토스테네스의 체
- 위상정렬
- dfs
- 문자열
- 스택
- 그리디
- Effective Java
- 완전탐색
- 시뮬레이션
- 이분탐색
- mst
- 구현
- 투 포인터
- 알고리즘
Archives
반갑습니다!
[백준] 1938 통나무 옮기기 본문
풀이
BFS를 통해서 해결할 수 있는 문제이다. 나무가 놓일 수 있는 방법이 2가지이므로 중복체크를 위한 visited
배열을 visited[50][50][2]
로 선언하여 방향에 따라 체크해주었다. 그리고 나무의 중심부를 저장하여 회전할 수 있는지, 이동시킬 수 있는지 확인하였다.
코드
#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
struct Tree {
int x, y, dir, cnt;
};
int n;
char map[50][51];
bool visited[50][50][2];
vector<pair<int, int>> tree, fin;
const int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
// 나무가 회전할 수 있는지 확인
bool can_rotate(int x, int y) {
if (x - 1 < 0 || x + 1 > n - 1 || y - 1 < 0 || y + 1 > n - 1) return false;
for (int i = y - 1; i <= y + 1; i++)
for (int j = x - 1; j <= x + 1; j++)
if (map[i][j] == '1') return false;
return true;
}
bool can_move(int x, int y, int dir, int go) {
// 나무가 가로로 놓여있을 때
if (dir == 0) {
// 좌우 방향으로 옮길 때
if (go == 0 || go == 2) {
int nx = x + 2 * dx[go];
if (nx < 0 || nx > n - 1 || map[y][nx] == '1') return false;
}
// 상하 방향으로 옮길 때
else {
for (int i = -1; i <= 1; i++) {
int nx = x + i;
int ny = y + dy[go];
if (nx < 0 || nx > n - 1 || ny < 0 || ny > n - 1) return false;
if (map[ny][nx] == '1') return false;
}
}
}
// 나무가 세로로 놓여있을 때
else {
// 상하 방향으로 옮길 때
if (go == 1 || go == 3) {
int ny = y + 2 * dy[go];
if (ny < 0 || ny > n - 1 || map[ny][x] == '1') return false;
}
// 좌우 방향으로 옮길 때
else {
for (int i = -1; i <= 1; i++) {
int nx = x + dx[go];
int ny = y + i;
if (nx < 0 || nx > n - 1 || ny < 0 || ny > n - 1) return false;
if (map[ny][nx] == '1') return false;
}
}
}
return true;
}
int bfs() {
Tree f;
f.x = fin[1].first; f.y = fin[1].second; f.cnt = 0;
// 목적지의 x값이 바뀌면 세로
if (fin[0].first != fin[1].first) f.dir = 0;
// 목적지의 x값이 모두 같으면 가로
else f.dir = 1;
Tree t;
t.x = tree[1].first; t.y = tree[1].second; t.cnt = 0;
// 나무의 x값이 바뀌면 세로
if (tree[0].first != tree[1].first) t.dir = 0;
// 나무의 x값이 바뀌면 세로
else t.dir = 1;
queue<Tree> q;
q.push(t);
visited[t.y][t.x][t.dir] = true;
while (!q.empty()) {
Tree cur = q.front();
if (cur.x == f.x && cur.y == f.y && cur.dir == f.dir) return cur.cnt;
q.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.x + dx[i];
int ny = cur.y + dy[i];
if (nx < 0 || nx > n - 1 || ny < 0 || ny > n - 1) continue;
if (!visited[ny][nx][cur.dir] && can_move(cur.x, cur.y, cur.dir, i)) {
visited[ny][nx][cur.dir] = true;
q.push({ nx, ny, cur.dir, cur.cnt + 1 });
}
}
if (!visited[cur.y][cur.x][!cur.dir] && can_rotate(cur.x, cur.y)) {
visited[cur.y][cur.x][!cur.dir] = true;
q.push({ cur.x, cur.y, !cur.dir, cur.cnt + 1 });
}
}
return 0;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {
cin >> map[i][j];
// 나무의 좌표 저장
if (map[i][j] == 'B') tree.push_back({ j, i });
// 목적지의 좌표 저장
else if (map[i][j] == 'E') fin.push_back({ j, i });
}
cout << bfs() << '\n';
return 0;
}
'알고리즘 문제 풀이' 카테고리의 다른 글
[백준] 5566 주사위 게임 (0) | 2020.04.24 |
---|---|
[백준] 1941 소문난 칠공주 (0) | 2020.04.24 |
[백준] 2146 다리 만들기 (0) | 2020.04.23 |
[백준] 10973 이전 순열 (0) | 2020.04.23 |
[백준] 14425 문자열 집합 (0) | 2020.04.23 |