-
Notifications
You must be signed in to change notification settings - Fork 0
/
17140.cpp
99 lines (95 loc) · 2.21 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
// https://www.acmicpc.net/problem/17140
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
pair<int,int> find_target(unordered_map<int,int>& m) {
if(m.empty() == 1) return {-1,-1};
int a=200, b=200;
for(auto ptr=m.begin(); ptr != m.end(); ptr++) {
if(ptr->second < b) {
a = ptr->first;
b = ptr->second;
}
else if(ptr->second == b && ptr->first < a)
a = ptr->first;
}
m.erase(a);
return {a, b};
}
void R_operation(vector<vector<int>>& a) {
int n=0;
pair<int,int> m;
unordered_map<int,int> elements;
for(int i=1; i<a.size(); i++) {
elements.clear();
for(int j=1; j<a[i].size(); j++) {
if(a[i][j] == 0) continue;
elements[a[i][j]] += 1;
}
m = find_target(elements);
for(int j=1; j<a[i].size(); j++) {
if(m.first == -1) a[i][j] = 0;
else if(m.first == 0) {
a[i][j] = m.second;
m = find_target(elements);
}
else {
a[i][j] = m.first;
m.first = 0;
}
}
while(m.first!=-1) {
if(m.first == 0) {
a[i].push_back(m.second);
m = find_target(elements);
}
else {
a[i].push_back(m.first);
m.first = 0;
}
}
if(a[i].size() > n) n = a[i].size();
}
for(int i=1; i<a.size(); i++) {
if(a[i].size() < n) a[i].resize(n, 0);
}
}
int solution(vector<vector<int>>& a, int r, int c, int k, int cnt) {
if(cnt > 100) return -1;
if(r < a.size() && c < a[1].size() && a[r][c] == k) return cnt;
if(a.size() >= a[1].size()) R_operation(a);
else {
vector<vector<int>> tmp(a[1].size(), vector<int>(a.size()));
for(int i=1; i<a.size(); i++) {
for(int j=1; j<a[i].size(); j++)
tmp[j][i] = a[i][j];
}
R_operation(tmp);
a.clear();
a.resize(tmp[1].size(), vector<int>(tmp.size()));
for(int i=1; i<tmp.size(); i++) {
for(int j=1; j<tmp[1].size(); j++)
a[j][i] = tmp[i][j];
}
}
if(a.size() > 101) a.resize(101, vector<int>(101));
for(int i=1; i<a.size(); i++) {
for(int j=1; j<a[i].size(); j++) {
if(a[i].size() > 101) a[i].resize(101);
}
}
return solution(a, r, c, k, cnt+1);
}
int main() {
cin.tie(0);
int r, c, k;
vector<vector<int>> a(4, vector<int>(4));
cin >> r >> c >> k;
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++)
cin >> a[i][j];
}
printf("%d", solution(a, r, c, k, 0));
return 0;
}