반응형

문제 출처 :


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



알고리즘 분석 :


문제 해결에 필요한 사항

1. 이분 매칭 :: http://www.crocus.co.kr/499

2. 룩 시리즈 :: http://www.crocus.co.kr/search/rook

3. 비숍 시리즈 :: http://www.crocus.co.kr/search/%EB%B9%84%EC%88%8D


이 문제는 이분 매칭으로 해결이 가능하고, 룩, 비숍 시리즈 문제를 병행해서 푸는 것을 추천한다.


[1799번] 비숍 :: http://www.crocus.co.kr/774


위의 비숍 문제와 상당히 유사한 문제이다.


위의 문제는 1이 아닌 0인 부분에는 놓을 수는 없지만 이동하는데는 지장이 없다.


하지만, 이번 문제는 1이 아닌 0인 부분에 놓을 수도 없고, 이동할 수 도 없다.(벽 의미)


따라서 그 부분만 수정해서 문제를 푼다면 쉽게 해결할 수 있는 문제이다.


설명은 1788번 비숍 문제의 링크를 통해 더 자세하게 공부할 수 있으면 좋겠다.






소스 코드 : 


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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <iostream>
#include <cstdio>
#include <vector>
#include <memory.h>
 
#define max(a,b)(a > b ? a : b)
#define MAX_N 102
#define MAX_V 1002
using namespace std;
 
vector<int> adj[MAX_V];
vector<int> aMatch;
vector<int> bMatch;
 
int map[MAX_N][MAX_N];
int visit[MAX_V];
int visitCnt;
 
int Left[MAX_N][MAX_N];
int Right[MAX_N][MAX_N];
 
int n, m;
 
void fillLeft();
void fillRight();
void checkLeftandRight();
 
bool dfs(int a)
{
    if (visit[a] == visitCnt)
        return false;
 
    visit[a] = visitCnt;
 
    for (int next = 0; next < adj[a].size(); next++)
    {
        int b = adj[a][next];
 
        if (bMatch[b] == -|| dfs(bMatch[b]))
        {
            aMatch[a] = b;
            bMatch[b] = a;
 
            return true;
        }
    }
 
    return false;
}
 
int bipartiteMatch()
{
    aMatch = vector<int>(n + 1-1);
    bMatch = vector<int>(m + 1-1);
 
    int size = 0;
 
    for (int a = 1; a <= n; a++)
    {
        visitCnt++;
        size += dfs(a);
    }
 
    return size;
}
 
int main()
{
    scanf("%d"&n);
 
    int t;
    scanf("%d"&t);
    
    // 영역에 포함되지 않는 부분은 -1로 초기화
    memset(map, -1sizeof(map));
 
    // 영역을 모두 1로 초기화
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            map[i][j] = 1;
 
    // 벽을 모두 0으로 갱신
    for (int i = 0; i < t; i++)
    {
        int y, x;
        scanf("%d %d"&y, &x);
        map[y][x] = 0;
    }
    fillLeft();
    fillRight();
    // checkLeftandRight();
 
    // 1일때만 간선 연결(0일때는 벽이니)
    int maxL = 0, maxR = 0;
 
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (map[i][j] == 1)
            {
                adj[Left[i][j]].push_back(Right[i][j]);
 
                maxL = max(maxL, Left[i][j]);
                maxR = max(maxR, Right[i][j]);
            }
 
    // 이분매칭에 이용 될 n과 m은 maxL, maxR로 교체된다.
    n = maxL;
    m = maxR;    
 
    int get = bipartiteMatch();
    printf("%d", get);
 
    return 0;
}
 
 
void fillLeft()
{
    int y = 1;
    int x = n;
    int cnt = 1;
 
    // 오른쪽 위 시작 기준 대각선은 총 n번 반복
    for (int k = 0; k < 2*n-1; k++)
    {
        int tmpy = y;
        int tmpx = x;
        bool chk = false;
        
        while (tmpy <= n)
        {
            if(map[tmpy][tmpx] != -1)
            {
                if (map[tmpy][tmpx] == 1)
                {
                    Left[tmpy][tmpx] = cnt;
                    chk = true;
                }
                else
                    if (chk)
                    {
                        cnt++;
                        chk = false;
                    }                    
            }        
 
            tmpy++;
            tmpx++;
        }
 
        if (chk)
        {
            cnt++;
            chk = false;
        }
 
        x--;
    }
}
 
void fillRight()
{
    int y = n;
    int x = n;
    int cnt = 1;
 
    // 오른쪽 아래 시작 기준 대각선은 총 n번 반복
    for (int k = 0; k < 2*n-1; k++)
    {
        int tmpy = y;
        int tmpx = x;
        bool chk = false;
 
        while (tmpy > 0)
        {
            if(map[tmpy][tmpx] != -1)
            { 
                if (map[tmpy][tmpx] == 1)
                {
                    Right[tmpy][tmpx] = cnt;
                    chk = true;
                }
                else
                    if (chk)
                    {
                        cnt++;
                        chk = false;
                    }
            }
 
            tmpy--;
            tmpx++;
        }
        if (chk)
        {
            cnt++;
            chk = false;
        }
 
        x--;
    }
}
 
void checkLeftandRight()
{
    cout << endl;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            cout << Left[i][j] << " ";
        cout << endl;
    }
 
    cout << endl;
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            cout << Right[i][j] << " ";
        cout << endl;
    }
 
    cout << endl;
 
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
            cout << map[i][j] << " ";
        cout << endl;
    }
}
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus


반응형

'Applied > 알고리즘 문제풀이' 카테고리의 다른 글

[14492번] 부울행렬의 부울곱  (0) 2017.04.17
[1560번] 비숍  (0) 2017.04.16
[1799번] 비숍  (7) 2017.04.15
[9525번] 룩 배치하기  (0) 2017.04.15
[11444번] 피보나치 수 6  (0) 2017.04.15