sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)
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
提示
来源
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搜索)的更多相关文章
- sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)
Ivan comes again! Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...
- sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)
Clockwise Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...
- sdut 2154:Shopping(第一届山东省省赛原题,水题)
Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together.You can ass ...
- sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)
Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...
- sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)
The Android University ACM Team Selection Contest Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里 ...
- sdut 2163:Identifiers(第二届山东省省赛原题,水题)
Identifiers Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Identifier is an important c ...
- sdut 2165:Crack Mathmen(第二届山东省省赛原题,数论)
Crack Mathmen Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Since mathmen take securit ...
- Rectangles(第七届ACM省赛原题+最长上升子序列)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=1255 描述 Given N (4 <= N <= 100) rec ...
- sdut 2610:Boring Counting(第四届山东省省赛原题,划分树 + 二分)
Boring Counting Time Limit: 3000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 In this problem you a ...
随机推荐
- [转发]using的几种用法
1.using指令.using + 命名空间名字,这样可以在程序中直接用命令空间中的类型,而不必指定类型的详细命名空间,类似于Java的import,这个功能也是最常用的,几乎每个cs的程序都会用到. ...
- yum groupinstall报错,处理方法
http://www.cnblogs.com/xiaoluo501395377/archive/2013/05/21/3089970.html ===== 创建repo库 # createrepo - ...
- xUtils中用DbUtils,ViewUtils的用法
一.有关xUtils的简介 xUtils 包含了很多实用的android工具.xUtils 最初源于Afinal框架,进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持(10 ...
- 【转帖】基于Zookeeper的服务注册与发现
http://www.techweb.com.cn/network/hardware/2015-12-25/2246973.shtml 背景 大多数系统都是从一个单一系统开始起步的,随着公司业务的快速 ...
- 【LeetCode】101. Symmetric Tree (2 solutions)
Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...
- 视频播放器控制原理:ffmpeg之ffplay播放器源代码分析
版权声明:本文由张坤原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/535574001486630869 来源:腾云阁 ht ...
- freemarker,*.ftl文件在eclipse中正确的打开方式
解决方式一:下载eclipse相关的freemarker插件 解决方式二:本文重点介绍的,原生解决方式,方法特点无需下载插件,和eclipse编辑html和jsp文件一模一样 步骤: 1:window ...
- C#:TextBox控件操作类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- nyoj 37 回文字符串 【DP】
先反向复制一个新的字符串,然后再找出最长公共子串,在用长度n减去就可以 回文字符串 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描写叙述 所谓回文字符串,就是一个字符串,从 ...
- QQ窗体的控制,同步异步打开360网盘,控制360网盘窗体的移动
1.通过system启动飞秋进程的方式: 2.Windows下杀死进程的方式是:taskkill /f/im QQ.exe.截图例如以下: watermark/2/text/aHR0cDovL2 ...