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

 

13904번: 과제

예제에서 다섯 번째, 네 번째, 두 번째, 첫 번째, 일곱 번째 과제 순으로 수행하고, 세 번째, 여섯 번째 과제를 포기하면 185점을 얻을 수 있다.

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    priority_queue<pair<intint>vector<pair<intint>>,greater<pair<int,int>>> pq;
    priority_queue<intvector<int>, greater<int>> answer;
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int day, cost;
        cin >> day >> cost;
        pq.push(make_pair(day, cost));
    }
    int result = 0;
    while (!pq.empty()) {
        answer.push(pq.top().second);
        result += pq.top().second;
        if (answer.size() > pq.top().first) {
            result -= answer.top();
            answer.pop();
        }
        pq.pop();
    }
    cout << result << endl;
}
cs

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

 

1374번: 강의실

첫째 줄에 강의의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에 걸쳐 각 줄마다 세 개의 정수가 주어지는데, 순서대로 강의 번호, 강의 시작 시간, 강의 종료 시간을 의미한다. 강의

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
// 1374 강의실
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    priority_queue<pair<intint>vector<pair<intint>>, greater<pair<intint>>> pq;
    priority_queue<intvector<int>, greater<int>> tq;
    cin >> N;
    for (int i = 0; i < N; i++) {
        int num, st, ed;
        cin >> num >> st >> ed;
        pq.push(make_pair(st, ed));
    }
    while (!pq.empty()) {
        pair<intint> pre = pq.top();
        pq.pop();
        if (tq.empty()) {
            tq.push(pre.second);
        }
        else {
            if (pre.first >= tq.top()) {
                tq.pop();
                tq.push(pre.second);
            }
            else {
                tq.push(pre.second);
            }
        }
    }
    cout << tq.size();
}
cs

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

 

15922번: 아우으 우아으이야!!

N개의 선분을 모두 그렸을 때, 수직선 위에 그어진 선분 길이의 총합을 출력한다아아어으잉에애야우아으아이아야아아아아아아이야!!!

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
// 아우으 우아으이야!! 15922
 
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<pair<intint>> line;
    for (int i = 0; i < N; i++) {
        int x, y;
        cin >> x >> y;
        line.push_back(make_pair(x, y));
    }
    int temp_x = line.front().first;
    int temp_y = line.front().second;
    int total = temp_y - temp_x;
    for (int i = 1; i < N; i++) {
        int next_x = line[i].first;
        int next_y = line[i].second;
        if (next_x <= temp_y && next_y <= temp_y) {
            continue;
        }
        else if (next_x <= temp_y && next_y >= temp_y) {
            total += next_y - temp_y;
            temp_x = temp_y;
            temp_y = next_y;
        }
        else {
            total += next_y - next_x;
            temp_x = next_x;
            temp_y = next_y;
        }
    }
    cout << total << endl;
}
cs

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

 

13164번: 행복 유치원

입력의 첫 줄에는 유치원에 있는 원생의 수를 나타내는 자연수 N(1 ≤ N ≤ 300,000)과 나누려고 하는 조의 개수를 나타내는 자연수 K(1 ≤ K ≤ N)가 공백으로 구분되어 주어진다. 다음 줄에는 원생들

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
// 13164 행복유치원
int N, K;
vector<int> v;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> K;
    int prev_height = 0;
    cin >> prev_height;
    for (int i = 0; i < N - 1; i++) {
        int temp;
        cin >> temp;
        v.push_back(temp - prev_height);
        prev_height = temp;
    }
    sort(v.begin(), v.end());
    int result = 0;
    for (int i = 0; i < N - K; i++) {
        result += v[i];
    }
    cout << result << endl;
}
cs

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

 

11000번: 강의실 배정

