해당 문제는 그냥 시뮬레이션 구현 문제이다.
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef struct {
int yangbun;
vector<int> trees;
}Map;
int n,m,k;
int dx[] = {-1, -1, 0 ,1, 1,1, 0,-1};
int dy[] = {0,-1,-1,-1,0,1,1,1};
//좌 상 우 하
int A[11][11];
Map G[11][11];
void initVar(void){
cin >> n >> m >> k;
for(int i = 1; i<=n ;i++)
{
for(int j = 1; j<= n; j++)
{
cin >> A[i][j];
G[i][j].yangbun = 5;
}
}
for(int i = 0; i<m;i++)
{
int row, col,age;
cin >> row >> col >> age;
G[row][col].trees.push_back(age);
}
}
void goSpringandSummer(void){
// printf("gospring!!\n");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
int deadTree = 0;
int popCnt = 0;
if(G[i][j].trees.size()!=0)
{
sort(G[i][j].trees.begin(), G[i][j].trees.end());
for(int t = 0; t<G[i][j].trees.size();t++)
{
int tree_age = G[i][j].trees[t];
if(G[i][j].yangbun>=tree_age)
{
G[i][j].yangbun -= tree_age;
G[i][j].trees[t] += 1;
}
else{
deadTree += tree_age /2;
popCnt += 1;
}
}
}
for(int pop = 0; pop<popCnt;pop++)
G[i][j].trees.pop_back();
G[i][j].yangbun += deadTree;
}
}
}
void goAutumn(void)
{
// printf("goAutum!!\n");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
// printf("%d %d %d\n", i, j, G[i][j].trees.size());
if (G[i][j].trees.size() != 0)
{
for (int t = 0; t < G[i][j].trees.size(); t++)
{
int tree_age = G[i][j].trees[t];
if(tree_age%5 == 0)
{
for(int mov = 0; mov <8;mov++){
int row = i + dy[mov];
int col = j + dx[mov];
if(row >n || row <=0 || col > n || col <=0) continue;
G[row][col].trees.push_back(1);
}
}
}
}
}
}
}
void goWinter(void){
// printf("winter!!\n");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
G[i][j].yangbun += A[i][j];
}
}
}
int getTreesNum(void)
{
int answer = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
answer += G[i][j].trees.size();
}
}
return answer;
}
int main()
{
initVar();
for(int i= 0; i<k;i++)
{
goSpringandSummer();
goAutumn();
goWinter();
}
int answer = getTreesNum();
printf("%d\n", answer);
}
'코테 문풀 > [Algorithm]백준(BOJ) 풀이' 카테고리의 다른 글
[Algorithm] BOJ 1260 - DFS와 BFS (C++) (0) | 2023.08.24 |
---|---|
[BOJ] 12100 - 2048(EASY) (0) | 2023.04.08 |
[Algorithm] BOJ 11053번 (0) | 2023.02.04 |