解题思路:

  1. 遍历扫描二维数组,遇到‘@’,结果ans++;

  2. 将当前 i,j 位置置为‘*’,将当前‘@’的 i,j 传人到DFS函数中,开始遍历八个方向的字符

   如果碰到 '@' 则先将当前置为‘*’,然后再次递归传递,直到超出界限或者扫描不到‘@’,结束递归

  3. DFS()的作用是将i,j为开始周围连续的“@”全部改为‘*’

  4. 最后输出 ans 即可;

Ac code :

 #include<bits/stdc++.h>
using namespace std;
char xstr[][];
static int ans;
int x,y;
void dfs(int i,int j)
{ if(j+<y&&xstr[i][j+]=='@')
{
xstr[i][j+]='*';
dfs(i,j+);
}
if(i+<x&&j->=&&xstr[i+][j-]=='@')
{
xstr[i+][j-]='*';
dfs(i+,j-);
}
if(i+<x&&xstr[i+][j]=='@')
{
xstr[i+][j]='*';
dfs(i+,j);
}
if(i+<x&&j+<y&&xstr[i+][j+]=='@')
{
xstr[i+][j+]='*';
dfs(i+,j+);
}
if(i->=&&j+<y&&xstr[i-][j+]=='@')
{
xstr[i-][j+]='*';
dfs(i-,j+);
}
if(j->=&&xstr[i][j-]=='@')
{
xstr[i][j-]='*';
dfs(i,j-);
}
if(i->=&&xstr[i-][j]=='@')
{
xstr[i-][j]='*';
dfs(i-,j);
}
if(i->=&&j->=&&xstr[i-][j-]=='@')
{
xstr[i-][j-]='*';
dfs(i-,j-);
}
}
int main()
{ while(scanf("%d%d",&x,&y)!=EOF&&x+y)
{
ans=;
memset(xstr,,sizeof(xstr));
int i,j;
for(i=; i<x; i++)
{
scanf("%s",&xstr[i]);
}
for(i=; i<x; i++)
{
for(j=; j<y; j++)
{
if(xstr[i][j]=='@')
{
xstr[i][j]='*';
ans++;
dfs(i,j);
}
}
}
printf("%d\n",ans);
}
return ;
}

hdu 1241 Oil Deposits(水一发,自我的DFS)的更多相关文章

  1. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  2. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  3. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  4. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  6. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. HDOJ/HDU 1241 Oil Deposits(经典DFS)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

随机推荐

  1. Android强制弹出,隐藏输入法.

    当我们弹出一个Dialog时候,如果这个Dialog需要输入数据,然后确定后又需要关闭输入法,一般系统的hide,跟show方法总会有各种问题,最霸道的解决方法就是写一个定时器,定时弹出或者关闭输入法 ...

  2. Gradle tip #3: Tasks ordering

    I noticed that the quite often problem I face when I work with Gradle - is tasks ordering (either ex ...

  3. [CareerCup] 10.1 Client-facing Service 面向客户服务器

    10.1 Imagine you are building some sort of service that will be called by up to 1000 client applicat ...

  4. 实验一(不知道怎么上传.docx格式)

    北京电子科技学院(BESTI) 实     验    报     告 课程:深入理解计算机系统        班级:1353 姓名:魏静静 文艺 刘虹辰 学号:20135302 20135331 20 ...

  5. 查询一个ID出现2种结果的情况

    项目中书籍分个人和机构,分属不同的表 所以有的时候ID是一样的,那么只根据ID查询书籍就会存在ID=xxx的既有个人又有机构,而通常我们可能只需要一个,多的没做区分就出问题了! 所以数据统一做查询的时 ...

  6. node 学习笔记 - fs 文件操作

    本文同步自我的个人博客:http://www.52cik.com/2015/12/03/learn-node-fs.html 最近看到群里不少大神都开始玩 node 了,我感觉跟他们步伐越来越大了, ...

  7. Object-Oriented CSS

    1.指导思想: http://oocss.org/ 2.reset.css http://meyerweb.com/eric/tools/css/reset/ 3.normalize.css http ...

  8. So... what's up?

    So... testing markdown editor what to learn in May? html5 canvas api codeigniter framework var test ...

  9. [C/C++基础] C语言常用函数sprintf和snprintf的使用方法

    Sprintf 函数声明:int sprintf(char *buffer, const char *format [, argument1, argument2, …]) 用途:将一段数据写入以地址 ...

  10. 浅析Javascript

    Javascript是一种脚本语言,从出生就被唾弃,一开始人们使用它只是为了解决诸如页面数据校验之类的问题.它基于prototype的面向对象实现一度被认为很丑很难用,甚至很多身处一线Web开发者都不 ...