POJ 1562 Oil Deposits
来源:互联网 发布:最近淘宝流量出奇的少 编辑:程序博客网 时间:2024/05/16 08:53
Description
The GeoSurvComp geologicsurvey company is responsible for detecting underground oil deposits.GeoSurvComp works with one large rectangular region of land at a time, andcreates a grid that divides the land into numerous square plots. It thenanalyzes each plot separately, using sensing equipment to determine whether ornot the plot contains oil. A plot containing oil is called a pocket. If twopockets are adjacent, then they are part of the same oil deposit. Oil depositscan be quite large and may contain numerous pockets. Your job is to determinehow many different oil deposits are contained in a grid.
Input
The input contains one or more grids. Each grid begins with a linecontaining m and n, the number of rows and columns in the grid, separated by asingle 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 characterseach (not counting the end-of-line characters). Each character corresponds toone plot, and is either `*', representing the absence of oil, or `@',representing an oil pocket.
Output
are adjacent horizontally,vertically, or diagonally. An oil deposit will not contain more than 100pockets.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题目简介:@表示油井。@只要周围的8个方向上存在@,表示这两个油井是连接的。连接在一起的油井算一个油田。问一共多少个油田。这道题略坑爹的是,输入的两个数字之后有可能存在多个空格,这里需要注意。
方法:深度优先搜索。没有写过深度优先搜索。之前有听过学长讲了一点点栈,结合自己学习的队列的知识,套用在栈上面,就写了一个深搜。当时感觉还不错,后来看了人家用递归写的,发现弱爆了!!!!
#include<iostream>#include<cstdio>#include<cstring>#include<stack>using namespace std;struct Stack{int x, y;int flag[8];};int flag[110][110], m, n;int main(){int dir[8][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};//表示8个方向int i, j, k, sum;char map[110];while(cin>>m>>n){if(m + n==0)break;//输入操作int count = 0;memset(flag,0,sizeof(flag));for(i = 0;i<m;i++){scanf("%s",&map);for(j = 0;j<n;j++){if(map[j]=='@'){flag[i][j] = 2;}}}//‘*’为0,未被处理的‘@’为2,处理了的额为3stack<struct Stack> S;struct Stack tempS, newdS;for(i = 0;i<m;i++){for(j = 0;j<n;j++){memset(newdS.flag,0,sizeof(newdS.flag)); memset(tempS.flag,0,sizeof(tempS.flag));if(flag[i][j]==2){tempS.x = i;tempS.y = j;flag[tempS.x][tempS.y] = 3;S.push(tempS);while(!S.empty()){tempS = S.top();int flag1 = 0;for(k = 0;k<8;k++){if(!tempS.flag[k]){newdS.x = tempS.x + dir[k][0];newdS.y = tempS.y + dir[k][1];tempS.flag[k] = 1;if(newdS.x>=0&&newdS.x<m&&newdS.y>=0&&newdS.y<n){if(flag[newdS.x][newdS.y]==2){flag[newdS.x][newdS.y] = 3;S.push(newdS);flag1 = 1;break;}}}}if(flag1==0){S.pop();}}count++;}}}printf("%d\n",count);}return 0;}
- Oil Deposits(POJ 1562)
- poj 1562 Oil Deposits
- POJ 1562 Oil Deposits
- poj 1562 Oil Deposits
- poj 1562 Oil Deposits
- poj-1562Oil Deposits
- POJ-1562-Oil Deposits
- POJ 1562 Oil Deposits
- POJ 1562 Oil Deposits
- POJ 1562 Oil Deposits
- POJ 1562 Oil Deposits
- poj 1562 Oil Deposits
- poj 1562 Oil Deposits
- POJ:1562 Oil Deposits
- poj 1562 Oil Deposits
- poj 1562:Oil Deposits
- poj 1562 oil Deposits
- POJ 1562 Oil Deposits
- Android layout属性大全
- 数据挖掘与R语言
- ORACLE 11g安装图解
- 实现了一个比nginx速度更快的HTTP服务器
- 医学博士分享男人行房延时经验
- POJ 1562 Oil Deposits
- 一个空类会生成哪些默认函数
- android 虚拟摇杆图片版
- Jquery 实现标签云 标签数据从Json导入
- String.Format格式说明
- js for in浏览器兼容问题
- mysql 添加和删除用户
- Linux(CentOS)下把python脚本转化成可执行程序
- c# 设置窗口显示位置