hdu Oil Deposits
2015-12-09 20:25
155 查看
算法:深搜
题意:让你判断有几个油库;
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each
plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous
pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following
this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
代码:
题意:让你判断有几个油库;
Problem Description
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each
plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous
pockets. Your job is to determine how many different oil deposits are contained in a grid.
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following
this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
代码:
#include <iostream> #include <cstring> #include <algorithm> #include <iomanip> using namespace std; char ch[105][105];int n,m; void dfs(int x,int y) { ch[x][y]='*'; int i,j; for(int nx=-1;nx<=1;nx++) { for(int ny=-1;ny<=1;ny++) { int dx=x+nx,dy=y+ny; if(dx>=0&&dx<n&&dy>=0&&dy<m&&ch[dx][dy]=='@') dfs(dx,dy); } } return ; } int main() { int i,j,k; while(cin>>n>>m&&n&&m) { memset(ch,0,sizeof(ch)); for(i=0;i<n;i++) cin>>ch[i]; k=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(ch[i][j]=='@') { dfs(i,j); k++; } } } cout<<k<<endl; } return 0; }
相关文章推荐
- 多线程学习-停止线程
- Saiku一个简短的引论
- hdu Oil Deposits
- 第三个Sprint冲刺第五天(燃尽图)
- 放球模型
- STM32中EXTI(外部中断)和NVIC(嵌套向量中断)的关系
- ubuntu配置javaweb环境(jdk,tomcat,mysql,eclipse)
- leetcode Triangle
- 【蓝桥第一周】计数的梦
- UNIX环境编程-----原始套接字-----疑问:发送给原始套接字的数据是怎么处理的???
- Ejb框架搭建
- 【iOS】MD5数据加密和网络安全
- Java中关键字super与this的区别
- ubuntu下安装Apache+PHP+Mysql
- thinkphp session过期时间设置
- BZOJ 1415: [Noi2005]聪聪和可可
- c++
- linux下history(历史)命令用法详解
- Android Java(2015-6-18 15:28、2016-1-30 21:18)
- leetcode -- Reorder List -- 重点,很适合作为面试题