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 ...
随机推荐
- js 向上取整、向下取整、四舍五入
js 向上取整.向下取整.四舍五入 CreateTime--2018年4月14日11:31:21 Author:Marydon // 1.只保留整数部分(丢弃小数部分) parseInt(5.12 ...
- oracle 存储过程 返回结果集
oracle 存储过程 返回结果集 CreationTime--2018年8月14日09点50分 Author:Marydon 1.情景展示 oracle存储过程如何返回结果集 2.解决方案 最简 ...
- javascript 的继承实例
shape.prototype = { getEdge:function(){ return this.edge; }, getArea:function(){ return this.a*this. ...
- JDBC 关于大文本数据
大文本数据Clob,在不同的数据库中类型名不一致,有的是text格式,有的是clob,还有其他一些格式 package test; import java.io.BufferedReader; i ...
- Swift 与 Object-C 交互 (Swift版本为:1.2)
这篇文章主要是介绍 Swift 与 Object-C 之间进行交互的代码,主要分为两个部分.一个是 Swift 项目调用 Object-C 的类,另一个是 Object-C 项目调用 Swift 类. ...
- 层次选择器[selector_2.html]
层次选择器[selector_2.html] $("form input"):祖先 后代 $("form>input"):父亲>直接小孩 $(&qu ...
- Android—— 定制界面风格
统一的用户界面是可以使得应用程序更友好.要做到用户界面的统一,我们就必须用到风格(style)和主题(theme).OPhone系统提供了很多系统默认的风格和主题,但是很多情况下,这些不能满足我们的需 ...
- Axure 蚂蚁设计团队组件库 让交互稿美美"搭"
Github资源:https://github.com/ant-design/ant-design-pro English | 简体中文 技术实践篇 https://pro.ant.design/do ...
- Atitit. 状态模式(State)attilax 总结 跟个策 略模式的区别
Atitit. 状态模式(State)attilax 总结 跟个策 略模式的区别 1. 状态模式(State)概览 1 2. 状态的维护和转换:① 在Context 中.② 在状态的处理类中.2 3. ...
- vue的路由使用
1). 安装 vue-router npm install vue-router --save 2). 新建路由配置 安装成功后,在 src 新建 router 文件夹,然后新建 index.js 文 ...