본문 바로가기
  • 소소한 개발자 이야기
Algorithm Study/BOJ with C++

(BAEKJOON) 17143번: 낚시왕

by Siwan_Min 2020. 7. 8.
728x90

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

 

17143번: 낚시왕

낚시왕이 상어 낚시를 하는 곳은 크기가 R×C인 격자판으로 나타낼 수 있다. 격자판의 각 칸은 (r, c)로 나타낼 수 있다. r은 행, c는 열이고, (R, C)는 아래 그림에서 가장 오른쪽 아래에 있는 칸이다.

www.acmicpc.net

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
#include <iostream>
 
//using namespace std;
 
struct info {
    int s; //속력
    int d; //이동 방향
    int z; //크기
};
 
 
int r, c, m;
info map[2][105][105];
/* 
1 상 
2 하 
3 우 
4 좌 
*/
 
int fish(int cur, int idx) {
    int size = 0;
    for (int i = 0; i < r; i++) {
        if (map[cur][i][idx].z) {
            size += map[cur][i][idx].z;
            map[cur][i][idx].z = 0;
            map[cur][i][idx].s = 0;
            map[cur][i][idx].d = 0;
            break;
        }
    }
    return size;
}
void move(int cur) {
    int next = (cur + 1) % 2;
 
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            map[next][i][j].s = 0;
            map[next][i][j].d = 0;
            map[next][i][j].z = 0;
        }
    }
    
    for (int i = 0; i < r; i++) {
        for (int j = 0; j < c; j++) {
            if (map[cur][i][j].z != 0) {
                // (변의 길이 -1) * 2 가 한 사이클 
                if (map[cur][i][j].d == 1){
 
                    int yy = (((r - 1* 2- i) + map[cur][i][j].s;
                    yy = (yy % ((r - 1* 2));
                    int nd = 2// 방향은 반대 
                    if (yy >= (r - 1)) {
                        yy = ((r - 1* 2- yy;
                        nd = 1;
                    }
                    if(map[next][yy][j].z < map[cur][i][j].z){
                        map[next][yy][j].z = map[cur][i][j].z;
                        map[next][yy][j].s = map[cur][i][j].s;
                        map[next][yy][j].d = nd;
                    }
                    
                }
                else if (map[cur][i][j].d == 2) {
                    int yy = i + map[cur][i][j].s;
                    yy = (yy % ((r - 1* 2));
                    int nd = 2// 방향은 반대 
                    if (yy >= (r - 1)) {
                        yy = ((r - 1* 2- yy;
                        nd = 1;
                    }
                    if(map[next][yy][j].z < map[cur][i][j].z){
                        map[next][yy][j].z = map[cur][i][j].z;
                        map[next][yy][j].s = map[cur][i][j].s;
                        map[next][yy][j].d = nd;
                    }
                }
                else if (map[cur][i][j].d == 3) {
                    int xx = j + map[cur][i][j].s;
                    xx = (xx % ((c - 1* 2));
                    int nd = 3// 방향은 반대 
                    if (xx >= (c - 1)) {
                        xx = ((c - 1* 2- xx;
                        nd = 4;
                    }
                    if(map[next][i][xx].z < map[cur][i][j].z){
                        map[next][i][xx].z = map[cur][i][j].z;
                        map[next][i][xx].s = map[cur][i][j].s;
                        map[next][i][xx].d = nd;
                    }
                }
                else if (map[cur][i][j].d == 4) {
                    int xx = (((c - 1* 2- j) + map[cur][i][j].s;
                    xx = (xx % ((c - 1* 2));
                    int nd = 3// 방향은 반대 
                    if (xx >= (c - 1)) {
                        xx = ((c - 1* 2- xx;
                        nd = 4;
                    }
                    if(map[next][i][xx].z < map[cur][i][j].z){
                        map[next][i][xx].z = map[cur][i][j].z;
                        map[next][i][xx].s = map[cur][i][j].s;
                        map[next][i][xx].d = nd;
                    }
                }
            }
        }
    }
}
 
int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
      std::cout.tie(NULL);
    int cur = 0;
    std::cin >> r >> c >> m;
 
    for (int i = 0; i < m; i++) {
        int y, x , s, d, z;
        std::cin >> y >> x >>s>>d>>z;
        y--, x--;
        map[0][y][x].s = s;
        map[0][y][x].d = d;
        map[0][y][x].z = z;
    }
 //   cout<<endl;
    int result = 0;
    for (int i = 0; i < c; i++) {
        result += fish(cur, i);
        // cout<<"fish: "<<i<<endl;
        // for(int y = 0 ; y < r; y++) {
        //     for(int x = 0; x< c; x++){
        //         cout<<map[cur][y][x].z<<" ";        
        //     }cout<<endl;
        // }
 
        move(cur);
        // cout<<"move: "<<i<<endl;
        // for(int y = 0 ; y < r; y++) {
        //     for(int x = 0; x< c; x++){
        //         cout<<map[cur][y][x].z<<" ";        
        //     }cout<<endl;
        // }
        cur = (cur + 1) % 2;
        
    }
    std::cout<<result<<std::endl;
    return 0;
}
728x90

댓글