A - Oil Deposits DFS
InputThe 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.
OutputFor 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
思路 : 这是一道特别经典的DFS题目 就是先找到一个@然后将与@相连的@全部变成“×” dfs结束,说明没有与@相连的点了 计一此数
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char arr[][];
int mark[][]={};
int d[][]={{,},{,},{,-},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y){
mark[x][y]=;
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]=='@'){
arr[dx][dy]='*';
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>n>>m&&m){
int ans=;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
scanf("%s",&arr[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(arr[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
} return ;
}
最新文章
- Effective java笔记(二),所有对象的通用方法
- 安卓智能POS终端手持机PDA应用仓库出入库,移库,盘点,销售开单系统
- Java - 安全的退出线程
- Atitit 判断判断一张图片是否包含另一张小图片
- webservice MaxReceivedMessageSize :已超过传入消息(65536)的最大消息大小配额
- SignalR入门之小试身手
- Hibernate(三)__核心接口和类
- ado.net 连接,删除,添加
- javascript复习总结
- FIO使用指南
- AC自动机(转)
- Ubuntu 14.04/14.10下安装VMware Workstation 11图文教程
- 用视频编辑软件打不开jpg格式的图片的解决方法
- 通用方法解决dedecms导航调用二级、三级栏目菜单
- 深究带PLL的错误复位设计
- JSP中 JSTL
- Delphi调用Android的.so文件(转)
- Java-大集合拆分为指定大小的小集合
- js原生的轮播,原理以及实践
- 联想功能 Jquery autocomplete.js输入框联想补全功能