Title Description
There is only one digital 0 and 1 composed of n- × n-maze grid. If you are in a grid on a 0, then you can move to an adjacent four cells in a cell 1, if you are in a same grid 1, then you can move to an adjacent 4 a lattice grid of 0 on the .
Your task is: for a given maze inquiry can begin to move from one cell to the number of grid (including its own).
Input Format
The first 1 Behavior two positive integers n-, m.
Here n lines of n characters, characters can only be 0 or 1, with no spaces between characters.
Subsequently m rows, each row 2 , separated by a space positive integers i, j, corresponding to the first maze i row j a column of the grid, this query initiate movable from cell to cell number.
Output Format
m rows for each query output corresponding answers.
Sample input and output
2 2 01 10 1 1 2 2
4 4
Description / Tips
All the grid up to each other.
For % . 1 0 0 % data, n≤1000, m≤100000 n- ≤ . 1 0 0 0 , m ≤ . 1 0 0 0 0 0.
Problem-solving ideas: watching the topic is obviously search, but a direct bfs began to write a T, and thought for a moment found, in fact, in the same communication block, so to communicate directly block number, recorded at each point in a few numbers connectivity block, the size of the array recall opening maxn * maxn, consider a case where each communication block.
bfs version, a little slower
#include <iostream> #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e3+3; int dir[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};//上下左右 int vis[maxn][maxn],a[maxn][maxn],in[maxn][maxn],sum[maxn*maxn+10]; int n,m,ans=1; struct node { int x,y; }; inline void bfs(int x,int y,int k) { ans=1; node s; s.y=y,s.x=x; vis[x][y]=1; queue <node> Q; Q.push(s); while (!Q.empty()) { node now=Q.front(); Q.pop(); in[now.x][now.y]=k; for (int i=0; i<4; i++) { node ne; ne.x=now.x+dir[i][0]; ne.y=now.y+dir[i][1]; if (ne.x>=1&&ne.x<=n&&ne.y>=1&&ne.y<=n&&!vis[ne.x][ne.y]&&a[ne.x][ne.y]!=a[now.x][now.y]) { vis[ne.x][ne.y]=1; ans++; Q.push(ne); } } } sum[k]=ans; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { scanf("%1d",&a[i][j]); } } int k=0; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if(in[i][j]==0) { bfs(i,j,++k); } } } for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",sum[in[x][y]]); } return 0; }
dfs version, simple and easy to write, fast
#include <iostream> #include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 1e3+3; int dir[4][2]= {{0,1},{0,-1},{-1,0},{1,0}};//上下左右 int vis[maxn][maxn],a[maxn][maxn],in[maxn][maxn],sum[maxn*maxn+10]; int n,m,ans=0; struct node { int x,y; }; inline void dfs(int x,int y,int k) { in[x][y]=k; vis[x][y]=1; ans++; for (int i=0;i<4;i++) { int nx=x+dir[i][0],ny=y+dir[i][1]; if (nx>=1&&nx<=n&&ny>=1&&ny<=n&&!vis[nx][ny]&&a[nx][ny]!=a[x][y]) dfs(nx,ny,k); } sum[k]=ans; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { scanf("%1d",&a[i][j]); } } int k=0; for (int i=1;i<=n;i++) { for (int j=1;j<=n;j++) { if(in[i][j]==0) { dfs(i,j,++k); ans=0; } } } for (int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); printf("%d\n",sum[in[x][y]]); } return 0; }