티스토리 뷰
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 59 60 61 62 63 64 65 66 67 | #include <cstdio> #include <queue> #include <tuple> using namespace std; int arr[100][100][100]{ 0, }; int visited[100][100][100]{ 0, }; queue<tuple<int, int, int>> order; int bfs(int sizeH, int sizeN, int sizeM, int nu) { int max = 1; while (!order.empty()) { tuple<int, int, int> now = order.front(); order.pop(); nu++; max = arr[get<0>(now)][get<1>(now)][get<2>(now)]; if (get<0>(now) + 1 < sizeH && arr[get<0>(now) + 1][get<1>(now)][get<2>(now)] == 0) { order.push({ get<0>(now) + 1, get<1>(now), get<2>(now) }); arr[get<0>(now) + 1][get<1>(now)][get<2>(now)] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } if (get<0>(now) -1 >= 0 && arr[get<0>(now) - 1][get<1>(now)][get<2>(now)] == 0) { order.push({ get<0>(now) - 1, get<1>(now), get<2>(now) }); arr[get<0>(now) - 1][get<1>(now)][get<2>(now)] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } if (get<1>(now) + 1 < sizeN && arr[get<0>(now)][get<1>(now) + 1][get<2>(now)] == 0) { order.push({ get<0>(now), get<1>(now) + 1, get<2>(now) }); arr[get<0>(now)][get<1>(now) + 1][get<2>(now)] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } if (get<1>(now) - 1 >= 0 && arr[get<0>(now)][get<1>(now) - 1][get<2>(now)] == 0) { order.push({ get<0>(now), get<1>(now) - 1, get<2>(now) }); arr[get<0>(now)][get<1>(now) - 1][get<2>(now)] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } if (get<2>(now) + 1 < sizeM && arr[get<0>(now)][get<1>(now)][get<2>(now) + 1] == 0) { order.push({ get<0>(now), get<1>(now), get<2>(now) + 1 }); arr[get<0>(now)][get<1>(now)][get<2>(now) + 1] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } if (get<2>(now) - 1 >= 0 && arr[get<0>(now)][get<1>(now)][get<2>(now) - 1] == 0) { order.push({ get<0>(now), get<1>(now), get<2>(now) - 1 }); arr[get<0>(now)][get<1>(now)][get<2>(now) - 1] = arr[get<0>(now)][get<1>(now)][get<2>(now)] + 1; } } return sizeH * sizeN * sizeM == nu ? max - 1 : -1; } int main() { int M, N, H, nu = 0; scanf("%d %d %d", &M, &N, &H); for (int i = 0; i < H; i++) { for (int j = 0; j < N; j++) { for (int k = 0; k < M; k++) { scanf("%d", &arr[i][j][k]); switch (arr[i][j][k]) { case -1: nu++; break; case 1: order.push({ i,j,k }); break; } } } } printf("%d\n", bfs(H, N, M, nu)); } | cs |
'algorithm'''problem solve' 카테고리의 다른 글
[백준] 11047 - 동전 0(설명X) (0) | 2020.05.09 |
---|---|
[백준] 9205 - 맥주 마시면서 걸어가기(설명X) (0) | 2020.05.09 |
[백준] 2667 - 단지번호붙이기(설명X) (0) | 2020.05.09 |
[백준] 2178 - 미로 탐색(설명X) (0) | 2020.05.09 |
[백준] 1992 - 쿼드트리(설명X) (0) | 2020.05.01 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 컨트리뷰톤
- 오픈소스
- c++
- 플로이드 와셜
- 프로그래머스
- c
- DFS
- 코딩
- 카카오
- 백준
- 알고리즘
- BaekJoon
- PyPy3
- 구현
- 한화큐셀
- BFS
- DP
- Dynamic Programming
- 백트래킹
- 인공지능
- 1932
- 이분탐색
- 브루트포스
- LG
- 완전탐색
- 동적 계획법
- 정렬
- 피보나치
- webOS
- 파이썬
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함