코딩 공부/백준

[백준][C++] 20057 마법사 상어와 토네이도

김 정 환 2021. 5. 25. 19:32
반응형

https://www.acmicpc.net/problem/20057

 

20057번: 마법사 상어와 토네이도

마법사 상어가 토네이도를 배웠고, 오늘은 토네이도를 크기가 N×N인 격자로 나누어진 모래밭에서 연습하려고 한다. 위치 (r, c)는 격자의 r행 c열을 의미하고, A[r][c]는 (r, c)에 있는 모래의 양을

www.acmicpc.net

 

 

알고리즘 종류

구현

 

사고 과정

 

 

구현(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= {
                    {000.0200},
                    {00.10.070.010}, 
                    {0.051.0000}, 
                    {00.10.070.010}, 
                    {000.0200}};
 
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 == 0continue;
            
            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

 

 

시행착오

 

반응형