sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
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<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.
輸出
?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 100100示例輸出
Case 1: 3 2提示
來源
2010年山東省第一屆ACM大學生程序設計競賽?
DFS搜索,求連通分量的個數。需要注意的是第一個只能是4個方向,而第二個可以有8個方向,即可以斜著走。
代碼:
1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 int n; 5 int dx[4] = {0,-1,0,1}; 6 int dy[4] = {-1,0,1,0}; 7 int Dx[8] = {0,-1,-1,-1,0,1,1,1}; 8 int Dy[8] = {-1,-1,0,1,1,1,0,-1}; 9 10 bool judge(char a[101][101],int x,int y) 11 { 12 if(x<0 || y<0 || x>=n || y>=n) 13 return 1; 14 if(a[x][y]!='1') 15 return 1; 16 return 0; 17 } 18 void dfs1(char a[][101],int x,int y) 19 { 20 a[x][y] = '0'; 21 for(int i=0;i<4;i++){ 22 int cx = x+dx[i]; 23 int cy = y+dy[i]; 24 if(judge(a,cx,cy)) 25 continue; 26 dfs1(a,cx,cy); 27 } 28 } 29 void dfs2(char a[][101],int x,int y) 30 { 31 a[x][y] = '0'; 32 for(int i=0;i<8;i++){ 33 int cx = x+Dx[i]; 34 int cy = y+Dy[i]; 35 if(judge(a,cx,cy)) 36 continue; 37 dfs2(a,cx,cy); 38 } 39 } 40 int main() 41 { 42 int Count=1; 43 while(cin>>n){ 44 if(n==0) break; 45 int num1 = 0,num2 = 0; 46 char a[101][101]; 47 char b[101][101]; 48 for(int i=0;i<n;i++){ 49 cin>>a[i]; 50 } 51 for(int i=0;i<n;i++) 52 for(int j=0;j<n;j++) 53 b[i][j]=a[i][j]; 54 int i,j; 55 for(i=0;i<n;i++) 56 for(j=0;j<n;j++){ 57 if(a[i][j]=='1'){ 58 num1++; 59 dfs1(a,i,j); 60 } 61 } 62 for(i=0;i<n;i++) 63 for(j=0;j<n;j++){ 64 if(b[i][j]=='1'){ 65 num2++; 66 dfs2(b,i,j); 67 } 68 } 69 cout<<"Case "<<Count++<<": "<<num1<<' '<<num2<<endl<<endl; 70 } 71 return 0; 72 }?
Freecode : www.cnblogs.com/yym2013
轉載于:https://www.cnblogs.com/yym2013/p/3662190.html
總結
以上是生活随笔為你收集整理的sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS-字符串太长换行拼接
- 下一篇: HTML页面背景音乐控制