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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <queue>
 
using namespace std;
 
int main() {
    int m, n;
    scanf("%d %d"&m, &n);
 
    vector<vector<int>> tomato(n + 1vector<int>(m + 1));
    int totalTomato = 0//익어야하는 토마토의 갯수
    int currentTomato = 0;  //현재 익은 토마토의 갯수
    queue<pair<intint>> q;
 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            scanf("%d"&tomato[i][j]);
 
            if (tomato[i][j] != -1)
                ++totalTomato;  //토마토가 없는 곳 빼고는 다 익어야 함
            if (tomato[i][j] == 1) {
                ++currentTomato;
                q.push(make_pair(i, j));
            }
        }
    }
 
    int yesterdayTomato = currentTomato;  //yesterdayTomato는 전날에 익은 토마토의 갯수. q에서 yesterdayTomato개만큼 pop하면서 주변 보면 됨
    int ans = 0;
 
    while (true) {
        if (currentTomato == totalTomato)
            break;  //다 익었으면 loop 탈출
 
        ++ans;
        int temp = currentTomato;  //-1로 막혀있는지 여부 확인을 위해서. 나중에 currentTomato가 변하지 않았다면(temp)와 같다면 -1 출력하고 종료해야 함
        int todayTomato = 0;  //오늘 하루동안 익은 토마토의 갯수. 나중에 yesterdayTomato에 저장
 
        for (int yesterday = 0; yesterday < yesterdayTomato; yesterday++) {
            //어제 하루동안 익은 토마토갯수만큼 pop
            int row = q.front().first;
            int col = q.front().second;
            q.pop();
 
            if (row - 1 >= 1 && tomato[row - 1][col] == 0) {
                tomato[row - 1][col] = 1;  //위
                q.push(make_pair(row - 1, col));
                ++currentTomato;
                ++todayTomato;
            }
            if (row + 1 <= n && tomato[row + 1][col] == 0) {
                tomato[row + 1][col] = 1;  //아래
                q.push(make_pair(row + 1, col));
                ++currentTomato;
                ++todayTomato;
            }
            if (col - 1 >= 1 && tomato[row][col - 1== 0) {
                tomato[row][col - 1= 1;  //왼쪽
                q.push(make_pair(row, col - 1));
                ++currentTomato;
                ++todayTomato;
            }
            if (col + 1 <= m && tomato[row][col + 1== 0) {
                tomato[row][col + 1= 1;  //오른쪽
                q.push(make_pair(row, col + 1));
                ++currentTomato;
                ++todayTomato;
            }
        }
        yesterdayTomato = todayTomato;
        if (currentTomato == temp) {
            //변하지 않았으면 더이상 -1로 막혀서 더이상 익지 못하는것
            ans = -1;
            break;
        }
    }
 
    printf("%d", ans);
 
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

처음에는 익은 모든 토마토에 대해 주변을 익혔는데 시간초과가 떠서

전날 익은 토마토만 보면서 주변을 익힘

 

'Baekjoon' 카테고리의 다른 글

[#1753] 최단 경로  (0) 2020.04.01
[#1697] 숨바꼭질  (0) 2020.03.31
[#2178] 미로탐색  (0) 2020.03.31
[#1012] 유기농 배추  (0) 2020.03.31
[#2667] 단지번호붙이기  (0) 2020.03.31

+ Recent posts