1、题目大意:给定一个图,上边有*和@两种标记,其中@表示石油,好多@连在一起可以看成一个大的石油区,问在这个区域中有多少个石油区

#include<iostream>
using namespace std;
int n, m;
char grid[105][105]; //存储网格; //以下定义了移动的8个方向
int dir[8][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, 1 }, { 0, -1 },
{ 1, 1 }, { 1, 0 }, { 1, -1 } }; //从(x,y)位置进行DFS
void DFS(int x, int y) {
int i, xx, yy;
grid[x][y] = '*'; //进过后设置成*保证不会在经过了
for (i = 0; i < 8; i++) { xx = x + dir[i][0];
yy = y + dir[i][1]; //判断是否坐标越界。以坐标(0,0)、(n,m)作为标准.可是总以为应该是(m,n)??有点奇怪
if (xx >= n || yy >= m || xx < 0 || yy < 0) {
continue;
}
if (grid[xx][yy] != '*') {
DFS(xx, yy);
}
}
}
int main() {
int i, j;
int count; //统计数目
while (cin >> n >> m, n) {
cin.get(); //可以用来接收字符,在这里可以没有
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++)
cin >> grid[i][j];
cin.get();
}
count = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
if (grid[i][j] == '@') {
DFS(i, j); //从(i,j)位置进行DFS;
count++;//只有在所有能连成一片的油田连成一片的时候count才++
}
}
}
cout << count << endl;
}
return 0;
}

hdu 1241的更多相关文章

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

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

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

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

  3. 深搜基础题目 杭电 HDU 1241

    HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...

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

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

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

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

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

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

  7. Oil Deposits HDU - 1241 (dfs)

    Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...

  8. HDU 1241 (DFS搜索+染色)

    题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=1241 题目大意:求一张地图里的连通块.注意可以斜着连通. 解题思路: 八个方向dfs一遍,一边df ...

  9. HDU 1241 - Oil Deposits - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意: 求某块平面上,连通块的数量.一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通 ...

  10. HDU 1241 Oil Deposits bfs 难度:0

    http://acm.hdu.edu.cn/showproblem.php?pid=1241 对每个还未访问的点bfs,到达的点都标为一块,最后统计有多少块即可 #include <cstdio ...

随机推荐

  1. JMS集群部署问题 java.net.ConnectException: Connection refused; No available router to destination

    1:本地spring配置如下 <!-- JndiTemplate --> <bean id="jndiTpl" class="org.springfra ...

  2. Shortest Prefixes

    poj2001:http://poj.org/problem?id=2001 题意:给你一些单词,然后让你寻找每个单词的一个前缀,这个前缀能够唯一表示这个单词,并且是最短的. 题解:直接用trie树来 ...

  3. 在Qt中将函数发送到主线程执行

    考虑这样一种需求,使用Qt的线程类QThread在后台执行操作(比如说拷贝文件)的时候发生了错误,产生了一个错误信息需要提醒给用户,在后台输出很显然是不够的,因为用户可能根据就没有任何控制台可供程序输 ...

  4. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  5. 【转】掌握java枚举类型(enum type)

    原文网址:http://iaiai.iteye.com/blog/1843553 1   背景 在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量.之前我们通常利用 ...

  6. Letter Combinations of a Phone Number——LeetCode

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  7. jquery 定时器

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD ...

  8. C++ —— 非常量引用不能指向临时对象

    目录 举例 分析 解决 1.举例 非常量引用 指向 临时对象 —— 即:将 临时对象 传递给 非常量引用类型. 如以下情况就会出现: 实现实数Rational类,实数可以使用+号相加,运算的结果要可以 ...

  9. Windows 10 TH2

    Windows 10 TH2到底更新了啥? 15年11月,微软正式向Windows 10用户推送了Threshold 2(简称TH2)更新, 也就是传说中的November Update.更新后系统版 ...

  10. Spring-----1、Spring一个简短的引论

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGVrZXdhbmd6aQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...