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
100100

示例输出

Case 1: 3 2

提示

来源

2010年山东省第一届ACM大学生程序设计竞赛

  DFS搜索,求连通分量的个数。需要注意的是第一个只能是4个方向,而第二个可以有8个方向,即可以斜着走。

  代码:

 1 #include <iostream>
 2 #include <stdio.h>
 3 using namespace std;
 4 int n;
 5 int dx[4] = {0,-1,0,1};
 6 int dy[4] = {-1,0,1,0};
 7 int Dx[8] = {0,-1,-1,-1,0,1,1,1};
 8 int Dy[8] = {-1,-1,0,1,1,1,0,-1};
 9
10 bool judge(char a[101][101],int x,int y)
11 {
12     if(x<0 || y<0 || x>=n || y>=n)
13         return 1;
14     if(a[x][y]!='1')
15         return 1;
16     return 0;
17 }
18 void dfs1(char a[][101],int x,int y)
19 {
20     a[x][y] = '0';
21     for(int i=0;i<4;i++){
22         int cx = x+dx[i];
23         int cy = y+dy[i];
24         if(judge(a,cx,cy))
25             continue;
26         dfs1(a,cx,cy);
27     }
28 }
29 void dfs2(char a[][101],int x,int y)
30 {
31     a[x][y] = '0';
32     for(int i=0;i<8;i++){
33         int cx = x+Dx[i];
34         int cy = y+Dy[i];
35         if(judge(a,cx,cy))
36             continue;
37         dfs2(a,cx,cy);
38     }
39 }
40 int main()
41 {
42     int Count=1;
43     while(cin>>n){
44         if(n==0) break;
45         int num1 = 0,num2 = 0;
46         char a[101][101];
47         char b[101][101];
48         for(int i=0;i<n;i++){
49             cin>>a[i];
50         }
51         for(int i=0;i<n;i++)
52             for(int j=0;j<n;j++)
53                 b[i][j]=a[i][j];
54         int i,j;
55         for(i=0;i<n;i++)
56             for(j=0;j<n;j++){
57                 if(a[i][j]=='1'){
58                     num1++;
59                     dfs1(a,i,j);
60                 }
61             }
62         for(i=0;i<n;i++)
63             for(j=0;j<n;j++){
64                 if(b[i][j]=='1'){
65                     num2++;
66                     dfs2(b,i,j);
67                 }
68             }
69         cout<<"Case "<<Count++<<": "<<num1<<' '<<num2<<endl<<endl;
70     }
71     return 0;
72 }

Freecode : www.cnblogs.com/yym2013

转载于:https://www.cnblogs.com/yym2013/p/3662190.html

sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)相关推荐

  1. sdut 2153:Clockwise(第一届山东省省赛原题,计算几何+DP)

    Clockwise Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Saya have a long necklace with ...

  2. sdut 2154:Shopping(第一届山东省省赛原题,水题)

    Shopping Time Limit: 1000MS Memory limit: 65536K 题目描述 Saya and Kudo go shopping together. You can as ...

  3. ACM第二站——2018年第九届山东省省赛

    5月6日:第九届山东省省赛 真正的ACM比赛,怀着无比的期待和满心的激动,终于迎来了2018年的第九届山东省省赛,初出茅庐的我们打响的第一场"战争",就着那一腔的热血,将会随5月6 ...

  4. 武汉工程大学第一届程序设计女生赛(牛客contest 4746)解题报告 Apare_xzc

    武汉工程大学第一届程序设计女生赛解题报告 xzc 2020.3.8 比赛链接:武汉工程大学第一届程序设计女生赛 A. Multiplication (101/861) 分析: 问x平方几次后就会> ...

  5. 第一届华数杯A题完整思路分享

    A题完整思路分享 第一问: 要想问题做的好,美图肯定少不了.这道题,建模时不但要用数学公式把模型说清楚,还要一些图来帮助别人理解,让别人一眼就看懂你写的东西,话不多说,我们先放一张类似下面的图,记得说 ...

  6. 第一届华数杯A题思路分析

    ** 华数杯a题浅见 需要本文的话请加2574364134 ** 当我刚拿到这个题目的时候,惊呆了,这个不就是2018年国赛的A题吗?2018年的国赛A题是为了进行高温防护,这道题现在就是低温防护服御 ...

  7. SDUT 3260 第六届山东省ACM省赛 Single Round Math(数字特征) C语言23行代码搞定~

    传送门:SDUT 3260 题目大意: 给你两个最多 1000 位的十进制数,问它们是否相等并且是 11 的倍数. 思路: 方法1:用 java 或 python 的大数来一发. 方法2: 利用数字倍 ...

  8. 第七届山东省省赛C Proxy(最短路)

    题意: 给出n个点和一些单向边,问从0到n+1 如果不能到则输出-1 如果能一步到则输出0 否则输出第一个到达的节点 如果两条路距离相等,则输出较小的节点 思路: 赛场上从前向后扫然后又向前推的,,, ...

  9. ICPC 山东省省赛刷题 第十七届哈尔滨工程大学ACM程序设计竞赛 牛客 AKE题 补题

    A:As long as I was by her side 题意:连续三个人的高度和是三的倍数,就说这三个人是"挺好的",一组数据进行排序,要求最多"挺好的" ...

最新文章

  1. 一周焦点 | 最强AI芯片麒麟980发布;前端开发者将被取代?
  2. python边缘检测显示原图边缘
  3. mysql5.7命中率_MySQL5.7中 performance和sys schema中的监控参数解释(推荐)
  4. Spring Cloud微服务再谈微服务架构(七)
  5. linux ppp拨号 USB,linux下ppp拨号上网
  6. elasticsearch sort illegal_argument_exception error
  7. 实用新型专利撰写模板(自己总结)
  8. Ignite学习笔记——Ignite的安装与配置
  9. 从辉煌走向消亡(上)——小型机之王DEC公司
  10. LINUX 常见问题1000个详细解答
  11. 无法加载DLL:找不到指定模块 问题解决办法
  12. 【模型开发】评分卡应用
  13. 【大数据计算】(一) HDFS操作方法和基础编程
  14. 每隔三个数加一个逗号,还要考虑小数点的情况
  15. Vue+高德地图API的使用(电子围栏)
  16. python怎么学比较有技巧_怎么学python学的快?学习技巧大分享
  17. 动态规划---例题6.多边形游戏
  18. 《网管员必读》系列丛书获国家级大奖
  19. 阿里云如何去查询域名实名认证的方法?
  20. win10 护眼模式的开启和关闭

热门文章

  1. C#期末考试题,图书系统
  2. 郑州百知面试题 SSM试题三
  3. Bypass WAF实战总结
  4. 批量生成印刷字体字库
  5. wide Deep tensorflow实现
  6. 《Kali Linux 渗透测试技术详解》笔记之 metasploit 学习纪要
  7. C与CPP 在线手册查找
  8. 九章算术卷第五 商功
  9. Unity2D实现贴图凹凸感并接受实时光照效果
  10. ASP.NET4.5Web API及非同步程序开发系列(3)