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 |
Tags
- 데이터전달
- github
- 단축키
- broadcastreceiver
- 두 동전
- mysql
- Algorithm
- Jenknis
- 데이터
- 제어반전
- 17837
- vscode
- service
- activity
- ubuntu
- 프로그래머스
- git
- 큐빙
- 백준
- goland
- insert
- 알고리즘
- intent
- 16197
- IntelliJ
- 안드로이드
- data
- Java
- Android
- spring
Archives
- Today
- Total
해보자
백준_7576번_토마토 본문
https://www.acmicpc.net/problem/7576
7576번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토들의 정보가 주어진다. 즉, 둘째 줄부터 N개의 줄에는 상자에 담긴 토마토의 정보가 주어진다. 하나의 줄에는 상자 가로줄에 들어있는 토마토의 상태가 M개의 정수로 주어진다. 정수 1은 익은 토마토, 정수 0은 익지 않은 토마토, 정수 -1은 토마
www.acmicpc.net
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 | #include <iostream> #include <algorithm> #include <queue> #define MAX 1000 using namespace std; pair<int, int> dir[4] = { make_pair(0,1), make_pair(1,0), make_pair(0,-1), make_pair(-1,0) }; int N, M; int map[MAX][MAX]; int visited[MAX][MAX]; queue<pair<pair<int, int>, int>> good_tomatoes; bool isRange(int y, int x) { if (y < N && y >= 0 && x < M && x >= 0) { return true; } return false; } int bfs() { int maxDepth = 0; while (!good_tomatoes.empty()) { pair<pair<int, int>, int> cur = good_tomatoes.front(); int curY = cur.first.first; int curX = cur.first.second; int curDepth = cur.second; maxDepth = max(curDepth, maxDepth); good_tomatoes.pop(); for (int i = 0; i < 4; i++) { int nextY = curY + dir[i].first; int nextX = curX + dir[i].second; if (isRange(nextY, nextX)) { if (map[nextY][nextX] == 0){ map[nextY][nextX] = 1; good_tomatoes.push(make_pair(make_pair(nextY, nextX), curDepth + 1)); } } } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (map[i][j] == 0) return -1; } } return maxDepth; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> M >> N; for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> map[i][j]; if ( map[i][j] == 1) good_tomatoes.push(make_pair(make_pair(i,j), 0)); } } int result = bfs() ; cout << result; return 0; } | cs |
'C++ > Solve & Think' 카테고리의 다른 글
프로그래머스_LV2_다리를 지나는 트럭 (0) | 2020.02.26 |
---|---|
프로그래머스_LV2_기능개발 (0) | 2020.02.21 |
프로그래머스_LV2_프린터 (0) | 2020.02.19 |
백준_10610번_30 (0) | 2020.02.12 |
백준_1966번_프린터 큐 (0) | 2019.12.18 |