반응형
https://www.acmicpc.net/problem/20057
알고리즘 종류
구현
사고 과정
구현(C++)
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
|
#include <iostream>
#include <cmath>
using namespace std;
int n;
int ans=0;
int mat[500][500];
double filter[5][5] = {
{0, 0, 0.02, 0, 0},
{0, 0.1, 0.07, 0.01, 0},
{0.05, 1.0, 0, 0, 0},
{0, 0.1, 0.07, 0.01, 0},
{0, 0, 0.02, 0, 0}};
int dir[4][2] = {{1,0}, {0,1}, {-1,0}, {0,-1}};
bool isValid(int y, int x){
return (y>=0 && y<n) && (x>=0 && x<n);
}
void scattering(int y, int x){
int amount = mat[y][x];
mat[y][x] = 0;
for(int i=-2; i<=2; i++){
for(int j=-2; j<=2; j++){
double percent = filter[i+2][j+2];
if(percent == 0) continue;
int left;
if(percent == 1.0){
left = amount - floor(amount*0.02)*2
- floor(amount*0.1)*2
- floor(amount*0.07)*2
- floor(amount*0.01)*2
- floor(amount*0.05);
}
else{
left = floor(amount * percent);
}
int ny = y+i;
int nx = x+j;
if(!isValid(ny, nx)){
ans += left;
}
else{
mat[ny][nx] += left;
}
}
}
}
void change_filter(){
double tmp[5][5];
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
tmp[4-j][i] = filter[i][j];
}
}
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
filter[i][j] = tmp[i][j];
}
}
}
void solution(){
int y = n/2;
int x = n/2;
int cnt=1;
int d=3;
while(true){
for(int i=0; i<2; i++){
for(int j=0; j<cnt; j++){
y = y + dir[d][0];
x = x + dir[d][1];
scattering(y, x);
if(y==0 && x==0){
cout << ans << endl;
return;
}
}
if(d==3) d=0;
else if(d==0) d=1;
else if(d==1) d=2;
else if(d==2) d=3;
change_filter();
}
cnt++;
}
}
int main(void){
cin >> n;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
cin >> mat[i][j];
}
}
solution();
}
|
cs |
시행착오
반응형
'코딩 공부 > 백준' 카테고리의 다른 글
[백준][Java] 16197 두 동전 (0) | 2021.05.27 |
---|---|
[백준][C++ ] 20058 마법사 상어와 파이어스톰 (0) | 2021.05.26 |
[백준][C++] 20056 마법사 상어와 파이어볼 (0) | 2021.05.25 |
[백준][C++] 1107 리모컨 (0) | 2021.04.22 |
[백준][C++] 1976 여행 가자 (0) | 2021.04.22 |