山东省第一届ACM大学生程序设计竞赛-Balloons(搜索)
来源:互联网 发布:程序员教程pdf 编辑:程序博客网 时间:2024/05/17 04:35
问题及代码:
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 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.
输出
示例输入
511001001001111111010100100
示例输出
Case 1: 3 2
提示
来源
题意:
输入一个n行n列的矩阵,其中:0 represents a block and 1represents balloons
根据以下条件
To Saya, elementA(xa,ya)andB(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 表示是上下左右对角八个方向;
判断连接在一起的“1”块有几组。
/** Copyright (c) 2016, 烟台大学计算机与控制工程学院* All rights reserved.* 文件名称:dfs.cpp* 作 者:单昕昕* 完成日期:2016年4月1日* 版 本 号:v1.0*/#include<iostream>#include<cstring>#include<cstdio>#include<malloc.h>using namespace std;int book[101][101];int Map[101][101];int n,tx,ty;int move1_x[4]= {0,1,0,-1};int move1_y[4]= {-1,0,1,0};int move2_x[8]= {-1,0,1,1,1,0,-1,-1};int move2_y[8]= {-1,-1,-1,0,1,1,1,0};void dfs1(int x,int y){ int i; if (Map[x][y]==1&&book[x][y]==false) { book[x][y]=true; for(i=0; i<4; i++) { tx=x+move1_x[i];//计算坐标 ty=y+move1_y[i]; if (tx>=0&&ty>=0&&tx<n&&ty<n&&Map[tx][ty]==1&&book[tx][ty]==false) { dfs1(tx,ty); } } return ; }}void dfs2(int x,int y){ int i; if (Map[x][y]==1&&book[x][y]==false) { book[x][y]=true; for(i=0; i<8; i++) { tx=x+move2_x[i];//计算坐标 ty=y+move2_y[i]; if (tx>=0&&ty>=0&&tx<n&&ty<n&&Map[tx][ty]==1&&book[tx][ty]==false) { dfs2(tx,ty); } } return ; }}int main(){ int Case=0; while(cin>>n&&n!=0) { memset(book,false,sizeof(book)); int i,j,sum1=0,sum2=0; for(i=0; i<n; i++) for(j=0; j<n; j++) cin >> Map[i][j]; for(i=0; i<n; i++) for(j=0; j<n; j++) { if(Map[i][j]==1&&book[i][j]==false) { dfs1(i,j); ++sum1; } } memset(book,false,sizeof(book)); for(i=0; i<n; i++) for(j=0; j<n; j++) { if(Map[i][j]==1&&book[i][j]==false) { dfs2(i,j); ++sum2; } } cout<<"Case "<<++Case<<": "<<endl; cout<<sum1<<" "<<sum2<<endl; } return 0;}/*51 1 0 0 10 0 1 0 01 1 1 1 11 1 0 1 01 0 0 1 0*/
运行结果:
- 山东省第一届ACM大学生程序设计竞赛-Balloons(搜索)
- 山东省第一届ACM大学生程序设计竞赛 Balloons bfs搜索
- 2010年山东省第一届ACM大学生程序设计竞赛:Balloons
- 2010年山东省第一届ACM大学生程序设计竞赛——Balloons
- [2010山东省第一届ACM大学生程序设计竞赛]——Balloons
- SDUT 2152 Balloons(DFS 四面+八面) 2010年山东省第一届ACM大学生程序设计竞赛
- 山东省第一届ACM大学生程序设计竞赛--Greatest Number--二分
- 山东省第一届ACM大学生程序设计竞赛 Shopping 水。。。。。。。
- 2010年山东省第一届ACM大学生程序设计竞赛:shopping
- 山东省第一届ACM大学生程序设计竞赛 Emergency floyd变形
- 山东省第一届ACM大学生程序设计竞赛 problemG Shopping
- 山东省第一届ACM程序设计大赛 Balloons (简单dfs)
- 山东省第七届ACM大学生程序设计竞赛
- 第四届 山东省ACM大学生程序设计竞赛
- 第七届 山东省ACM大学生程序设计竞赛
- 山东省第一届ACM省赛题——Balloons(搜索)
- 山东省第三届ACM大学生程序设计竞赛(待添加)
- Mountain Subsequences(山东省第四届ACM大学生程序设计竞赛)
- Java 正则表达式的匹配
- 全排列问题、八皇后问题、组合问题的递归解法
- Codeforces Round #346 (Div. 2) D. Bicycle Race
- 第7章管理产品经理
- Android MTK Launcher3 替换桌面图标
- 山东省第一届ACM大学生程序设计竞赛-Balloons(搜索)
- java之文件
- TCP 聊天室
- nrf51822 ---协议栈时钟源选择(32.768khz)
- 输入URL页面加载过程
- 集合框架--Map集合之LinkedHashMap
- 2016蓝桥杯C++A组第六题 寒假作业
- VE设计#7 逻辑层Myset
- Trie——电话簿