Muscardinus
이차원 배열과 연산 본문
728x90
#include <iostream>
using namespace std;
int map[101][101];
int r, c, k;
int pre_row_size, cur_row_size;
int pre_col_size, cur_col_size;
int answer = 0;
void update_row(int sy) {
int bucket[101] = { 0, };
for (int x = 1; x <= pre_col_size; x++) {
bucket[map[sy][x]]++;
}
int size = 0;
for (int i = 1; i <= 100 && size < 100; i++) {
for (int j = 1; j <= 100 && size < 100; j++) {
if (bucket[j] == i) {
map[sy][++size] = j;
map[sy][++size] = i;
}
}
}
for (int i = size + 1; i <= 100; i++) {
map[sy][i] = 0;
}
if (cur_col_size < size) {
cur_col_size = size;
}
}
void update_col(int sx) {
int bucket[101] = { 0, };
for (int y = 1; y <= pre_row_size; y++) {
bucket[map[y][sx]]++;
}
int size = 0;
for (int i = 1; i <= 100 && size < 100; i++) {
for (int j = 1; j <= 100 && size < 100; j++) {
if (bucket[j] == i) {
map[++size][sx] = j;
map[++size][sx] = i;
}
}
}
for (int i = size + 1; i <= 100; i++) {
map[i][sx] = 0;
}
if (cur_row_size < size) {
cur_row_size = size;
}
}
void solve() {
while (map[r][c] != k) {
if (cur_row_size >= cur_col_size) {
pre_col_size = cur_col_size;
cur_col_size = 0;
//row update
for (int y = 1; y <= cur_row_size; y++) {
update_row(y);
}
}
else {
pre_row_size = cur_row_size;
cur_row_size = 0;
//col update
for (int x = 1; x <= cur_col_size; x++) {
update_col(x);
}
}
answer++;
if (answer > 100) return;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin >> r >> c >> k;
pre_row_size = 3; pre_col_size = 3;
cur_row_size = 3; cur_col_size = 3;
for (int y = 1; y <= 3; y++) {
for (int x = 1; x <= 3; x++) {
cin >> map[y][x];
}
}
answer = 0;
solve();
if (answer > 100) answer = -1;
cout << answer << "\n";
return 0;
}
728x90
Comments