日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

發(fā)布時間:2024/1/17 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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 100100

示例輸出

Case 1: 3 2

提示

來源

2010年山東省第一屆ACM大學(xué)生程序設(shè)計競賽

?

  DFS搜索,求連通分量的個數(shù)。需要注意的是第一個只能是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

轉(zhuǎn)載于:https://www.cnblogs.com/yym2013/p/3662190.html

總結(jié)

以上是生活随笔為你收集整理的sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。