-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1141.cpp
70 lines (60 loc) · 1.09 KB
/
P1141.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
#include <iostream>
using namespace std;
int N,M;
char haze[1002][1002];
int vis[1002][1002];
int cnt;
int nblocks;
int ans[1000005];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,char c)
{
if(x<1 || x>N || y<1 || y>N)
return;
if(haze[x][y] == c)
return;
if(vis[x][y])
return;
vis[x][y] = cnt;
++nblocks;
for(int i=0; i<4; ++i)
{
dfs(x+dir[i][0], y+dir[i][1], haze[x][y]);
}
}
void process()
{
for(int i=1; i<=N; ++i)
{
for(int j=1; j<=N; ++j)
{
if(!vis[i][j])
{
++cnt;
nblocks = 0;
dfs(i,j,'1' + '0' - haze[i][j]);
ans[cnt] = nblocks;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin >> N >> M;
for(int i=1;i<=N;++i)
{
for(int j=1; j<=N;++j)
{
cin >> haze[i][j];
}
}
process();
for(int i=0; i<M; ++i)
{
int x,y;
cin >> x >> y;
int type = vis[x][y];
cout << ans[type] << endl;
}
}