반응형
https://www.acmicpc.net/problem/1261
1261번: 알고스팟
첫째 줄에 미로의 크기를 나타내는 가로 크기 M, 세로 크기 N (1 ≤ N, M ≤ 100)이 주어진다. 다음 N개의 줄에는 미로의 상태를 나타내는 숫자 0과 1이 주어진다. 0은 빈 방을 의미하고, 1은 벽을 의미
www.acmicpc.net
[ 문제풀이 ]
1. visited [ 100 ][ 100 ] 배열을 만들어서 방문 여부를 저장합니다.
2. 0-1 너비 우선 탐색을 통해 빈 에 들어가면 deque의 front에 push 하고, 벽을 부시면 deque의 back에 push 하고 cnt + 1을 해줍니다.
3. (N - 1,M - 1) 좌표에 도착하면 cnt를 출력합니다.
[ 소스코드 ]
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | #include<iostream> #include<queue> using namespace std; int N, M; char arr[101][101]; int dy[4] = { -1,1,0,0 }; int dx[4] = { 0,0,-1,1 }; int visited[100][100]; struct node { int y; int x; int cnt; }; int main() { deque<node> deq; scanf("%d %d", &M, &N); for (int i = 0; i < N; i++) { scanf("%s", &arr[i]); } deq.push_back({ 0,0,0 }); visited[0][0] = 1; while (!deq.empty()) { const int y = deq.front().y; const int x = deq.front().x; const int cnt = deq.front().cnt; deq.pop_front(); if (y == N - 1 && x == M - 1) { printf("%d", cnt); return 0; } for (int i = 0; i < 4; i++) { const int yy = y + dy[i]; const int xx = x + dx[i]; if (yy >= 0 && yy < N && xx >= 0 && xx < M) { if (visited[yy][xx] != 1) { visited[yy][xx] = 1; if (arr[yy][xx] == '0') { deq.push_front({ yy,xx,cnt }); } else { deq.push_back({ yy,xx,cnt + 1 }); } } } } } } | cs |
반응형
'백준' 카테고리의 다른 글
[ 백준 ] 17835번 - 면접보는 승범이네 (C++) (0) | 2023.02.05 |
---|---|
[ 백준 ] 1368번 - 물대기 (C++) (0) | 2023.02.04 |
[ 백준 ] 10423번 - 전기가 부족 (C++) (0) | 2023.02.02 |
[ 백준 ] 5214번 - 환승 (C++) (0) | 2023.02.01 |
[ 백준 ] 1584번 - 게임 (C++) (0) | 2023.01.31 |