티스토리 뷰
2206번: 벽 부수고 이동하기
N×M의 행렬로 표현되는 맵이 있다. 맵에서 0은 이동할 수 있는 곳을 나타내고, 1은 이동할 수 없는 벽이 있는 곳을 나타낸다. 당신은 (1, 1)에서 (N, M)의 위치까지 이동하려 하는데, 이때 최단 경로
www.acmicpc.net
백준 소스코드 [C++] 2206 벽 부수고 이동하기
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#define all(v) v.begin(), v.end()
#define pii pair<int,int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
#define dot first
#define flag second
int n, m;
int arr[1002][1002];
int dist[1002][1002][2];
struct dots {
int y, x, chk,cost;
};
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };
void bfs() {
queue<dots> q;
q.push({ 1,1,0,1 });
while (q.empty() != true) {
dots n = q.front();
q.pop();
for (int i = 0;i < 4;i++) {
if (arr[n.y + dy[i]][n.x + dx[i]] == 1) {
if (n.chk == 0 && n.cost + 1 < dist[n.y + dy[i]][n.x + dx[i]][1]) {
dist[n.y + dy[i]][n.x + dx[i]][1] = n.cost + 1;
q.push({ n.y + dy[i],n.x + dx[i],1,n.cost + 1 });
}
}
else if (arr[n.y + dy[i]][n.x + dx[i]] == 0) {
if (n.cost + 1 < dist[n.y + dy[i]][n.x + dx[i]][n.chk]) {
dist[n.y + dy[i]][n.x + dx[i]][n.chk] = n.cost + 1;
q.push({ n.y + dy[i],n.x + dx[i],n.chk,n.cost + 1 });
}
}
}
}
if (min(dist[n][m][0], dist[n][m][1]) < INT_MAX) cout << min(dist[n][m][0], dist[n][m][1]);
else cout << "-1";
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
string s;
for (int i = 0;i <= n + 1;i++)
for (int j = 0;j <= m + 1;j++)
arr[i][j] = 2;
for (int i = 1;i <= n;i++)
for (int j = 1;j <= m;j++)
dist[i][j][0] = dist[i][j][1] = INT_MAX;
dist[1][1][0] = dist[1][1][1] = 1;
for (int i = 1;i <= n;i++) {
cin >> s;
for (int j = 1;j <= m;j++)
arr[i][j] = s[j - 1] - '0';
}
bfs();
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 16933 벽 부수고 이동하기 3 (0) | 2021.01.06 |
---|---|
백준 소스코드 [C++] 14442 벽 부수고 이동하기 2 (0) | 2021.01.06 |
백준 소스코드 [C++] 1620 나는야 포켓몬 마스터 이다솜 (0) | 2021.01.05 |
백준 소스코드 [C++] 11723 집합 (0) | 2021.01.05 |
백준 소스코드 [C++] 2096 내려가기 (0) | 2021.01.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- 벨만포드 알고리즘
- 벨만포드 시간복잡도
- WWDC16
- Testable
- CPU와 Memory
- observeOn
- 다익스트라 시간복잡도
- State Restoration
- CompositionalLayout
- 코딩대회
- 에드몬드 카프 알고리즘
- 부스트캠프 6기
- WWDC19
- 강한 순환 참조
- 최대 매칭
- MeTal
- 최단경로문제
- IOS
- 포드 풀커슨 알고리즘
- 컴퓨터 추상화
- HIG
- WWDC17
- 네트워크 유량
- test coverage
- WWDC21
- 네트워크 플로우
- 최단경로 알고리즘
- 최단경로 문제
- rxswift
- mach-o
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함