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. LDAP编辑器 LDAPAdmin

    LDAPAdmin 是一个在 Windows 用来编辑 LDAP 账户信息的管理工具,采用 Delphi 开发.

  2. 【Redis】redis开机自启动、设置守护进程、密码设置、访问权限控制等安全设置(redis默认端口6379)

    一.redis设置开机自启动:centOS: 1.修改redis.conf中daemonize为yes,确保守护进程开启,也就是在后台可以运行. (守护进程:孤儿进程:独立于终端而存在的进程,不会因为 ...

  3. 常见URL字符及URL编码值

    大家上网的时候一定会看到很多这类情况有的网页地址都是%22%32%11%23%21等这种机器语言恐怕只有机器能马上辨认吧现在我把大概知道的总结一下 字符                         ...

  4. sql 百分比

    select [city], bfb=cast(cast(count(*)*100./(select count(*) from [UserBasicInfo]) as decimal(10,0)) ...

  5. 网页字体生成工具fontello firefox下无效,未跨域,研究两天得出解决办法

    fontello是一个非常好的web font生成工具,但是在使用过程中发现生成的字体在firefox下死活渲染不出来,只有chrome可以正常渲染,字体文件和页面在同域下. 试过各种办法,最后发现一 ...

  6. 使用while循环和伪列的存储过程

    使用while循环和伪列的存储过程如下: USE [JointFrame2] GO /****** Object: StoredProcedure [dbo].[Proc_enterprise_uni ...

  7. Atitit.软件GUI按钮与仪表盘(01)--js区-----js格式化的使用

    Atitit.软件GUI按钮与仪表盘(01)--js区-----js格式化的使用 1. Chrome36( recomm) DEV TOOL>SOURCE> DSWEIHAMYAR  &q ...

  8. Python中的strip()的理解

    在看到Python中strip的时候产生了疑问 strip() 用于移除字符串头尾指定的字符(默认为空格) 开始测试: >>> s = 'ncy_123.python' >&g ...

  9. vue2 vue-router2 webpack1

    转自:http://www.qinshenxue.com/article/20161106163608.html: 项目地址:https://github.com/qinshenxue/vue2-vu ...

  10. CSS3边框圆角知识

    <div class="item" data-brief="整圆"> <div class="border-radius" ...