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
10010 0

示例输出

Case 1: 3 2

提示

 

来源

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

  DFS搜索,求连通分量的个数。需要注意的是第一个只能是4个方向,而第二个可以有8个方向,即可以斜着走。

  代码:

 #include <iostream>
#include <stdio.h>
using namespace std;
int n;
int dx[] = {,-,,};
int dy[] = {-,,,};
int Dx[] = {,-,-,-,,,,};
int Dy[] = {-,-,,,,,,-}; bool judge(char a[][],int x,int y)
{
if(x< || y< || x>=n || y>=n)
return ;
if(a[x][y]!='')
return ;
return ;
}
void dfs1(char a[][],int x,int y)
{
a[x][y] = '';
for(int i=;i<;i++){
int cx = x+dx[i];
int cy = y+dy[i];
if(judge(a,cx,cy))
continue;
dfs1(a,cx,cy);
}
}
void dfs2(char a[][],int x,int y)
{
a[x][y] = '';
for(int i=;i<;i++){
int cx = x+Dx[i];
int cy = y+Dy[i];
if(judge(a,cx,cy))
continue;
dfs2(a,cx,cy);
}
}
int main()
{
int Count=;
while(cin>>n){
if(n==) break;
int num1 = ,num2 = ;
char a[][];
char b[][];
for(int i=;i<n;i++){
cin>>a[i];
}
for(int i=;i<n;i++)
for(int j=;j<n;j++)
b[i][j]=a[i][j];
int i,j;
for(i=;i<n;i++)
for(j=;j<n;j++){
if(a[i][j]==''){
num1++;
dfs1(a,i,j);
}
}
for(i=;i<n;i++)
for(j=;j<n;j++){
if(b[i][j]==''){
num2++;
dfs2(b,i,j);
}
}
cout<<"Case "<<Count++<<": "<<num1<<' '<<num2<<endl<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)的更多相关文章

  1. sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  2. sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)

    Clockwise Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...

  3. sdut 2154:Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...

  4. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  5. sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)

    The Android University ACM Team Selection Contest Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里 ...

  6. sdut 2163:Identifiers(第二届山东省省赛原题,水题)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Identifier is an important c ...

  7. sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)

    Crack Mathmen Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述  Since mathmen take securit ...

  8. Rectangles(第七届ACM省赛原题+最长上升子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1255 描述 Given N (4 <= N <= 100)  rec ...

  9. sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)

    Boring Counting Time Limit: 3000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述     In this problem you a ...

随机推荐

  1. 在mvc4里怎样引用:System.Web.Optimization和entityframework

    请在nuget 里运行: Install-Package Microsoft.AspNet.Web.Optimizationinstall-package entityframework

  2. Ubuntu14.04安装redis-server

    1.update再install操作: sudo apt-get update sudo apt-get install -y redis-server 如果你已经安装了redis,会提示:redis ...

  3. iOS官方文档阅读 基本格式指北

    一些关键词作用 NS_AVAILABLE 表示可用 如 NS_AVAILABLE(NA, 6_0);例如上面这句就是表示 该方法在6.0系统后可用 如果在6.0以下的系统用不了的 或者直接崩溃. NS ...

  4. 为MFC中的ListBox添加水平滚动条

    我们知道,MFC中的水平滚动条并不像垂直滚动条那样的智能.当文字超出ListBox的宽度时,水平滚动条并不会自己出现,我们需要手动的调用CListBox中的函数SetHorizontalExtent设 ...

  5. linux学习规划

  6. 浅谈AngularJS的$parse服务

    $parse 作用:将一个AngularJS表达式转换成一个函数 Usage$parse(expression) arguments expression:需要被编译的AngularJS语句 retu ...

  7. vim资源帖

    vimscript教程 http://learnvimscriptthehardway.stevelosh.com/ 阿信的vimscript http://www.axiaoxin.com/arti ...

  8. queue for max elem, pop, push

    queue for max elem, pop, push 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人 ...

  9. Python @ddt.file_data() 为.yml 文件实例

    一,创建login.yml 文件(以登录接口为例) 1,创建 login.yml 文件,内容如下图: 打印login.yml 文件,代码及显示效果如下: 代码: import yaml,jsonf = ...

  10. 【Java】验证码识别解决方案

    对于类似以下简单的验证码的识别方案: 1. 2 3 4. 1.建库:切割验证码为单个字符,人工标记,比如:A. 2.识别:给一个验证码:切割为单个字符,在库中查询识别. /*** * author:c ...