HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15533 Accepted Submission(s): 8911
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
1
2
2
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
char MAP[SIZE][SIZE];
int UPDATE[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
int N,M;
int ANS; struct Node
{
int x,y;
}QUE[SIZE * SIZE];
void dfs(int x,int y);
void bfs(int r,int c);
int main(void)
{
while(scanf("%d%d",&N,&M) && (N || M))
{
ANS = ;
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
scanf(" %c",&MAP[i][j]);
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
if(MAP[i][j] == '@')
{
ANS ++;
bfs(i,j);
}
printf("%d\n",ANS);
} return ;
} void dfs(int x,int y)
{
MAP[x][y] = '*';
int new_x,new_y;
for(int i = ;i < ;i ++)
{
new_x = x + UPDATE[i][];
new_y = y + UPDATE[i][];
if(new_x >= && new_x <= N && new_y >= && new_y <= M && MAP[new_x][new_y] == '@')
{
MAP[new_x][new_y] = '*';
dfs(new_x,new_y);
}
}
} void bfs(int r,int c)
{
MAP[r][c] = '*'; QUE[].x = r;
QUE[].y = c;
int front,rear;
front = ;
rear = ; while(front < rear)
{
int cur_x = QUE[front].x;
int cur_y = QUE[front].y;
front ++; for(int i = ;i < ;i ++)
{
int new_x = cur_x + UPDATE[i][];
int new_y = cur_y + UPDATE[i][];
if(new_x >= && new_x <= N && new_y >= && new_y <= M && MAP[new_x][new_y] == '@')
{
MAP[new_x][new_y] = '*';
QUE[rear].x = new_x;
QUE[rear].y = new_y;
rear ++;
}
}
}
}
HDU 1241 Oil Deposits (DFS/BFS)的更多相关文章
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits (DFS or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- HDU 1241 Oil Deposits DFS搜索题
题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
随机推荐
- MYSQL event_scheduler
一.概述 事件调度器是在 MySQL 5.1 中新增的另一个特色功能,可以作为定时任务调度器,取代部分原先只能用操作系统任务调度器才能完成的定时功>能.例如,Linux 中的 crontabe ...
- work_5
第五次作业对我个人来说是很难的,因为之前没怎么接触过这方面的内容,有幸能跟宗毅组成一队,我也仔细看了他的Python代码,因为对于Python也是第一次接触,所以我感觉在有限的时间里学会并且灵活运用还 ...
- String中的Indexof,LastIndexOf, Indexofany,LastIndexOfAny 的区别
本文转载自 http://www.cnblogs.com/qinying/archive/2008/09/22/1295730.html 定位子串是指在一个字符串中寻找其中包含的子串或者某个字符.在S ...
- hdu2121 - Ice_cream’s world II(朱刘算法,不固定根)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目意思大概是要你在一些城市中选一个做首都 , 要求首都都能到其他城市 , 道路花费要最少 , ...
- Simulator模拟器 硬件键盘不能输入
快捷键: Command + Shift +K
- TypeScript学习笔记(二):基本数据类型及数据转换
数据类型 我们来看看TypeScript中的基本数据类型都有哪些. boolean 布尔值,支持true和false. var isDone: boolean = false; 默认为undefine ...
- ADO.NET 快速入门(十五):ADO 应用转换为 ADO.NET
这是一个已经移植到 .NET 的 ADO 应用的例子.也演示了单向.只读.快速 DataReader 的使用.它演示如何使用 DataView 类从 DataSet 获取一个 Table 和 操作一个 ...
- 配置DNS服务器IP
#############################脚本功能及说明#################### #该脚本用来在本地服务器上配置DNS服务器IP #创建时间:2014-10-22 ## ...
- BZOJ 2152: 聪聪可可 点分治
2152: 聪聪可可 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php ...
- POJ - 2965 - The Pilots Brothers' refrigerator (高效贪心!!)
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19356 ...