sdutoj 2152 Balloons
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 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 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.
输出
示例输入
5
11001
00100
11111
11010
10010 0
示例输出
Case 1: 3 2
提示
来源
示例程序
分析:
题意:(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的更多相关文章
- sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...
- Balloons(山东省第一届ACM省赛)
Balloons Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Both Saya and Kudo like balloons ...
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] Burst Balloons 打气球游戏
Given n balloons, indexed from 0 to n-1. Each balloon is painted with a number on it represented by ...
- 第一届山东省ACM——Balloons(java)
Description Both Saya and Kudo like balloons. One day, they heard that in the central park, there wi ...
- BZOJ 2152 & 点分治
Description: 只是打法法塔前测试一下板子 Code: /*================================= # Created time: 2016-04-20 14:3 ...
- [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 ...
- BZOJ 2152: 聪聪可可 树分治
2152: 聪聪可可 Description 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...
- Code[VS] 2152 滑雪题解
Code[VS] 2152 滑雪题解 题目描述 Description trs喜欢滑雪.他来到了一个滑雪场,这个滑雪场是一个矩形,为了简便,我们用r行c列的矩阵来表示每块地形.为了得到更快的速度,滑行 ...
随机推荐
- ubuntu下各个软件完全卸载
1.卸载mysql sudo rm /var/lib/mysql/ -R删除mysql的数据文件2sudo rm /etc/mysql/ -R删除mqsql的配置文件3sudo apt-get aut ...
- java JDK8 学习笔记——第18章 自定义泛型、枚举与注释
第十八章 自定义泛型.枚举与注释 18.1 自定义泛型 泛型定义: (1)仅定义在方法上的泛型语法 (2)用来限制泛型可用类型的extends与super关键字(3)?类型通配字符的使用 18.1.1 ...
- Unicode与UTF8相互转化(使用MultiByteToWideChar)
1.简述 最近在发送网络请求时遇到了中文字符乱码的问题,在代码中调试字符正常,用抓包工具抓的包中文字符显示正常,就是发送到服务器就显示乱码了,那就要将客户端和服务器设置统一的编码(UTF-8),而我们 ...
- 使用多种客户端消费WCF RestFul服务(二)——.net4.0篇
.net 4.0篇 在.net 4.0下面微软并没有提供类似Net.Http的Rest访问组件,而是在codeplex上面提供的WCF REST Starter Kit Preview 2 里面可以找 ...
- css3 transition属性变化与animation动画的相似性以及不同点
下面列子中的2个图片的效果. http://zqtest.e-horse.cn/DongXueImportedCar/assets/mouseOverAnimate.html 第一个为transiti ...
- NuGet的几个小技巧
因为是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接有时候会失效,所以 ...
- 转:【工欲善其事必先利其器】—Entity Framework实例详解
开始本篇文章之前,先说一下Entity Framework 6 Alpha1在NuGet中已可用,原文链接http://blogs.msdn.com/b/adonet/archive/2012/10/ ...
- PresentViewController切换界面(一些系统自带的页面切换动画)
视图切换,没有NavigationController的情况下,一般会使用presentViewController来切换视图并携带切换时的动画, 其中切换方法如下: – presentViewCon ...
- git还原成某个点
1.本地 git reset --hard commit值 git push -f // 强制同步到git库(git服务器) 2.项目所在线上服务器 git reset --ha ...
- 为什么一个object_id在dba_objects中为什么查不到记录?
SQL> drop table test purge;SQL> create table test (id int,comments CLOB); SQL> select INDEX ...