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.

示例输入

  1. 5
  2. 11001
  3. 00100
  4. 11111
  5. 11010
  6. 10010
  7.  
  8. 0

示例输出

  1. Case 1: 3 2

提示

 

来源

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

示例程序

分析:

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

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

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

AC代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. const int N = ;
  4. int vis1[N][N],map[N][N],vis[N][N];
  5. void dfs1(int x,int y)//搜索四个方向
  6. {
  7. if (!map[x][y]||vis1[x][y])
  8. return ;
  9. vis1[x][y] = ;
  10. dfs1(x-,y);
  11. dfs1(x+,y);
  12. dfs1(x,y-);
  13. dfs1(x,y+);
  14. }
  15. void dfs(int x,int y)//搜索八个方向
  16. {
  17. if(!map[x][y]||vis[x][y])
  18. return ;
  19. vis[x][y] =;
  20. dfs(x-,y-);dfs(x-,y);dfs(x-,y+);
  21. dfs(x,y-); dfs(x,y+);
  22. dfs(x+,y-);dfs(x+,y);dfs(x+,y+);
  23.  
  24. }
  25. int main()
  26. {
  27. int n,i,j,o = ;
  28. char s[N];
  29. while(~scanf("%d",&n)&&n)
  30. {
  31. ++o;
  32. int cnt1 = ;
  33. int cnt2 = ;
  34. memset(vis1,,sizeof(vis));
  35. memset(vis,,sizeof(vis));
  36. memset(map,,sizeof(map));
  37. for (i = ; i < n; i ++)
  38. {
  39. scanf("%s",s);
  40. for (j = ; j < n; j ++)
  41. {
  42. map[i+][j+] = s[j]-'';//在图周围加一圈空格,防止判断时越界
  43. }
  44. }
  45. for (i = ; i <= n; i ++)
  46. {
  47. for (j = ; j <= n; j ++)
  48. {
  49. if (map[i][j]&&!vis1[i][j])
  50. {
  51. cnt1++;
  52. dfs1(i,j);
  53.  
  54. }
  55. if (map[i][j]&&!vis[i][j])
  56. {
  57. cnt2++;
  58. dfs(i,j);
  59. }
  60.  
  61. }
  62. }
  63. printf("Case %d: %d %d\n\n",o,cnt1,cnt2);
  64. }
  65.  
  66. return ;
  67. }

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. (转)面试题--JAVA中静态块、静态变量加载顺序详解

    public class Test { //1.第一步,准备加载类 public static void main(String[] args) { new Test(); //4.第四步,new一个 ...

  2. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  3. C# 操作Cookie类

    1.Cookie操作类 using System; using System.Data; using System.Configuration;using System.Web;using Syste ...

  4. myeclipse调式与属性显示

    最近做项目的时候发现一个奇怪的东西,当我用myeclipse调式时,调式窗口显示实体user所关联的role的对象属性是空的,但是,从syst打印出来是有的,最近感到很奇怪,后来发现这只是调式的一种显 ...

  5. Qt系统托盘

    Qt的系统托盘的使用,可比mfc中好多了!他封装了一个专门的QSystemTrayIcon类,建立系统托盘图标.其实在Qt提供的示例程序已经很不错了,$QTDIR\examples\desktop\s ...

  6. 超级管理员登录后如果连续XX分钟没有操作再次操作需要重新登录

    首先在设置session页面时 session_start(); session("name",$adminname); //加入session时间 time() session( ...

  7. ligerui ligerTip气泡提示信息

    onAfterShowData:function(){ $('.l-grid-row-cell-inner').hover(function (){ $(this).ligerTip({content ...

  8. mongo安装、备份与常见命令整理

    http://zlboy888.blog.163.com/blog/static/315357072012919241104/ 1 下载安装包  官方下载地址:http://www.mongodb.o ...

  9. [代码片段]OSTU算法

    用在片上系统上的 //OSTU求图像的阈值 u8 otsuThreshold(u8 **img, u16 height, u16 width) { // int width = frame->w ...

  10. windows server 2008服务器IIS绑定阿里云域名

    一.打开Internet 信息服务(IIS)管理器   二.将你的网站放到服务器目录下,比如D盘下的WWW文件夹.   三.在IIS中,添加网站,网站的物理路径指向第二部中创建的网站.   五.在绑定 ...