Balloons
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.
They were very interested about this event, and also curious about the image.
Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?
You can assume that the image is an N*N matrix, while each element can be either balloons or blank.
Suppose element A and element B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1, C2, … , Cn, while A and C1 are connected, C1 and C2 are connected …Cn and B are connected.
And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.
To Saya, element A(xa,ya)and B(xb,yb) is adjacent if |xa-xb| + |ya-yb| ≤ 1
But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1 and |ya-yb|≤1
They want to know that there’s how many connected blocks with there own definition of adjacent?
输入
The first line of input in each test case contains one integer N (0<N≤100), which represents the size of the matrix.
Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
The last case is followed by a line containing one zero.
输出
示例输入
5 11001 00100 11111 11010 10010 0
示例输出
Case 1: 3 2
BFS两遍,一遍求4个方向的,一遍求8个方向的
<pre name="code" class="cpp">#include <bits/stdc++.h>
using namespace std;
struct node
{
int x, y;
};
int Dir_f[][2] = {{1,0},{0,1},{0,-1},{-1,0}};
int Dir_e[][2] = {{1,0},{0,1},{-1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
char Map[110][110];
bool vis[110][110];
int n;
void BFS1(int x, int y)
{
node tmp;
queue<node >Q;
tmp.x = x, tmp.y = y;
Q.push(tmp);
vis[x][y] = true;
while(!Q.empty())
{
node t = Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
tmp.x = t.x + Dir_f[i][0];
tmp.y = t.y + Dir_f[i][1];
if(tmp.x >= 0 && tmp.x < n && tmp.y >=0 && tmp.y < n && !vis[tmp.x][tmp.y] && Map[tmp.x][tmp.y] == '1')
{
Q.push(tmp);
vis[tmp.x][tmp.y] = true;
}
}
}
}
void BFS2(int x, int y)
{
node tmp;
queue<node >Q;
tmp.x = x, tmp.y = y;
Q.push(tmp);
vis[x][y] = true;
while(!Q.empty())
{
node t = Q.front();
Q.pop();
for(int i=0; i<8; i++)
{
tmp.x = t.x + Dir_e[i][0];
tmp.y = t.y + Dir_e[i][1];
if(tmp.x >= 0 && tmp.x < n && tmp.y >=0 && tmp.y < n && !vis[tmp.x][tmp.y] && Map[tmp.x][tmp.y] == '1')
{
Q.push(tmp);
vis[tmp.x][tmp.y] = true;
}
}
}
}
int main()
{
int cas = 0;
while(~scanf("%d",&n) && n)
{
memset(Map, 0, sizeof(Map));
memset(vis, false, sizeof(vis));
for(int i=0; i<n; i++)
scanf("%s",Map[i]);
int ans1 = 0;
int ans2 = 0;
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(!vis[i][j])
{
if(Map[i][j] == '1')
{
BFS1(i, j);
ans1++;
}
}
}
}
memset(vis, false, sizeof(vis));
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(!vis[i][j])
{
if(Map[i][j] == '1')
{
BFS2(i, j);
ans2++;
}
}
}
}
printf("Case %d: %d %d\n",++cas, ans1, ans2);
printf("\n");
}
return 0;
}