https://www.acmicpc.net/problem/16235
16235번: 나무 재테크
부동산 투자로 억대의 돈을 번 상도는 최근 N×N 크기의 땅을 구매했다. 상도는 손쉬운 땅 관리를 위해 땅을 1×1 크기의 칸으로 나누어 놓았다. 각각의 칸은 (r, c)로 나타내며, r은 가장 위에서부터
www.acmicpc.net
실패 한 이유
처음에는 info 라는 구조체를 만들어서 했다.
비효율적이다.
vector<int> map[15][15];
로 해서
vector<int> tmp;
sort(map[i][j].begin(), map[i][j].end());
map[i][j].clear();
for (int k = 0; k < tmp.size(); k++)
{
map[i][j].push_back(tmp[k]);
}
1. 사계를 fourseason으로 묶어서 했었다.
2. memset(nut, 5, sizeof(nut)) 얘가 문제였다.
memset에는 0(false), 1(true), 0x3f 만 들어갈 수 있다.
<소스 코드>
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#include <iostream>
#include <vector>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int dx[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dy[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
// 하나의 칸에 여러개의 나무
vector<int> map[15][15];
int nut[15][15];
int input[15][15];
int n, m, k;
void breed(int row, int col)
{
for (int k = 0; k < 8; k++)
{
int nx = row + dx[k];
int ny = col + dy[k];
if (nx >= 1 && nx <= n && ny >= 1 && ny <= n)
{
map[nx][ny].push_back(1);
}
}
}
void harunatsu()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (map[i][j].size() == 0)
continue;
else
{
int die_yang = 0;
// 산 나무들을 tmp에 저장
vector<int> tmp;
// 나이 오름차순 정렬
sort(map[i][j].begin(), map[i][j].end());
for (int a = 0; a < map[i][j].size(); a++)
{
int age = map[i][j][a];
if (age <= nut[i][j])
{
nut[i][j] -= map[i][j][a];
tmp.push_back(age + 1);
}
// 양분이 된다
else
die_yang += (map[i][j][a] / 2);
}
map[i][j].clear();
for (int k = 0; k < tmp.size(); k++)
{
map[i][j].push_back(tmp[k]);
}
nut[i][j] += die_yang;
}
}
}
}
void aki()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (map[i][j].size() == 0)
continue;
else
{
for (int a = 0; a < map[i][j].size(); a++)
{
// 나이가 5의 배수라면
if (map[i][j][a] % 5 == 0)
breed(i, j);
}
}
}
}
}
void fuyu()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
nut[i][j] += input[i][j];
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m >> k;
// 얘가 문제였음
// memset에는 5를 넣을수 없거든!
// memset(nut, 5, sizeof(nut));
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cin >> input[i][j];
nut[i][j] = 5;
}
}
for (int i = 0; i < m; i++)
{
int x, y, toshi;
cin >> x >> y >> toshi;
map[x][y].push_back(toshi);
}
for (int i = 0; i < k; i++)
{
harunatsu();
aki();
fuyu();
}
int ans = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
ans += map[i][j].size();
}
}
cout << ans << '\n';
return 0;
}
|
cs |
'시뮬레이션과 구현' 카테고리의 다른 글
마법사 상어와 토네이도 (0) | 2023.10.03 |
---|---|
주사위 굴리기 2 (0) | 2023.10.01 |
미세먼지 안녕! (0) | 2023.09.27 |
이차원 배열과 연산 (0) | 2023.09.26 |
게리맨더링 2 (0) | 2023.09.25 |