[2010山东ACM省赛] Balloons(搜索)
2014-04-13 17:47
260 查看
Balloons
Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^
题目描述
BothSaya 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?
输入
Theinput 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.
输出
Foreach 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 10010 0
示例输出
Case 1: 3 2
提示
来源
2010年山东省第一届ACM大学生程序设计竞赛解题思路:
有一段时间没有做搜索题了,这一题花了不少时间,汗。这题属于简单题,两个搜索,一个搜索是四个方向,另一个搜索是八个方向,对应相同的地图,搜索了几次,就有多少个块。
代码:
#include <iostream>
#include <string.h>
using namespace std;
char maze[102][102];
char maze2[102][102];
int n;
int dx[4]={0,0,-1,1};//四个方向
int dy[4]={-1,1,0,0};
int dxx[8]={0,0,-1,1,-1,1,-1,1};//八个方向
int dyy[8]={1,-1,0,0,1,1,-1,-1};
void dfs(int x,int y)
{
if(x<1||x>n||y<1||y>n)
return ;
if(maze[x][y]=='0')
return;
maze[x][y]='0';
for(int i=0;i<4;i++)
{
int a=x+dx[i];//不能写成 x=x+dx[i]
int b=y+dy[i];
dfs(a,b);
}
}
void dfs2(int x,int y)
{
if(x<1||x>n||y<1||y>n)
return ;
if(maze2[x][y]=='0')
return;
maze2[x][y]='0';
for(int i=0;i<8;i++)
{
int a=x+dxx[i];
int b=y+dyy[i];
dfs2(a,b);
}
}
int main()
{
string s;
int c=1;
while(cin>>n&&n)
{
for(int i=1;i<=n;i++)
{
cin>>s;
for(int j=0;j<s.length();j++)
{
maze[i][j+1]=s[j];
maze2[i][j+1]=s[j];
}
}
int count1=0;
int count2=0;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(maze[i][j]=='1')
{
dfs(i,j);
count1++;
}
if(maze2[i][j]=='1')
{
dfs2(i,j);
count2++;
}
}
cout<<"Case "<<c++<<": "<<count1<<" "<<count2<<endl;
cout<<endl;
}
return 0;
}
相关文章推荐
- C++虚函数大总结
- mb_convert_encoding使用举例
- java桌面项目打包_by icewee_写得太棒了,直接转载了
- 编译安装MariaDB 及系统初始化 推荐
- [2010山东ACM省赛] Balloons(搜索)
- [2010山东ACM省赛] Balloons(搜索)
- Javascript 实现的StopWatch
- ExtJs4 学习笔记(一) 准备工作
- Java运算符
- window xp 与 mac 电脑共享
- Introduction to MySQL Stored Procedures
- 第七周 项目2 体验常成员函数
- Qt学习——聊天的QQ列表QToolBox类
- MongoDB学习之旅十二:MongoDB MapReduce
- 数据库中批量插入数据的方法,记录一下。
- Windows7+WDK+VS2010+VisualDDK驱动开发环境搭建(两篇)
- php脚本超时时间设置
- Python中第三方的用于解析HTML的库:BeautifulSoup
- Java 数据类型
- cocos2d-x 控件之 下拉菜单列表