Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Cloud Run
- r
- 시뮬레이션
- 데이터 분석
- 그리디
- Air Table
- CI/CD
- 삼성SW역량테스트
- 컴퓨터 구조
- REACT
- dp
- 펜윅 트리
- 다익스트라
- LCS
- 삼성 SW 역량테스트
- 생활코딩
- BFS
- JavaScript
- 우선순위 큐
- Bit
- 접미사 배열
- Cloud Pub/Sub
- 수학
- ICPC
- 백준 1753번
- 이분탐색
- jpa
- 고속 푸리에 변환
- 다이나믹 프로그래밍
- 종만북
Archives
- Today
- Total
코딩스토리
백준 17140번 - 이차원 배열과 연산 본문
17140번: 이차원 배열과 연산
첫째 줄에 r, c, k가 주어진다. (1 ≤ r, c, k ≤ 100) 둘째 줄부터 3개의 줄에 배열 A에 들어있는 수가 주어진다. 배열 A에 들어있는 수는 100보다 작거나 같은 자연수이다.
www.acmicpc.net
난이도 자체는 어렵지 않았으나 구현이 너무 복잡했다..
처음에 map으로 접근해보려다가 2시간 넘게 날리고 다시 그냥 배열로 접근했다..
(처음부터 배열로 접근할걸 괜히 까불다가)
이 문제의 핵심 포인트는 아무래도 매 연산마다 바뀌는 숫자들의 개수를 파악해놓는 것 아닐까 싶다.
그리고 이런 문제를 풀때마다 실수하는 점은 i와 j를 헷갈리거나 +=인데 -=으로 쓰는 등 이런 점들을 조심해야 한다
(내가 저것들 땜에 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
|
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;
typedef pair<int, int> pii;
int arr[107][107];
int rm[107][107];
int cm[107][107];
int main() {
int r, c, k, x;
int row = 3, col = 3;
cin >> r >> c >> k;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> x;
arr[i][j] = x;
rm[i][x] += 1;
cm[j][x] += 1;
}
}
int ans = 0;
while (1) {
if (ans > 100 || arr[r - 1][c - 1] == k)break;
ans += 1;
if (row >= col) { // R 연산 시행
int max_size = 0;
for (int i = 0; i < row; i++) { // 모든 행에 대하여 연산
vector<pii>temp;
for (int j = 1; j <= 100; j++) {
if (rm[i][j] > 0) temp.push_back(pii(rm[i][j], j));
rm[i][j] = 0;
}
sort(temp.begin(), temp.end()); // 정렬
int size = temp.size();
max_size = max(size * 2, max_size);
int cnt = 0;
for (int j = 0; j < 50; j++) {
cm[cnt][arr[i][cnt]] -= 1; cm[cnt + 1][arr[i][cnt + 1]] -= 1;
if (j >= size) {
arr[i][cnt] = 0; arr[i][cnt + 1] = 0;
}
else {
rm[i][temp[j].first] += 1; rm[i][temp[j].second] += 1;
arr[i][cnt] = temp[j].second; arr[i][cnt + 1] = temp[j].first;
cm[cnt][temp[j].second] += 1; cm[cnt + 1][temp[j].first] += 1;
}
cnt += 2;
}
}
col = max(max_size, col);
}
else {
int max_size = 0;
for (int i = 0; i < col; i++) { // 모든 열에 대하여 연산
vector<pii>temp;
for (int j = 1; j <= 100; j++) {
if (cm[i][j] > 0) temp.push_back(pii(cm[i][j], j));
cm[i][j] = 0;
}
sort(temp.begin(), temp.end()); // 정렬
int size = temp.size();
max_size = max(size * 2, max_size);
int cnt = 0;
for (int j = 0; j < 50; j++) {
rm[cnt][arr[cnt][i]] -= 1; rm[cnt + 1][arr[cnt + 1][i]] -= 1;
if (j >= size) {
arr[cnt][i] = 0; arr[cnt + 1][i] = 0;
}
else {
cm[i][temp[j].first] += 1; cm[i][temp[j].second] += 1;
arr[cnt][i] = temp[j].second; arr[cnt + 1][i] = temp[j].first;
rm[cnt][temp[j].second] += 1; rm[cnt + 1][temp[j].first] += 1;
}
cnt += 2;
}
}
row = max(max_size, row);
}
}
if (arr[r - 1][c - 1] == k) cout << ans << "\n";
else cout << "-1" << "\n";
}
|
cs |
코드가 길어서 이해하기 어렵지만..
여러 생각들이 겹치다 보니 너무 복잡하게 구현된 것 같다...
'알고리즘 > BOJ 문제 풀이' 카테고리의 다른 글
백준 1339번 - 단어 수학 (0) | 2021.04.02 |
---|---|
백준 17142번 - 연구소 3 (0) | 2021.03.31 |
백준 4485번 - 녹색 옷 입은 애가 젤다지? (0) | 2021.02.22 |
백준 17135번 - 캐슬 디펜스 (0) | 2021.02.21 |
백준 9370번 - 미확인 도착지 (0) | 2021.02.16 |
Comments