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
- 스프링 입문
- 10026번
- OS
- BOJ
- 우아한레디스
- C#
- 제프리리처
- Operating System.
- 2475번
- FIFO paging
- 운영체제
- 윤성우 저자
- 이펙티브코틀린
- 우아한 테크 세미나
- C++
- HTTP
- 김영한
- 토마토
- redis
- 열혈 tcp/ip 프로그래밍
- Spring
- Four Squares
- inflearn
- n타일링2
- 열혈 TCP/IP 소켓 프로그래밍
- 에러핸들링
- TCP/IP
- 스프링 핵심 원리
- Window-Via-c/c++
- Operating System
Archives
- Today
- Total
나의 브을로오그으
[c++] 7576번 : 토마토 본문
https://www.acmicpc.net/problem/7576
7576번: 토마토
첫 줄에는 상자의 크기를 나타내는 두 정수 M,N이 주어진다. M은 상자의 가로 칸의 수, N은 상자의 세로 칸의 수를 나타낸다. 단, 2 ≤ M,N ≤ 1,000 이다. 둘째 줄부터는 하나의 상자에 저장된 토마토
www.acmicpc.net
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
#define RIPE 1
#define NOT_RIPE 0
#define NOTHING -1
struct Point
{
int m;
int n;
};
void RipeTomato(vector<vector<int>>& tomatoBox, queue<Point>& ripeQ, int M, int N)
{
Point pt = ripeQ.front();
for (int i = 0; i < 2; ++i)
{
for (int j = -1; j < 2; j += 2)
{
int m = pt.m + (i == 0 ? j : 0);
int n = pt.n + (i == 1 ? j : 0);
if (m < 0 || n < 0 || m >= M || n >= N)
{
continue;
}
else
{
if (tomatoBox[n][m] == NOT_RIPE)
{
tomatoBox[n][m] = RIPE;
ripeQ.push(Point{ m, n });
}
}
}
}
return;
}
bool IsRipeAll(const vector<vector<int>>& tomatoBox, int M, int N)
{
for (int n = 0; n < N; ++n)
{
for (int m = 0; m < M; ++m)
{
if (tomatoBox[n][m] == NOT_RIPE)
{
return false;
}
}
}
return true;
}
int Progress(vector<vector<int>>& tomatoBox, int M, int N)
{
int days = 0;
queue<Point>ripeQ;
for (int n = 0; n < N; ++n)
{
for (int m = 0; m < M; ++m)
{
if (tomatoBox[n][m] == RIPE)
{
ripeQ.push(Point{ m, n });
}
}
}
while(true)
{
int len = ripeQ.size();
for (int i = 0; i < len; ++i)
{
RipeTomato(tomatoBox, ripeQ, M, N);
ripeQ.pop();
}
if (ripeQ.empty() == true)
{
break;
}
else
{
++days;
}
}
if (IsRipeAll(tomatoBox, M, N) == false)
{
days = -1;
}
return days;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
int input, M, N, days;
cin >> M >> N;
vector<vector<int>> tomatoBox(N, vector<int>());
for (int i = 0; i < N; ++i)
{
tomatoBox[i].reserve(M);
for (int j = 0; j < M; ++j)
{
cin >> input;
tomatoBox[i].push_back(input);
}
}
days = Progress(tomatoBox, M, N);
cout << days << '\n';
return 0;
}
'알고리즘 > BaekJoon' 카테고리의 다른 글
[c++] 9019번 : DSLR (0) | 2022.10.27 |
---|---|
[c++] 7662번 : 이중 우선순위 큐 (0) | 2022.10.26 |
[c++] 7569번 : 토마토 (0) | 2022.10.19 |
[c++] 18870번 : 좌표 압축 (0) | 2022.10.12 |
[c++] 17626번 : Four Squares (0) | 2022.10.04 |