[2010山东省第一届ACM大学生程序设计竞赛]——Balloons

2019-04-14 17:31发布

Balloons

题目描述
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 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
简单DFS,这道题刚开始还没咋看懂,后来才醒悟就是两个DFS, 这个和杭电油田那题目类似,前面的搜索找的四个方向的,后面的找的是八个方向的。。。 详情可见: http://blog.csdn.net/lttree/article/details/19908339(八个方向的)
#include using namespace std; char map1[101][101]; char map2[101][101]; int n,dis[4][2]={0,1,0,-1,1,0,-1,0}; int dis2[8][2]={-1,-1,-1,0,-1,1,0,-1,0,1,1,1,1,0,1,-1}; bool judge(int x,int y,int flag) { if(x<0 || y<0 || x>=n || y>=n) return 0; if(flag==1) { if(map1[x][y]=='0') return 0; } else { if(map2[x][y]=='0') return 0; } return 1; } void dfs1(int x,int y) { map1[x][y]='0'; int i,xx,yy; for(i=0;i<4;++i) { xx=x+dis[i][0]; yy=y+dis[i][1]; if(judge(xx,yy,1)) { dfs1(xx,yy); } } return; } void dfs2(int x,int y) { map2[x][y]='0'; int i,xx,yy; for(i=0;i<8;++i) { xx=x+dis2[i][0]; yy=y+dis2[i][1]; if(judge(xx,yy,2)) { dfs2(xx,yy); } } return; } int main() { int test,sum1,sum2; int i,j; test=1; while(cin>>n && n) { for(i=0;i>map1[i][j]; map2[i][j]=map1[i][j]; } sum1=0; for(i=0;i