-
Notifications
You must be signed in to change notification settings - Fork 0
/
2246.cpp
68 lines (53 loc) · 1023 Bytes
/
2246.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
//tiles
#include <bits/stdc++.h>
using namespace std;
#define Y first
#define X second
typedef pair<int, int> ii;
int mosaic[201][201];
int area = INT_MAX;
int floodfill(int y, int x, int row, int col)
{
int area = 0;
int mx[] = {0, 1, 0, -1};
int my[] = {1, 0, -1, 0};
queue<ii> next;
next.push(ii(y, x));
int cur = mosaic[y][x];
while(!next.empty())
{
ii u = next.front();
next.pop();
for(int adj = 0; adj < 4; adj++)
{
int i = u.Y + my[adj];
int j = u.X + mx[adj];
if(i >= 0 && j >= 0 && i < row &&
j < col && mosaic[i][j] == cur)
{
mosaic[i][j] = 4;
area++;
next.push(ii(i, j));
}
}
}
return area;
}
int main()
{
int row, col;
cin >> row >> col;
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
cin >> mosaic[i][j];
for(int i = 0; i < row; i++)
for(int j = 0; j < col; j++)
{
if(mosaic[i][j] != 4)
{
int x = floodfill(i, j, row, col);
if(x < area) area = x;
}
}
printf("%d\n", area ? area : 1);
}