17140 Samsung sw test
코드
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
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define MAX_NUM 101
using namespace std;
typedef struct Cell {
int value;
int count;
bool operator < (Cell c) {
if (count < c.count)
return true;
else if (c.count < count)
return false;
else {
if (value < c.value)
return true;
else
return false;
}
}
} Cell;
int map[MAX_NUM][MAX_NUM];
int width, height;
int r, c, k;
int getTimeToReachRCK();
void print() {
cout << endl;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++)
cout << map[i][j] << " ";
cout << endl;
}
}
int main() {
cin >> r >> c >> k;
for (int i = 0; i < MAX_NUM; i++)
memset(map[i], 0, sizeof(int) * MAX_NUM);
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cin >> map[i][j];
width = height = 3;
cout << getTimeToReachRCK();
}
int getTimeToReachRCK() {
int time = 0;
for (time; time <= 100; time++) {
if (map[r-1][c-1] == k)
break;
int temp[MAX_NUM];
memset(temp, 0, sizeof(int) * (MAX_NUM));
if (height >= width) {
int maxWidth = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
temp[map[i][j]]++;
}
vector<Cell> temp2;
for (int j = 1; j < MAX_NUM; j++)
if (temp[j] != 0)
temp2.push_back({ j, temp[j] });
sort(temp2.begin(), temp2.end());
int index = 0;
for (int j = 0; j < temp2.size(); j++) {
if (index >= 100)
break;
map[i][index++] = temp2[j].value;
map[i][index++] = temp2[j].count;
}
if (index < width)
for (int j = index; j < width; j++)
map[i][j] = 0;
maxWidth = index > maxWidth ? index : maxWidth;
memset(temp, 0, sizeof(int) * (MAX_NUM));
}
width = maxWidth;
}
else {
int maxHeight = 0;
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
temp[map[j][i]]++;
}
vector<Cell> temp2;
for (int j = 1; j < MAX_NUM; j++)
if (temp[j] != 0)
temp2.push_back({ j, temp[j] });
sort(temp2.begin(), temp2.end());
int index = 0;
for (int j = 0; j < temp2.size(); j++) {
if (index >= 100)
break;
map[index++][i] = temp2[j].value;
map[index++][i] = temp2[j].count;
}
if (index < height)
for (int j = index; j < height; j++)
map[j][i] = 0;
maxHeight = index > maxHeight ? index : maxHeight;
memset(temp, 0, sizeof(int) * MAX_NUM);
}
height = maxHeight;
}
}
return (time == 101 ? -1 : time);
}