标签:nbsp while -- ble ring class highlight ret tps
https://www.luogu.org/problem/show?pid=1141
题意:n*n地图,0可以走到相邻的某个1上,1可以走到相邻的某个0上,m次询问,问(x,y)能走到多少个格子?
n<=1e3,m<=1e5.若点a能到b,则b也能到a.显然a,b能到达的点相同,同一个联通块内的ans是相同的,即求点所在的连通块大小
#include <bits/stdc++.h> using namespace std; const int N=2e3+20; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; int vis[N][N]; int n,mx,ans[N*N],cnt;//ans[i]第i块答案,连通块编号 string s[N]; int dfs(int x,int y,int cur) { vis[x][y]=cnt; mx++; int p=1-cur; for(int i=0;i<4;i++) { int a=x+dx[i]; int b=y+dy[i]; if(a>=0&&a<n&&b>=0&&b<n&&s[a][b]-‘0‘==p&&!vis[a][b]) dfs(a,b,s[a][b]-‘0‘); } } int main() { int m; while(cin>>n>>m) { cnt=0; memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) cin>>s[i]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { if(!vis[i][j]) { mx=0; cnt++; dfs(i,j,s[i][j]-‘0‘); ans[cnt]=mx; } } } while(m--) { int x,y; cin>>x>>y; x--,y--; cout<<ans[vis[x][y]]<<endl; } } return 0; }
标签:nbsp while -- ble ring class highlight ret tps
原文地址:http://www.cnblogs.com/HIKARI1149/p/6655881.html