https://www.acmicpc.net/problem/17140
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
int r, c, k;
vector<vector<int>> m;
int answer = 0;
void copym() {
cout << endl;
for (int i = 0; i < m.size(); i++) {
for (int j = 0; j < m[i].size(); j++) {
cout << m[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
bool cmp(pair<int, int> a, pair<int, int> b) {
if (a.second < b.second) return true;
else if (a.second == b.second) {
return a.first < b.first;
}
else return false;
}
void R_cal() {
vector<vector<int>> m_tmp;
m_tmp.resize(m.size());
int max_len = 0;
for (int i = 0; i < m.size(); i++) {
vector<pair<int, int>> tmp;
map <int, int> num;
vector<int> sorting;
for (int j = 0; j < m[i].size(); j++) {
if (m[i][j] == 0) continue;
num[m[i][j]] += 1;
}
for (auto i = num.begin(); i != num.end(); i++) {
int number, count;
number = i->first;
count = i->second;
tmp.push_back({ number,count });
}
sort(tmp.begin(), tmp.end(), cmp);
int id = 0;
for (int i = 0; i < tmp.size(); i++) {
sorting.push_back(tmp[i].first);
sorting.push_back(tmp[i].second);
}
m_tmp[i] = sorting; //크기 에러 안나는지 확인
int sz = sorting.size();
max_len = max(max_len, sz);
}
m = m_tmp;
for (int i = 0; i < m.size(); i++) {
if (m[i].size() < max_len) {
int c = max_len - m[i].size();
for (int k = 0; k < c; k++) {
m[i].push_back(0);
}
}
}
}
void C_cal() {
vector<vector<int>> m_tmp;
m_tmp.resize(m.size() * 2 + 1);
int max_len = 0;
for (int j = 0; j < m[0].size(); j++) {
vector<pair<int, int>> tmp;
map <int, int> num;
vector<int> sorting;
for (int i = 0; i < m.size(); i++) {
if (m[i][j] == 0) continue;
num[m[i][j]] += 1;
}
for (auto i = num.begin(); i != num.end(); i++) {
int number, count;
number = i->first;
count = i->second;
tmp.push_back({ number,count });
}
sort(tmp.begin(), tmp.end(), cmp);
for (int i = 0; i < tmp.size(); i++) {
sorting.push_back(tmp[i].first);
sorting.push_back(tmp[i].second);
}
int sz = sorting.size();
max_len = max(max_len, sz);
int id = 0;
if (m_tmp.size() < max_len) {
m_tmp.resize(max_len);
}
for (int ii = 0; ii < sorting.size(); ii++) {
m_tmp[id].push_back(sorting[ii]);
id++;
}
for (int i = id; i < m_tmp.size(); i++) {
m_tmp[i].push_back(0);
}
}
m_tmp.resize(max_len);
int sz = m_tmp[0].size();
for (int i = 0; i < max_len; i++) {
if (m_tmp[i].size() < sz) {
int c = sz - m_tmp[i].size();
for (int k = 0; k < c; k++) {
m_tmp[i].push_back(0);
}
}
}
m.resize(max_len);
for (int i = 0; i < max_len; i++) {
for (int j = 0; j < m_tmp[0].size(); j++) {
if (m[i].size() <= j) m[i].push_back(m_tmp[i][j]);
else m[i][j] = m_tmp[i][j];
}
}
}
int main(void) {
cin >> r >> c >> k;
m.resize(3);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int value;
cin >> value;
m[i].push_back(value);
}
}
while (answer <= 100) {
if (r - 1 < m.size() && c - 1 < m[0].size() && m[r - 1][c - 1] == k) break;
int r_size = m.size();
int c_size = m[0].size();
if (r_size >= c_size) {//R 연산
R_cal();
}
else { //C 연산
C_cal();
}
//copym();
answer++;
}
if (answer > 100) cout << -1 << endl;
else cout << answer << endl;
}
'백준 알고리즘 > 구현' 카테고리의 다른 글
나무 재테크 C++ (0) | 2021.10.19 |
---|---|
낚시왕 C++ (0) | 2021.10.19 |
게리멘더링 2 C++ (0) | 2021.10.17 |
주사위 윷놀이 C++ (0) | 2021.10.16 |
모노미노도미노 2 (0) | 2021.10.15 |