-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17140.cpp
202 lines (186 loc) · 2.9 KB
/
17140.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 17140. 이차원 배열과 연산
// 2020.02.26
// 시뮬레이션
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int map[101][101];
int tmpMap[101][101];
int counts[101];
int x, y;
int ans;
vector<pair<int, int>> v;
bool compare(pair<int, int>& a, pair<int, int>& b)
{
if (a.second == b.second)
{
return a.first < b.first;
}
return a.second < b.second;
}
void tmpMapInit()
{
for (int i = 0; i < 101; i++)
{
fill(tmpMap[i], tmpMap[i] + 101, 0);
}
}
void mapInit()
{
for (int i = 0; i < 101; i++)
{
fill(map[i], map[i] + 101, 0);
}
}
// R연산
void OperationR()
{
tmpMapInit();
int tmpX = 1;
for (int i = 1; i <= x; i++)
{
int tmpY = 1;
fill(counts, counts + 101, 0);
v.clear();
// 값이 0이 아닐때 counts값 증가
for (int j = 1; j <= y; j++)
{
if (map[i][j] != 0)
{
counts[map[i][j]]++;
}
}
// 0이 아닌 값들을 넣음
for (int j = 1; j <= 100; j++)
{
if (counts[j] != 0)
{
v.push_back({ j,counts[j] });
}
}
// 최대 열의 수 조정
y = max(y, (int)v.size() * 2);
if (y > 100)
{
y = 100;
}
// 주어진 조건대로 정렬
sort(v.begin(), v.end(), compare);
// tmpMap에 값 삽입
for (int j = 0; j < v.size(); j++)
{
tmpMap[tmpX][tmpY++] = v[j].first;
tmpMap[tmpX][tmpY++] = v[j].second;
if (tmpY > 100)
{
break;
}
}
tmpX++;
}
// 맵복사
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= y; j++)
{
map[i][j] = tmpMap[i][j];
}
}
}
// C연산
void OperationC()
{
tmpMapInit();
int tmpY = 1;
for (int i = 1; i <= y; i++)
{
int tmpX = 1;
fill(counts, counts + 101, 0);
v.clear();
// 값이 0이 아닐때 counts값 증가
for (int j = 1; j <= x; j++)
{
if (map[j][i] != 0)
{
counts[map[j][i]]++;
}
}
// 0이 아닌 값들을 넣음
for (int j = 1; j <= 100; j++)
{
if (counts[j] != 0)
{
v.push_back({ j,counts[j] });
}
}
// 최대 행의 수 조정
x = max(x, (int)v.size() * 2);
if (x > 100)
{
x = 100;
}
// 주어진 조건대로 정렬
sort(v.begin(), v.end(), compare);
// tmpMap에 값 삽입
for (int j = 0; j < v.size(); j++)
{
tmpMap[tmpX++][tmpY] = v[j].first;
tmpMap[tmpX++][tmpY] = v[j].second;
if (tmpX > 100)
{
break;
}
}
tmpY++;
}
// 맵복사
for (int i = 1; i <= x; i++)
{
for (int j = 1; j <= y; j++)
{
map[i][j] = tmpMap[i][j];
}
}
}
int main()
{
int r, c, k;
cin >> r >> c >> k;
x = 3; // 행
y = 3; // 열
for (int i = 1; i <= 3; i++)
{
for (int j = 1; j <= 3; j++)
{
cin >> map[i][j];
}
}
while (1)
{
// map[r][c]에 k가 있다면 종료
if (map[r][c] == k)
{
break;
}
// 100번을 넘게했는데도 못했다면 종료
if (ans > 100)
{
ans = -1;
break;
}
ans++;
// R연산
if (x >= y)
{
OperationR();
}
// C연산
else
{
OperationC();
}
}
cout << ans << endl;
return 0;
}