山东省第一届ACM大学生程序设计竞赛 Balloons dfs搜索
2016-03-31 20:01
417 查看
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, whileA
andC1 are connected,C1
andC2 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 formatshould 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大学生程序设计竞赛示例程序
ACcode:#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 1000
using namespace std;
char mapp[maxn][maxn];
bool isused[maxn][maxn];
int n,ans1,ans2,loop=1;
void dfs1(int x,int y){
if(x<0||y<0||x>=n||y>=n||isused[x][y]||mapp[x][y]=='0')return;
isused[x][y]=true;
dfs1(x+1,y);dfs1(x-1,y);
dfs1(x,y+1);dfs1(x,y-1);
}
void dfs2(int x,int y){
if(x<0||y<0||x>=n||y>=n||isused[x][y]||mapp[x][y]=='0')return;
isused[x][y]=true;
dfs2(x+1,y);dfs2(x-1,y);
dfs2(x,y+1);dfs2(x,y-1);
dfs2(x+1,y+1);dfs2(x-1,y-1);
dfs2(x-1,y+1);dfs2(x+1,y-1);
}
int main(){
while(~scanf("%d",&n)&&n){
for(int i=0;i<n;++i)
scanf("%s",&mapp[i]);
ans1=0;memset(isused,false,sizeof(isused));
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(!isused[i][j]&&mapp[i][j]=='1'){
dfs1(i,j);
ans1++;
}
ans2=0;memset(isused,false,sizeof(isused));
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
if(!isused[i][j]&&mapp[i][j]=='1'){
dfs2(i,j);
ans2++;
}
printf("Case %d: %d %d\n\n",loop++,ans1,ans2);
}
return 0;
}
/*
5 11001 00100 11111 11010 10010 0
*/
相关文章推荐
- iOS开发之 CollectionViewCell 无法加载
- cocos: 无限循环滚动背景
- HDOJ 1070 Milk(水题,考英文的)
- acm_problem_1001 1008
- HDOJ 1070 Milk(水题,考英文的)
- 银行那些事儿--会计科目(1)
- 设计模式--单例模式(一)懒汉式和饿汉式
- OpenCV RGB2LAB执行效率测试
- 杭电ACM1001
- 【web前端】CSS基础
- OpenCV RGB2LAB执行效率测试
- C语言学习002:第一个完整的C程序代码
- 复利计算--单元测试
- linux 进程
- HDU2149——Public Sale(Bash Game巴什博奕)
- 如何简单的判断机器的大小端字节序
- CheckedTextView使用Demo
- Qt之等待提示框(QMovie)
- Qt之等待提示框(QMovie)
- 任何一个正整数都可以用2的幂次方表示