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. [Elasticsearch] 向已存在的索引中加入自己定义filter/analyzer

    问题描写叙述 随着应用的不断升级,索引中的类型也会越来越多,新添加的类型中势必会使用到一些自己定义的Analyzer.可是通过_settings端点的更新API不能直接在已经存在的索引上使用. 在se ...

  2. windows设置文件夹显示缩略图

      windows设置文件夹显示缩略图 CreateTime--2017年7月26日16:32:59Author:Marydon 为什么要显示缩略图? a.显示缩略图后,图片文件能够直接显示内容,不能 ...

  3. pdf+iphone+wechat

    可能很多人要问,为啥标题取这个名字. 因为今天在这个上面踩了太多坑.. 我们的需求其实很简单.做一个页面,把pdf文档嵌进去,在线显示. 如此需求,放在PC上chrome浏览器,一个embed标签就搞 ...

  4. Solr之困

    http://www.kafka0102.com/2010/08/319.html重写公司的站内搜索.经过前期一段时间对lucene和solr的熟悉,最后决定使用Solr作为新系统的基础框架.现在已经 ...

  5. JSON传参

    通过javascript将数据组织成json格式,然后传到java后台. 注意:前台json数组传参到后台时候需要将对象(json或json数组)转换成字符串(字符串数组). Simple: 1.前台 ...

  6. C#多枚举值的写法与读法

    首先,定义枚举的时候必须是2,4,8,16这种2的次方的值. using System; using System.Collections.Generic; using System.Linq; us ...

  7. 各种波形文件vcd,vpd,shm,fsdb生成的方法(zz)

    仿真是IC设计不可或缺的重要步骤,仿真后一般需要记录下波形文件,用于做详细分析和研究.说一下几种波形文件WLF(Wave Log File).VCD(Value Change Dump)文件,fsdb ...

  8. Memcached安装以及PHP的调用

    Memcached安装以及PHP的调用 [南京·10月17日]OSC源创会开始报名:Swift.大型移动项目构架分享 » 一:安装libevent 由于memcached安装时,需要使用libeven ...

  9. apue编程之参考df代码写的一个简单的df命令的源代码

    代码: #include <stdio.h> #include <mntent.h> #include <string.h> #include <sys/vf ...

  10. 0067 MySQL的日期字段的取值用单引号

    这两天在做sql练习题http://www.cnblogs.com/zxx193/p/4000467.html的时候,涉及到下面的建表+插数据操作 CREATE TABLE t1( s_no VARC ...