Description

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?

Input

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.

Output

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.

Sample Input

5 11001 00100 11111 11010 10010 0

Sample Output

Case 1: 3 2

HINT

 

Source

山东第一届省赛



题目链接:http://acm.upc.edu.cn/problem.php?id=1929

这道题不是很难,可能初次做的同学会觉得无从下手,思路很简单

   第一步:输入
     第二部:对输入的数据加墙,即把输入的数据用0括起来;假设输入

                  1 1 0 0 1
                  0 0 1 0 0
                  1 1 1 1 1
                  1 1 0 1 0
                  1 0 0 1 0

             加墙:

                0 0 0 0 0 0 0
                0 1 1 0 0 1 0
                0 0 0 1 0 0 0
                0 1 1 1 1 1 0
                0 1 1 0 1 0 0
                0 1 0 0 1 0 0
                0 0 0 0 0 0 0

    第三步:定义两个标志位数组a[n][n],b[n][n],分别对应第一种搜索方式,和第二种搜索方式

第四步:对于第一种搜索方式:上下左右四个方向搜索,跳出条件是“无路可走”或者“标志位为1”(代表已经搜索过)——if(a[x][y]==0||b[x][y]==1){return ;}

        对于第二种搜索方式:八个方向搜索,就跳出条件与第一种一致

代码:

import java.util.Scanner;
public class Main {
public static int a[][];
public static int b[][];
public static int c[][];
public static void dfs1(int x,int y){
if(a[x][y]==0||b[x][y]==1){
return ;
}
b[x][y]=1;
dfs1(x,y+1);
dfs1(x,y-1);
dfs1(x-1,y);
dfs1(x+1,y); }
public static void dfs2(int x,int y){
if(a[x][y]==0||c[x][y]==1){
return ;
}
c[x][y]=1;
dfs2(x,y+1);
dfs2(x,y-1);
dfs2(x-1,y);
dfs2(x+1,y);
dfs2(x+1,y+1);
dfs2(x-1,y-1);
dfs2(x-1,y+1);
dfs2(x+1,y-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int d=1;
while(sc.hasNextInt()){ int t=sc.nextInt();
if(t==0)
break;
a=new int[t+2][t+2];
b=new int[t+2][t+2];
c=new int[t+2][t+2];
for(int i=1;i<t+1;i++){
String s=sc.next();
for(int j=1;j<t+1;j++){
a[i][j]=s.charAt(j-1)-'0';
}
}
for(int i=0;i<t+2;i++){
for(int j=0;j<t+2;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
int con1=0,con2=0;
for(int i=1;i<t+1;i++){
for(int j=1;j<t+1;j++){
if(a[i][j]!=0&&b[i][j]==0){
con1++;
dfs1(i,j);
}
}
}
for(int i=1;i<t+1;i++){
for(int j=1;j<t+1;j++){
if(a[i][j]!=0&&c[i][j]==0){
con2++;
dfs2(i,j);
}
}
}
System.out.println("Case "+d+": "+con1+" "+con2);
System.out.println();
d++;
}
}
}

 

第一届山东省ACM——Balloons(java)的更多相关文章

  1. 第一届山东省ACM——Phone Number(java)

    Description We know that if a phone number A is another phone number B’s prefix, B is not able to be ...

  2. sdut 2152:Balloons(第一届山东省省赛原题,DFS搜索)

    Balloons Time Limit: 1000MS Memory limit: 65536K 题目描述 Both Saya and Kudo like balloons. One day, the ...

  3. sdut 2159:Ivan comes again!(第一届山东省省赛原题,STL之set使用)

    Ivan comes again! Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 The Fairy Ivan gave Say ...

  4. 第七届山东省ACM省赛

    激动人心的省赛终于结束了…平静下来再回头看真的感觉一波三折…先是赛前毫无预兆的查出突发性耳聋…伴随而来的就是左耳听力下降.轻微耳鸣.极个别情况下的头晕…不过这都还好,毕竟药物可以恢复…热身赛只过了一道 ...

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

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

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

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

  7. sdut 2158:Hello World!(第一届山东省省赛原题,水题,穷举)

    Hello World! Time Limit: 1000MS Memory limit: 65536K 题目描述 We know that Ivan gives Saya three problem ...

  8. 第十届山东省acm省赛补题(1)

    今天第一场个人训练赛的题目有点恐怖啊,我看了半个小时多硬是一道都不会写.我干脆就直接补题去了.... 先补的都是简单题,难题等我这周末慢慢来吧... A Calandar Time Limit: 1 ...

  9. 2018年第九届山东省ACM省赛总结

    去年打完区域赛之后,面对着两个队友都去找实习的情况,我自己对今年省赛还是有点慌的.不只一次的像我的队友说明自己很慌,但是老曹跟会长都说:“没事,慌啥!”前几场训练赛因为老曹跟秋洁有面试有时候只能一个人 ...

随机推荐

  1. 5-4 bash脚本编程之三 条件判断及算术运算

    1. 反引号是引用执行结果,并非是返回值 如下是错误的,结果是一行行记录,不是返回值 放大为: 练习 2. shell中如何进行算术运算 A=3 B=4 1. let算术运算表达式 2. $[算术运算 ...

  2. [原创]下拉框控件react-native-modal-dropdown更新历程

    前言 不知不觉从今年9月发布第一版控件到现在已经快3个月了 过去不断从开源社区索取,一直一直想着有机会一定要回报

  3. 实例:对2个Makefile的备注

    实例1:Makefile编译链接简单.c函数 example.c Makefile exe: example.c gcc example.c -o exe clean: rm exe 执行效果: 实例 ...

  4. float4数据类型

    GPU是以四维向量为基本单位来计算的.4个浮点数所组成的float4向量是GPU内置的最基本类型.使用GPU对两个float4向量进行计算,与CPU对两个整数或两个浮点数进行计算一样简单,都是只需要一 ...

  5. Selenium-java-Log4j环境搭建和

    1 导入Log4j ,我这版本是1.2.17  自己选择版本  **别告诉我不会导入 2  Path  奶瓶 3 创建一个与src同目录文件 命名为 log4.properties 4 文件的内容是, ...

  6. [No0000AA]Windows 系统环境变量列表

    环境变量 详细信息 %ALLUSERSPROFILE% 所有用户 Profile 文件位置 %APPDATA% 应用程序数据的默认存放位置 %CD% 当前目录 %CLIENTNAME% 联接到终端服务 ...

  7. iOS CALayer应用详解

    跟着大神一起进步,本篇博客原文地址:http://blog.csdn.net/hello_hwc?viewmode=contents 一 CALayer是什么? Layers是绘图和动画的基础,  L ...

  8. salt源码安装软件和yum安装软件

    上面简单列出了源码安装的sls文件书写思路. 涉及到一些固定的思路:如, 1,拷贝 解压安装时候需要依赖tar.gz存在 如果已安装则无需再次安装. 2,启动脚本 加入chk时候需要文件存在,如果已添 ...

  9. C#代码中实现两个表(DataTable)的关联查询(JOIN)

    之前通常都是使用SQL直接从数据库中取出表1和表2关联查询后的数据,只需要用一个JOIN就可以了,非常方便.近日遇到一种情况,两个表中的数据已经取到代码中,需要在代码中将这两个表关联起来,并得到它们横 ...

  10. DataSet与DataTable与DataRow的关系

    1. 添加数据: DataRow dr=MyDataSet.Tables["User"].NewRow(); dr["UserName"] = "张三 ...