sdutoj 2152 Balloons

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2152

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 input consists of several test cases.
The first line of input in each test case contains one integer N (0<N100), 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.

输出

 For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.

示例输入

5
11001
00100
11111
11010
10010

0

示例输出

Case 1: 3 2

提示

 

来源

 2010年山东省第一届ACM大学生程序设计竞赛

示例程序

分析:

题意:(1)求图中四连块(有公共边的方块)的个数;

         (2)求图中八连块(有公共顶点的方块)的个数。

这道题和nyist 上的水池数目 类似。直接dfs。

AC代码:

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int N = 1010;
 4 int vis1[N][N],map[N][N],vis[N][N];
 5 void dfs1(int x,int y)//搜索四个方向
 6 {
 7     if (!map[x][y]||vis1[x][y])
 8         return ;
 9     vis1[x][y] = 1;
10     dfs1(x-1,y);
11     dfs1(x+1,y);
12     dfs1(x,y-1);
13     dfs1(x,y+1);
14 }
15 void dfs(int x,int y)//搜索八个方向
16 {
17     if(!map[x][y]||vis[x][y])
18         return ;
19     vis[x][y] =1;
20     dfs(x-1,y-1);dfs(x-1,y);dfs(x-1,y+1);
21     dfs(x,y-1);             dfs(x,y+1);
22     dfs(x+1,y-1);dfs(x+1,y);dfs(x+1,y+1);
23 
24 }
25 int main()
26 {
27     int n,i,j,o = 0;
28     char s[N];
29     while(~scanf("%d",&n)&&n)
30     {
31         ++o;
32         int cnt1 = 0;
33         int cnt2 = 0;
34         memset(vis1,0,sizeof(vis));
35         memset(vis,0,sizeof(vis));
36         memset(map,0,sizeof(map));
37         for (i = 0; i < n; i ++)
38         {
39             scanf("%s",s);
40             for (j = 0; j < n; j ++)
41             {
42                 map[i+1][j+1] = s[j]-'0';//在图周围加一圈空格,防止判断时越界
43             }
44         }
45         for (i = 1; i <= n; i ++)
46         {
47             for (j = 1; j <= n; j ++)
48             {
49                 if (map[i][j]&&!vis1[i][j])
50                 {
51                     cnt1++;
52                     dfs1(i,j);
53 
54                 }
55                 if (map[i][j]&&!vis[i][j])
56                 {
57                     cnt2++;
58                     dfs(i,j);
59                 }
60 
61             }
62         }
63         printf("Case %d: %d %d

",o,cnt1,cnt2);
64     }

65     return 0;
66 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4468420.html