File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < bits/stdc++.h>
2+ #define xx first
3+ #define yy second
4+ using namespace std ;
5+ typedef pair<int ,int > pii;
6+
7+ int N,K,R;
8+ bool road[101 ][101 ][4 ]{};
9+ bool m[101 ][101 ]{};
10+ bool vi[101 ][101 ]{};
11+ int dy[4 ] = {-1 ,1 ,0 ,0 };
12+ int dx[4 ] = {0 ,0 ,-1 ,1 };
13+
14+ int bfs (int sty, int stx){
15+ queue<pii> q;
16+ q.push ({sty,stx});
17+ vi[sty][stx] = true ;
18+ int res = 1 ;
19+
20+ while (!q.empty ()){
21+ int cy = q.front ().xx ;
22+ int cx = q.front ().yy ;
23+ q.pop ();
24+
25+ for (int i=0 ; i<4 ; i++){
26+ if (road[cy][cx][i]) continue ;
27+ int ny = cy + dy[i];
28+ int nx = cx + dx[i];
29+ if (ny<=0 || nx<=0 || ny>N || nx>N || vi[ny][nx]) continue ;
30+ q.push ({ny,nx});
31+ vi[ny][nx] = true ;
32+ if (m[ny][nx]) res++;
33+ }
34+ }
35+
36+ return res;
37+ }
38+
39+ int main () {
40+ ios_base::sync_with_stdio (0 );
41+ cin.tie (0 );
42+ cin>>N>>K>>R;
43+ int a,b,x,y;
44+ for (int i=0 ; i<R; i++) {
45+ cin>>b>>a>>y>>x;
46+ int t1 = y-b;
47+ int t2 = x-a;
48+ if (t1==0 ){
49+ if (t2==1 ) {road[b][a][3 ] = true ; road[y][x][2 ] = true ;}
50+ else {road[b][a][2 ] = true ; road[y][x][3 ] = true ;}
51+ } else {
52+ if (t1==1 ) {road[b][a][1 ] = true ; road[y][x][0 ] = true ;}
53+ else {road[b][a][0 ] = true ; road[y][x][1 ] = true ;}
54+ }
55+ }
56+ queue<pii> q;
57+ for (int i=0 ; i<K; i++) {
58+ cin>>a>>b;
59+ m[a][b] = true ;
60+ q.push ({a,b});
61+ }
62+
63+ int ans=0 , sum=0 , tmp;
64+ while (!q.empty ()){
65+ int cy = q.front ().xx ;
66+ int cx = q.front ().yy ;
67+ q.pop ();
68+ if (vi[cy][cx]) continue ;
69+ tmp = bfs (cy, cx);
70+ ans += (sum*tmp);
71+ sum += tmp;
72+ }
73+
74+ cout<<ans;
75+ }
You can’t perform that action at this time.
0 commit comments