http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2152

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 and element B are both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of element C1C2, … , 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大学生程序设计竞赛

示例程序

分析:

题意:(1)求图中四连块(有公共边的方块)的个数;

(2)求图中八连块(有公共顶点的方块)的个数。

这道题和nyist 上的水池数目 类似。直接dfs。

AC代码:

 #include<stdio.h>
#include<string.h>
const int N = ;
int vis1[N][N],map[N][N],vis[N][N];
void dfs1(int x,int y)//搜索四个方向
{
if (!map[x][y]||vis1[x][y])
return ;
vis1[x][y] = ;
dfs1(x-,y);
dfs1(x+,y);
dfs1(x,y-);
dfs1(x,y+);
}
void dfs(int x,int y)//搜索八个方向
{
if(!map[x][y]||vis[x][y])
return ;
vis[x][y] =;
dfs(x-,y-);dfs(x-,y);dfs(x-,y+);
dfs(x,y-); dfs(x,y+);
dfs(x+,y-);dfs(x+,y);dfs(x+,y+); }
int main()
{
int n,i,j,o = ;
char s[N];
while(~scanf("%d",&n)&&n)
{
++o;
int cnt1 = ;
int cnt2 = ;
memset(vis1,,sizeof(vis));
memset(vis,,sizeof(vis));
memset(map,,sizeof(map));
for (i = ; i < n; i ++)
{
scanf("%s",s);
for (j = ; j < n; j ++)
{
map[i+][j+] = s[j]-'';//在图周围加一圈空格,防止判断时越界
}
}
for (i = ; i <= n; i ++)
{
for (j = ; j <= n; j ++)
{
if (map[i][j]&&!vis1[i][j])
{
cnt1++;
dfs1(i,j); }
if (map[i][j]&&!vis[i][j])
{
cnt2++;
dfs(i,j);
} }
}
printf("Case %d: %d %d\n\n",o,cnt1,cnt2);
} return ;
}

sdutoj 2152 Balloons的更多相关文章

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

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  2. Balloons(山东省第一届ACM省赛)

    Balloons Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Both Saya and Kudo like balloons ...

  3. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  4. [LeetCode] Burst Balloons 打气球游戏

    Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...

  5. 第一届山东省ACM——Balloons(java)

    Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...

  6. BZOJ 2152 & 点分治

    Description: 只是打法法塔前测试一下板子 Code: /*================================= # Created time: 2016-04-20 14:3 ...

  7. [LeetCode] 452 Minimum Number of Arrows to Burst Balloons

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  8. BZOJ 2152: 聪聪可可 树分治

    2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

  9. Code[VS] 2152 滑雪题解

    Code[VS] 2152 滑雪题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行 ...

随机推荐

  1. Hashtable、Dictionary和List 谁效率更高

    一 前言 很少接触HashTable晚上回来简单看了看,然后做一些增加和移除的操作,就想和List 与 Dictionary比较下存数据与取数据的差距,然后便有了如下的一此测试, 当然我测的方法可能不 ...

  2. android 定位的四种方式

    [原文]  开发中对于地图及地理位置的定位是我们经常要用地,地图功能的使用使得我们应用功能更加完善,下面总结了一下网络中现有对于介绍android定位的4种方式,希望对大家有帮助: android 定 ...

  3. Java Map遍历方式的选择

    [原文] 1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keyS ...

  4. 弄清const与指针、引用之间的关系

    const和 define在常量定义上的差别 在C++中,我们可以使用const 或者 宏define来定义常量.但是C++鼓励使用const定义常量,而不是宏define.原因有很多. 1.defi ...

  5. GetLogicalProcessorInformation(XP3才支持)和GetLogicalProcessorInformationEx(WIN7才支持)

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms683194(v=vs.85).aspx 例子执行结果如下: https://ms ...

  6. Unix网络编程(迭代服务器)

    #include <stdio.h> #include <string.h> #include <sys/types.h> #include <sys/soc ...

  7. PushKit和传统长连接方式的比较

    iOS and PushKit This post will cover basic concepts for VoIP apps on iOS. I will not post any code ( ...

  8. svn利用TortoiseSVN忽略文件或文件夹

    忽略已经版本控制的文件 如果你不小心添加了一些应该被忽略的文件,你如何将它们从版本控制中去除而不会丢失它们?或许你有 自己的IDE配置文件,不是项目的一部分,但将会花费很多时间使之按照自己的方式工作. ...

  9. 用代码调用Storyboard里面的viewController

    今天在帮助群里的一个朋友弄pop事件,在他那边,当前的viewcontroller,不能pop出去. 初步估计,他的ViewController层级多,他自己没有理清. 因为pushViewContr ...

  10. cannot open /proc/bus/usb/devices, No such file or directory

    由于kernel config中没有打开对应的配置. make menuconfig 选择: Device Drivers ---> [*] USB support ---> [*] US ...