DFS PKU 1562
简单地DFS
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 12801 | Accepted: 6998 |
Description
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
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
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <set>
//#include <map>
using namespace std;
#define MAXN 100 + 10
char map[MAXN][MAXN];
int vis[MAXN][MAXN];
int m,n;
//int count;
int xx[8] = {-1,-1,0,1,1,1,0,-1};
int yy[8] = {0,1,1,1,0,-1,-1,-1}; void DFS(int x,int y){
int i;
for(i=0;i<8;i++){
int dx = x+xx[i];
int dy = y+yy[i];
if(dx>=0 && dx<m && dy>=0 && dy<n){
if(map[dx][dy]=='@' && vis[dx][dy]==0){
vis[dx][dy] = 1;
DFS(dx,dy);
//vis[dx][dy] = 0;
}
}
} return ;
} int main(){
int i,j;
int ans; while(~scanf("%d%d",&m,&n)){
if(m==0 && n==0){
break;
}
for(i=0;i<m;i++){
scanf("%s",map[i]);
}
ans = 0;
memset(vis,0,sizeof(vis));
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(map[i][j]=='@' && vis[i][j]==0){
vis[i][j] = 1;
ans++;
DFS(i,j);
//vis[i][j] = 0;
}
}
}
printf("%d\n",ans);
}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
DFS PKU 1562的更多相关文章
- POJ 1562 && ZOJ 1709 Oil Deposits(简单DFS)
题目链接 题意 : 问一个m×n的矩形中,有多少个pocket,如果两块油田相连(上下左右或者对角连着也算),就算一个pocket . 思路 : 写好8个方向搜就可以了,每次找的时候可以先把那个点直接 ...
- poj 1562 dfs
http://poj.org/problem?id=1562 #include<iostream> using namespace std; ,m=,sum=; ][]; ][]={-,, ...
- POJ 1562 Oil Deposits (HDU 1241 ZOJ 1562) DFS
现在,又可以和她没心没肺的开着玩笑,感觉真好. 思念,是一种后知后觉的痛. 她说,今后做好朋友吧,说这句话的时候都没感觉.. 我想我该恨我自己,肆无忌惮的把她带进我的梦,当成了梦的主角. 梦醒之后总是 ...
- POJ 1562 Oil Deposits (并查集 OR DFS求联通块)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14628 Accepted: 7972 Des ...
- POJ 1562(L - 暴力求解、DFS)
油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...
- [POJ] 1562 Oil Deposits (DFS)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16655 Accepted: 8917 Des ...
- pku 2488 A Knight's Journey (搜索 DFS)
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28697 Accepted: 98 ...
- PKU 2531 Network Saboteur(dfs+剪枝||随机化算法)
题目大意:原题链接 给定n个节点,任意两个节点之间有权值,把这n个节点分成A,B两个集合,使得A集合中的每一节点与B集合中的每一节点两两结合(即有|A|*|B|种结合方式)权值之和最大. 标记:A集合 ...
- HDU - 1241 POJ - 1562 Oil Deposits DFS FloodFill漫水填充法求连通块问题
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil de ...
随机推荐
- CSharp SQLServer 登陆
=======后台SQLServer存储过程================ --创建数据库create database Stu; --创建表use MyShool;if exists(select ...
- BootStrap -- Grid System
<script src="jquery.1.9.js"></script> <script src="js/bootstrap.min.js ...
- CSDN markdown 编辑 三 基本语法
这是一个副本,在准备好了help档,另外补充一些他们自己的解释. 这一次是不说话UML和LaTEX. 准备抽两篇文章专门谈. 1. 标题和字体大小 markdown字号太少了.觉得应该再加些. 在文字 ...
- Linux 下开启ssh服务(转)
二.SSH SSH 为 Secure Shell 的缩写,由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目 ...
- iOS缓存类的设计
使用执行速度缓存的程序可以大大提高程序,设计一个简单的缓存类并不需要太复杂的逻辑. 只需要一个简单的3接口. 存款对象 以一个对象 删除对象 阅读对象 watermark/2/text/aHR0cDo ...
- TotoiseSVN基本用法
TotoiseSVN的基本用法 TotoiseSVN的基本用法 一.签入源码到SVNserver 假如我们使用Visual Studio在目录StartKit中创建了一个项目.我们要把这个项目的源码签 ...
- Object-C 新手教程
大纲 開始吧 下载这篇教学 设定环境 前言 编译 hello world 创建 Classes @interface @implementation 把它们凑在一起 具体说明... 多重參数 建构子( ...
- groovy : poi 导出 Excel
參考 poi-3.10-FINAL/docs/spreadsheet/quick-guide.html write_xls.groovy 代码例如以下 package xls; import java ...
- 证明 poj 1014 模优化修剪,部分递归 有错误
这个问题是存在做.我发现即使是可行的一个问题,但不一定正确. 大部分数据疲软,因为主题. id=1014">poj 1014 Dividing 题目大意:有6堆石头,权重分别为1 2 ...
- COM Interop
1.MSDN上的文章:COM Interop教程 2.接口的三种类型:IDispatch.IUnknown和Dual 3.使用TlbImp来更灵活地自动生成RCW 4.托管事件基于委托,而非托管事件( ...