알고리즘 문제/BOJ

17140번 이차원 배열과 연산

parkit 2019. 4. 18. 13:11
728x90
반응형

https://www.acmicpc.net/problem/17140



사실 처음에 틀릴 줄 알았는데, 맞았다.


시간에 따른 Map 배열을 출력하면서 


어디가 잘못 출력되는지, 어디를 실수했는지 등등 생각하면서


구현하였다.



그리고 코드가 좀 더러워 보이는건 기분 탓일 수도 있다.





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
#include <iostream>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
#include <sstream>
#include <tuple>
 
#pragma warning(disable:4996)  
#pragma comment(linker, "/STACK:336777216")
 
using namespace std;
 
int Map[110][110= { 0, }, Count[110= { 0, };
 
int r = 0, c = 0, k = 0, mr = 0, mc = 0, time = 0;
 
bool cmp(const pair<intint> & a, const pair<intint> & b)
{
    if (a.second == b.second) return a.first < b.first;
    else return a.second < b.second;
}
 
int simulation()
{
    for (int t = 0; t <= 100; t++)
    {        
        if (Map[r][c] == k) return t;
 
        vector<pair<intint> > v[110];
 
        bool use[110= { false, };
 
        int MAX = 0;
 
        if (mr >= mc) // 행의 개수가 더 많을 때
        {
            for (int i = 0; i < mr; i++)
            {        
                memset(use, falsesizeof(use));
                memset(Count, 0sizeof(Count));
 
                for (int j = 0; j < mc; j++)
                {
                    if (Map[i][j] == 0continue;
 
                    ++Count[Map[i][j]];
                }
                
                for (int j = 0; j < mc; j++)
                {
                    if (Map[i][j] == 0 || use[Map[i][j]]) continue;
 
                    use[Map[i][j]] = true;
                    v[i].push_back({ Map[i][j], Count[Map[i][j]] });
                }
            
                if (v[i].size() >= 2)
                    sort(v[i].begin(), v[i].end(), cmp);
                
                int vs = v[i].size();
 
                MAX = max(MAX, 2 * vs); // 최대 열의 개수
            }
 
            memset(Map, 0sizeof(Map)); 
            // [1, 1, 2, 2]에서 [1, 2] 이런 식으로 더 짧아 질 수 있으므로
            // 미리 0으로 초기화
 
            for (int i = 0; i < mr; i++)
                for (int j = 0, idx = 0; j < v[i].size(); j++, idx += 2)
                {
                    Map[i][idx] = v[i].at(j).first; 
                    Map[i][idx + 1= v[i].at(j).second;
                }
 
            if (MAX >= 100) MAX = 100;
 
            mc = MAX; // 열 개수 갱신
        }
        else
        {
            for (int j = 0; j < mc; j++)
            {
                memset(use, falsesizeof(use));
                memset(Count, 0sizeof(Count));
 
                for (int i = 0; i < mr; i++)
                {
                    if (Map[i][j] == 0continue;
 
                    ++Count[Map[i][j]];
                }
                
                for (int i = 0; i < mr; i++)
                {
                    if (Map[i][j] == 0 || use[Map[i][j]]) continue;
 
                    use[Map[i][j]] = true;
                    v[j].push_back({ Map[i][j], Count[Map[i][j]] });
                }
 
                if (v[j].size() >= 2) sort(v[j].begin(), v[j].end(), cmp);
 
                int vs = v[j].size();
 
                MAX = max(MAX, 2 * vs); // 최대 행의 개수
            }
            
            memset(Map, 0sizeof(Map));
 
            for (int j = 0; j < mc; j++)
                for (int i = 0, idx = 0; i < v[j].size(); i++, idx += 2)
                {
                    Map[idx][j] = v[j].at(i).first; 
                    Map[idx + 1][j] = v[j].at(i).second;
                }
 
            if (MAX >= 100) MAX = 100;
 
            mr = MAX; // 행 개수 갱신
        }
    }
 
    return -1;
}
 
int main(void)
{
    mr = mc = 3;
 
    scanf("%d %d %d"&r, &c, &k);
 
    --r; --c;
 
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)    
            scanf("%d"&Map[i][j]);
 
    printf("%d\n", simulation());
 
    return 0;
}
cs






















728x90
반응형