첫 번째 줄에 N이 주어진다. (1 ≤ N ≤ 200,000) 이후 N개의 줄에 Si, Ti가 주어진다. (1 ≤ Si < Ti ≤ 109)

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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
using namespace std;
int N;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> N;
    priority_queue < pair<intint>vector<pair<intint>>,greater<pair<int,int>>> pq;
    for (int i = 0; i < N; i++) {
        int S, T;
        cin >> S >> T;
        pq.push(make_pair(S, T));
    }
    priority_queue<intvector<int>, greater<int>> time;
    while (!pq.empty()) {
        pair<intint> top = pq.top();
        pq.pop();
        if (time.empty()) {
            time.push(top.second);
        }
        else {
            if (top.first >= time.top()) {
                time.pop();
                time.push(top.second);
            }
            else {
                time.push(top.second);
            }
        }
    }
    cout << time.size() << endl;
}
cs

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

 

21611번: 마법사 상어와 블리자드

마법사 상어는 파이어볼, 토네이도, 파이어스톰, 물복사버그, 비바라기 마법을 할 수 있다. 오늘 새로 배운 마법은 블리자드이고, 크기가 N×N인 격자에서 연습하려고 한다. N은 항상 홀수이고, (

www.acmicpc.net

지금까지 푼 문제중에 제일 오래 걸리고 제일 많이 틀린 문제이다.. 나에게 좌절감을 준 문제.

틀린 횟수...

내가 잘못 생각한 부분이 있었는데, 이 생각을 고치지 않아서 계속해서 틀린것이다.
나는 이 방식으로 진행했다.
1. N*N 맵을 일자로 핀다, 그 네모난 맵에는 들린 순서대로 1,2,3,,,, N*N -1 숫자를 부여한다.
2. 그리고 마법을 일으킨다. 이때 마법이 일어나면 아까 초기맵에 들린 순서대로 숫자를 부여 했으므로, 일자로 펼친 맵의 몇번쨰 index를 삭제하는지 알 수 있다.
3. 그 다음은 블럭을 터트린다. 이떄 일자 맵에 있는 것을 deque를 이용해 (횟수,숫자) 이 양식을 지켜 만든다.
터트린 블록은 제외한다.(터트린 블럭은 index를 알고 잇으므로, index가 같은 거는 그냥 continue)
4. 그리고 점수를 얻는 과정이다. 
우선 주의해야할게 많다. 나는 기존에 터트리자마자 배열을 앞으로 이동, 또 숫자가 겹치면 터트리게 하였다.

하지만 문제를 보면, 모두 터트리고 난 뒤에 다시 숫자를 움직이는 것을 알 수 있었다..
5. 그 다음에는 맵을 다시 일자로 만들어준다, 이 경우는 위에 횟수 숫자로 해놨기 때문에 쉽다.

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
// 마법사 상어와 블리자드 21611번
#include <iostream>
#include <vector>
#include <utility>
#include <deque>
#define MAX 50
using namespace std;
int N, M;
// ↑, ↓, ←, →
int pop_dx[5= { 0,-1,1,0,0 };
int pop_dy[5= { 0,0,0,-1,1 };
// ←, ↓, →, ↑ 일짜로 피는 과정
int dx[4= { 0,1,0,-1 };
int dy[4= { -1,0,1,0 };
int MAP[MAX][MAX];
vector<pair<intint>> operation;
// 처음부터 맵을 피고, 저렇게 할 때만 맵을 새로 만든다 그리고 다시 벡터로 맵 만들고
deque<pair<intint>> map_1;
deque<int> map_2;
deque<int> magic_pop;
int total_count = 0;
void turn(int& x, int& y, int& dir, int& index, int& count) {
    for (int i = 0; i < index; i++) {
        x += dx[dir];
        y += dy[dir];
        if (MAP[x][y] == 0) {
            MAP[x][y] = count++;
        }
        else {
            map_2.push_back(MAP[x][y]);
            MAP[x][y] = count++;
        }
    }
    dir += 1;
    if (dir == 4)
        dir = 0;
}
void make_in_a_row() {
    // map2 만들고, MAP을 순서에 따라 index
    int x = N / 2 + 1;
    int y = N / 2 + 1;
    int dir = 0;
    int index = 1;
    int count = 1;
    map_2.push_back(0);
    while (x != 1 || y != 1) {
        if (index == N - 1) {
            turn(x, y, dir, index, count);
            turn(x, y, dir, index, count);
            turn(x, y, dir, index, count);
        }
        else {
            turn(x, y, dir, index, count);
            turn(x, y, dir, index, count);
            index++;
        }
    }
}
void pop_marble() {
    for (int i = 0; i < map_2.size(); i++) {
        if (!magic_pop.empty() && i == magic_pop.front()) {
            magic_pop.pop_front();
            continue;
        }
        if (map_2[i] == 0)
            map_1.push_back(make_pair(00));
        else if (map_1.back().second == map_2[i])
            map_1.back().first++;
        else 
            map_1.push_back(make_pair(1, map_2[i]));
    }
    map_2.clear();
    magic_pop.clear();
    bool flag = false;
    while (flag == false) {
        deque<pair<intint>> temp;
        flag = true;
        temp.push_back(map_1.front());
        map_1.pop_front();
        while (!map_1.empty()) {
            if (map_1.front().first >= 4) {
                total_count += map_1.front().first * map_1.front().second;
                map_1.pop_front();
                flag = false;
                continue;
            }
            if (temp.back().second == map_1.front().second) {
                temp.back().first += map_1.front().first;
                map_1.pop_front();
                continue;
            }
            temp.push_back(map_1.front());
            map_1.pop_front();
        }
        map_1 = temp;
    }
    int count = 0;
    for (auto i : map_1) {
        if (i.first == 0) {
            map_2.push_front(0);
            continue;
        }
        if (count == N * N - 1) {
            break;
        }
        map_2.push_back(i.first);
        map_2.push_back(i.second);
        count += 2;
    }
    map_1.clear();
}
void magic(const int& dir, const int& size) {
    int x = N / 2 + 1;
    int y = N / 2 + 1;
    for (int i = 0; i < size; i++) {
        x += pop_dx[dir];
        y += pop_dy[dir];
        magic_pop.push_back(MAP[x][y]);
    }
}
void solve() {
    make_in_a_row();
    for (auto i : operation) {
        magic(i.first, i.second);
        pop_marble();
    }
    cout << total_count << endl;
}
void input() {
    cin >> N >> M;
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            cin >> MAP[i][j];
    for (int i = 1; i <= M; i++) {
        int dir, size;
        cin >> dir >> size;
        operation.push_back(make_pair(dir, size));
    }
}
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
int main() {
    init();
    input();
    solve();
}
cs

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

 

20061번: 모노미노도미노 2

모노미노도미노는 아래와 같이 생긴 보드에서 진행되는 게임이다. 보드는 빨간색 보드, 파란색 보드, 초록색 보드가 그림과 같이 붙어있는 형태이다. 게임에서 사용하는 좌표 (x, y)에서 x는 행,

www.acmicpc.net

생각보다 까다로운 문제.

크게 4가지  함수로 구현했다.

하나는 block_add , 그 다음은 check_point, 그 다음은 연한색 영역인 check_soft, 그 다음은 블록 행또는 열을 지워주는
down_block_point_or_disappear 함수.

1
2
3
4
5
6
7
#include <iostream>
#include <deque>
using namespace std;
int map[10][10];
int N, T, X, Y;
int block[4][2= { {0,0},{0,0},{0,1},{1,0} };
int total_result = 0;
cs

다음과 같이 변수들을 전역으로 사용했다.

map는 그냥 10*10이다. 초록색 파란색을 따로 선언할까 하는 생각을 했지만, 이게 더 편한거같다. 간편하게 행과 열만 바꿔서 생각하면된다.
block은 다음과 같이 한 이유는 이따가 설명, 뭐 대충보면 두개인 경우 한개 인 경우가 있으므로 저렇게 한 것이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void solve() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> T >> X >> Y;
        block_add();
        check_point();
        check_soft();
    }
    cout << total_result << endl;
    int count = 0;
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (map[i][j] == 1)
                count++;
        }
    }
    cout << count << endl;
}
cs

solve함수 이다. 입력과 동시에 block_add, check_point, check_soft가 작동한다.
block_add함수는 다음과 같다.

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
void block_add() {
    int x1 = X;
    int x2 = X + block[T][0];
    int y1 = Y;
    int y2 = Y + block[T][1];
    while (1) {
        if (x1 == 10 || x2 == 10) {
            x1--, x2--;
            break;
        }
        if (map[x1][y1] == 1 || map[x2][y2] == 1) {
            x1--, x2--;
            break;
        }
        else {
            x1++, x2++;
        }
    }
    map[x1][y1] = 1; map[x2][y2] = 1;
    x1 = X;
    x2 = X + block[T][0];
    y1 = Y;
    y2 = Y + block[T][1];
    while (1) {
        if (y1 == 10 || y2 == 10) {
            y1--, y2--;
            break;
        }
        if (map[x1][y1] == 1 || map[x2][y2] == 1) {
            y1--, y2--;
            break;
        }
        else {
            y1++, y2++;
        }
    }
    map[x1][y1] = 1; map[x2][y2] = 1;
}
cs

block_add 함수 이다. 이름과 같이 block을 추가하는 함수.
블럭은 많아도 좌표가 두개이다. 그러므로 다음과 같이 구현했다.
입력 받은(x,y), (x+dx[type][0], y+dy[type][1]) 이다. 
블럭이 내려가는 과정은 중력과 같다. 그냥 두개의 x1,x2좌표, 또는 y1,y2좌표의 map칸이 1이 아니고, 9이하이면 된다.

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
void check_point() {
    for (int i = 6; i <= 9; i++) {
        int count = 0;
        for (int j = 0; j < 4; j++) {
            if (map[i][j] == 1)
                count++;
        }
        if (count == 4) {
            for (int j = 0; j < 4; j++) {
                map[i][j] = 0;
            }
            total_result++;
            down_block_point_or_disappear(i, 1);
        }
    }
    for (int j = 6; j <= 9; j++) {
        int count= 0;
        for (int i = 0; i < 4; i++) {
            if (map[i][j] == 1)
                count++;
        }
        if (count == 4) {
            for (int i = 0; i < 4; i++) {
                map[i][j] = 0;
            }
            total_result++;
            down_block_point_or_disappear(j, 0);
        }
    }
}
cs

포인트를 얻는 과정, 여기선 그냥 연한칸을 제외하고 행과 열에서 1이 4개인 것을 count해서 이 행을 0으로 만든  뒤에 점수를 1점 더해주면된다.
그리고 블록이 위에서 내려오는 과정은 down_block_point_or_disappear로 구현했다.
0이면 푸른색, 1이면 초록색이다.

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
void down_block_point_or_disappear(int index, int line) { // line 0 이면 블루, 1 이면 그린
    deque<int> dq;
    if (line == 1) {
        for (int i = 4; i <= index - 1; i++) {
            for (int j = 0; j < 4; j++) {
                dq.push_back(map[i][j]);
                map[i][j] = 0;
            }
        }
        for (int i = 5; i <= index; i++) {
            for (int j = 0; j < 4; j++) {
                map[i][j] = dq.front();
                dq.pop_front();
            }
        }
    }
    else {
        for (int j = 4; j <= index - 1; j++) {
            for (int i = 0; i < 4; i++) {
                dq.push_back(map[i][j]);
                map[i][j] = 0;
            }
        }
        for (int j = 5; j <= index; j++) {
            for (int i = 0; i < 4; i++) {
                map[i][j] = dq.front();
                dq.pop_front();
            }
        }
    }
}
cs

이 함수는 대충 이런느낌이다. 포인트를 얻거나, 연한색에서 사라지면 위에줄을 한줄 내리는 방식,
dq에 순서를 맞춰서 넣은 다음에, 아래쪽 줄로 한줄 내려서 넣는다.

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
void check_soft() {
    int count = 0;
    for (int i = 4; i < 6; i++) {
        for (int j = 0; j < 4; j++) {
            if (map[i][j] == 1) {
                count++;
                break;
            }
        }
    }
    for (int i = 0; i < count; i++) {
        down_block_point_or_disappear(91);
    }
    count = 0;
    for (int j = 4; j < 6; j++) {
        for (int i = 0; i < 4; i++) {
            if (map[i][j] == 1) {
                count++;
                break;
            }
        }
    }
    for (int i = 0; i < count; i++
        down_block_point_or_disappear(90);
}
cs

check soft함수 연한색에 블럭이 있으면, 맨 아래 행이나 열을 삭제한다.
맨 아래 행이나 열 이므로 9가 들어간다.
전체 코드는 다음과 같다.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <deque>
using namespace std;
int map[10][10];
int N, T, X, Y;
int block[4][2= { {0,0},{0,0},{0,1},{1,0} };
int total_result = 0;
void print() {
    cout << "green" << endl;
    for (int i = 4; i <= 9; i++) {
        for (int j = 0; j < 4; j++) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
    cout << "blue" << endl;
    for (int i = 0; i < 4; i++) {
        for (int j = 4; j <= 9; j++) {
            cout << map[i][j] << " ";
        }
        cout << endl;
    }
    cout << endl;
}
void down_block_point_or_disappear(int index, int line) { // line 0 이면 블루, 1 이면 그린
    deque<int> dq;
    if (line == 1) {
        for (int i = 4; i <= index - 1; i++) {
            for (int j = 0; j < 4; j++) {
                dq.push_back(map[i][j]);
                map[i][j] = 0;
            }
        }
        for (int i = 5; i <= index; i++) {
            for (int j = 0; j < 4; j++) {
                map[i][j] = dq.front();
                dq.pop_front();
            }
        }
    }
    else {
        for (int j = 4; j <= index - 1; j++) {
            for (int i = 0; i < 4; i++) {
                dq.push_back(map[i][j]);
                map[i][j] = 0;
            }
        }
        for (int j = 5; j <= index; j++) {
            for (int i = 0; i < 4; i++) {
                map[i][j] = dq.front();
                dq.pop_front();
            }
        }
    }
}
void check_soft() {
    int count = 0;
    for (int i = 4; i < 6; i++) {
        for (int j = 0; j < 4; j++) {
            if (map[i][j] == 1) {
                count++;
                break;
            }
        }
    }
    for (int i = 0; i < count; i++) {
        down_block_point_or_disappear(91);
    }
    count = 0;
    for (int j = 4; j < 6; j++) {
        for (int i = 0; i < 4; i++) {
            if (map[i][j] == 1) {
                count++;
                break;
            }
        }
    }
    for (int i = 0; i < count; i++
        down_block_point_or_disappear(90);
}
void check_point() {
    for (int i = 6; i <= 9; i++) {
        int count = 0;
        for (int j = 0; j < 4; j++) {
            if (map[i][j] == 1)
                count++;
        }
        if (count == 4) {
            for (int j = 0; j < 4; j++) {
                map[i][j] = 0;
            }
            total_result++;
            down_block_point_or_disappear(i, 1);
        }
    }
    for (int j = 6; j <= 9; j++) {
        int count= 0;
        for (int i = 0; i < 4; i++) {
            if (map[i][j] == 1)
                count++;
        }
        if (count == 4) {
            for (int i = 0; i < 4; i++) {
                map[i][j] = 0;
            }
            total_result++;
            down_block_point_or_disappear(j, 0);
        }
    }
}
void block_add() {
    int x1 = X;
    int x2 = X + block[T][0];
    int y1 = Y;
    int y2 = Y + block[T][1];
    while (1) {
        if (x1 == 10 || x2 == 10) {
            x1--, x2--;
            break;
        }
        if (map[x1][y1] == 1 || map[x2][y2] == 1) {
            x1--, x2--;
            break;
        }
        else {
            x1++, x2++;
        }
    }
    map[x1][y1] = 1; map[x2][y2] = 1;
    x1 = X;
    x2 = X + block[T][0];
    y1 = Y;
    y2 = Y + block[T][1];
    while (1) {
        if (y1 == 10 || y2 == 10) {
            y1--, y2--;
            break;
        }
        if (map[x1][y1] == 1 || map[x2][y2] == 1) {
            y1--, y2--;
            break;
        }
        else {
            y1++, y2++;
        }
    }
    map[x1][y1] = 1; map[x2][y2] = 1;
}
void solve() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> T >> X >> Y;
        block_add();
        check_point();
        check_soft();
    }
    cout << total_result << endl;
    int count = 0;
    for (int i = 0; i < 10; i++) {
        for (int j = 0; j < 10; j++) {
            if (map[i][j] == 1)
                count++;
        }
    }
    cout << count << endl;
}
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
int main() {
    init();
    solve();
}
cs

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

 

21609번: 상어 중학교

상어 중학교의 코딩 동아리에서 게임을 만들었다. 이 게임은 크기가 N×N인 격자에서 진행되고, 초기에 격자의 모든 칸에는 블록이 하나씩 들어있고, 블록은 검은색 블록, 무지개 블록, 일반 블록

www.acmicpc.net

상당히 더러웠던 문제.

문제 자체는 어렵지 않다. 하지만 시키는게 많다, 터트리고, 중력, 돌리고, 중력...

코드의 흐름은 다음과 같다.

 

init() 에서는 빠른 입력을 위한 것

 

input() 도 그냥 입력 받는다. 딱히 특별한게 없다.


solve() 함수 에서 부터 문제 풀이를 시작한다.


find_block_group()을 돌린다, 이 함수는 false가 return되면 오토플레이가 끝나게 해놨다.
find_block_group에서는 블록그룹을 찾고, 터트려야하는 것을 판별, 그다음은 터트리는 것 까지 구현했다.

우선 memset을 이용해 check 함수, 즉 방문여부를 확인 하는 것을 false로 초기화 하였다.
맵 크기가 21*21이라 크지 않아서 해도 시간초과에 상관없을거같다.
이중 포문과 bfs를 통해서 블록 그룹을 찾는다. 이때 0도아니고, -1도 아니고 -2(빈칸)도 아니여야 한다.
그리고 check배열이 false여야 한다.

1
2
3
4
5
6
7
8
struct block {
    deque<pair<intint>> Rainbow_block;
    deque<pair<intint>> Base_block;
    int total_size;
    int base_block_x;
    int base_block_y;
    int base_color;
};
cs

이때 다음과 같은 block 그룹 함수를 사용 했다. 블럭의 색, 기준블럭의 좌표, 무지개랑 기준블럭을 저장하는 deque.
그 다음에는 bfs로 탐색한다. base_color가 같은것, 그리고 무지개 블록, bfs는 다음과 같다.

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
void bfs(const int& i, const int& j) {
    block temp;
    queue<pair<intint>> q;
    // 색깔지정 및 탐색 준비
    temp.base_color = MAP[i][j];
    temp.total_size = 0;
    temp.base_block_x = i;
    temp.base_block_y = j;
    check[i][j] = true;
    q.push(make_pair(i, j));
    // 베이스 블럭이 당연히 맨 처음시작하는 곳 아닌가? 라고 생각
    // 위에서 부터 시작하니
    // 탐색 시작
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (MAP[x][y] == temp.base_color) {
            temp.Base_block.push_back(make_pair(x, y));
            temp.total_size++;
        }
        else if (MAP[x][y] == 0) {
            temp.Rainbow_block.push_back(make_pair(x, y));
            temp.total_size++;
        }
        for (int index = 0; index < 4; index++) {
            int next_x = x + dx[index];
            int next_y = y + dy[index];
            if (check_range(next_x, next_y) && check[next_x][next_y] == false) {
                if (MAP[next_x][next_y] == temp.base_color || MAP[next_x][next_y] == 0) {
                    check[next_x][next_y] = true;
                    q.push(make_pair(next_x, next_y));
                }
            }
        }
    }
    // 탐색 종료 후 정리
    for (auto i : temp.Rainbow_block)
        check[i.first][i.second] = false;
    if (temp.total_size < 2)
        return;
    block_group.push_back(temp);
}
cs

평범한 bfs지만, 색블럭 저장, 무지개 블럭 저장하는 과정만 추가. 그리고 탐색이 끝난 뒤에는 다음과 같이 
check 배열에서 rainbow_block들은 다시 false로 처리 해야한다. 왜냐하면 다른 블록 그룹도 무지개 블록을 가질 수 
있기때문이다.
그리고 만약 size가 2보다 작으면 그냥 return, 문제조건이다,
block_group에 push_back해준다.
이 과정을 거치면 모든  block_group을 찾을 수 있다. 그러면 문제의 조건에 맞춰서  터트릴 block을 선정해야한다.
나는 정렬을 사용했다 . 다음과 같은 cmp함수를 만들었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool cmp(const block& x, const block& y) {
    if (x.total_size != y.total_size)
        return x.total_size > y.total_size;
    else {
        if (x.Rainbow_block.size() != y.Rainbow_block.size())
            return x.Rainbow_block.size() > y.Rainbow_block.size();
        else {
            if (x.base_block_x != y.base_block_x)
                return x.base_block_x > y.base_block_x;
            else
                return x.base_block_y > y.base_block_y;
        }
    }
}
cs

뭐 그냥 그렇다. 처음 조건 size가 큰것, 그 다음은 무지개 블럭 사이즈가 큰거, 그 다음은 기준블럭의 행, 그 다음은 기준블럭의 열.
그러면 터트려야 하는 것이 0번 인덱스로 온다.
그리고 나면 뭐 deque에 있는 터트리는 블럭 좌표를 pop하고, 더해주면 끝이다.
그 다음은 중력,
중력은 별거없다. 맨 아래줄부터 아래가 뭔지 check하면서 내려오면된다. 
반시계방향도 별거 없다. 기존에 1열이 -> 5행, 2열 ->4행,... 5열->1행으로 가는 것만 알면된다.
그리고 코드를 반복하면 끝이다. 전체코드는 다음과 같다.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// 백준 21609 상어 중학교
#include <iostream>
#include <queue>
#include <deque>
#include <utility>
#include <iomanip>
#include <cstring>
#include <algorithm>
#define MAX 20+1
using namespace std;
struct block {
    deque<pair<intint>> Rainbow_block;
    deque<pair<intint>> Base_block;
    int total_size;
    int base_block_x;
    int base_block_y;
    int base_color;
};
int dx[4= { 0,1,0,-1 };
int dy[4= { 1,0,-1,0 };
int N, M;
int MAP[MAX][MAX];
bool check[MAX][MAX] = { false };
deque<block> block_group;
int result_point = 0;
void reverse_clock_wise() {
    // 시계방향으로 돌리면 1열 2열 3열 4열 5열 -> 5행 4행 3행 2행 1행
    queue<int> q;
    for (int j = 1; j <= N; j++)
        for (int i = 1; i <= N; i++)
            q.push(MAP[i][j]);
    for (int i = N; i >= 1; i--) {
        for (int j = 1; j <= N; j++) {
            MAP[i][j] = q.front();
            q.pop();
        }
    }
}
void Gravity() {
    // 내려가는 방향은 1번 인덱스
    for (int i = N - 1; i >= 1; i--) {
        for (int j = 1; j <= N; j++) {
            if (MAP[i][j] == -1 || MAP[i][j] == -2)
                continue;
            int num = MAP[i][j];
            MAP[i][j] = -2;
            int prev_x = i;
            prev_x++;
            while (prev_x <= N) {
                if (MAP[prev_x][j] != -2)
                    break;
                prev_x++;
            }
            prev_x--;
            MAP[prev_x][j] = num;
        }
    }
}
bool cmp(const block& x, const block& y) {
    if (x.total_size != y.total_size)
        return x.total_size > y.total_size;
    else {
        if (x.Rainbow_block.size() != y.Rainbow_block.size())
            return x.Rainbow_block.size() > y.Rainbow_block.size();
        else {
            if (x.base_block_x != y.base_block_x)
                return x.base_block_x > y.base_block_x;
            else
                return x.base_block_y > y.base_block_y;
        }
    }
}
bool check_range(const int& next_x, const int& next_y) {
    if (next_x >= 1 && next_y >= 1 && next_x <= N && next_y <= N)
        return true;
    else
        return false;
}
void bfs(const int& i, const int& j) {
    block temp;
    queue<pair<intint>> q;
    // 색깔지정 및 탐색 준비
    temp.base_color = MAP[i][j];
    temp.total_size = 0;
    temp.base_block_x = i;
    temp.base_block_y = j;
    check[i][j] = true;
    q.push(make_pair(i, j));
    // 베이스 블럭이 당연히 맨 처음시작하는 곳 아닌가? 라고 생각
    // 위에서 부터 시작하니
    // 탐색 시작
    while (!q.empty()) {
        int x = q.front().first;
        int y = q.front().second;
        q.pop();
        if (MAP[x][y] == temp.base_color) {
            temp.Base_block.push_back(make_pair(x, y));
            temp.total_size++;
        }
        else if (MAP[x][y] == 0) {
            temp.Rainbow_block.push_back(make_pair(x, y));
            temp.total_size++;
        }
        for (int index = 0; index < 4; index++) {
            int next_x = x + dx[index];
            int next_y = y + dy[index];
            if (check_range(next_x, next_y) && check[next_x][next_y] == false) {
                if (MAP[next_x][next_y] == temp.base_color || MAP[next_x][next_y] == 0) {
                    check[next_x][next_y] = true;
                    q.push(make_pair(next_x, next_y));
                }
            }
        }
    }
    // 탐색 종료 후 정리
    for (auto i : temp.Rainbow_block)
        check[i.first][i.second] = false;
    if (temp.total_size < 2)
        return;
    block_group.push_back(temp);
}
bool find_block_group() {
    // 블록 그룹 먼저 찾기
    memset(check, falsesizeof(check));
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            if (MAP[i][j] != 0 && MAP[i][j] != -1 && MAP[i][j] != -2 && check[i][j] == false)
                bfs(i, j);
    if (block_group.empty())
        return false;
    sort(block_group.begin(), block_group.end(), cmp);
    // block 터트리기 -2 는 빈칸
    result_point += block_group[0].total_size * block_group[0].total_size;
    for (auto i : block_group[0].Rainbow_block)
        MAP[i.first][i.second] = -2;
    for (auto i : block_group[0].Base_block)
        MAP[i.first][i.second] = -2;
    block_group.clear();
    return true;
}
void solve() {
    for (;;) {
        if (find_block_group() == false)
            return;
        Gravity();
        reverse_clock_wise();
        Gravity();
    }
}
void input() {
    cin >> N >> M;
    // (-1,검은색),(0,무지개),그리고 나머지 M까지 색
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= N; j++)
            cin >> MAP[i][j];
}
void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
}
int main() {
    init();
    input();
    solve();
    cout << result_point << endl;
}
cs

+ Recent posts