-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa11094.cpp
64 lines (61 loc) · 1.33 KB
/
UVa11094.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
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// shortcuts for "common" data types in contests
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
int dr[] = {1, 0, -1, 0};
int dc[] = {0, 1, 0, -1};
vector<vector<char>> grid;
int R, C;
int floodfill(int r, int c, char c1, char c2)
{
if (r < 0 || r >= R || c < 0 || c >= C) return 0;
if (grid.at(r).at(c) != c1) return 0;
int ans = 1;
grid.at(r).at(c) = c2;
for (int i = 0; i < 4; i++)
{
int toR = r + dr[i];
int toC = (C + c + dc[i]) % C;
ans += floodfill(toR, toC, c1, c2);
}
return ans;
}
int main()
{
while (scanf("%d %d\n", &R, &C) != EOF)
{
// printf("%d %d\n", R, C);
grid.clear();
for (int i = 0 ; i < R; ++i)
{
char* line = new char[C]; scanf("%s\n", line);
// printf("%s\n", line);
grid.push_back(vector<char>(line, line + C));
delete[] line;
}
int x, y; scanf("%d %d\n\n", &x, &y);
// printf("%d %d\n", x, y);
char land = grid.at(x).at(y);
floodfill(x, y, land, '\0');
int largest = 0;
for (int i = 0; i < R; ++i)
{
for (int j = 0; j < C; ++j)
{
char c = grid.at(i).at(j);
if (c == land)
largest = max(largest, floodfill(i, j, land, '\0'));
}
}
printf("%d\n", largest);
}
return 0;
}