题目来源  poj 1562

Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each 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

The input 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.

Output

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 ac代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
struct s{
int x,y;
char c;
}map[110][110];
const int dir[8][2]={{1,0},{1,-1},{1,1},{0,1},{0,-1},{-1,-1},{-1,0},{-1,1}};
queue<s> q;
int n,m,ans;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<8;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='@'){
q.push(map[next.x][next.y]);
map[next.x][next.y].c='*';
}
}
}
return ;
}
int main()
{
while (cin>>n>>m&&n&&m){
ans=0;
while (!q.empty())
q.pop();
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='@'){
map[i][j].x=i;
map[i][j].y=j;
}
}
}
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
if (map[i][j].c=='@'){
q.push(map[i][j]);
map[i][j].c='*';
ans++;
dfs(i,j);
}
}
}
printf("%d\n",ans);
}
return 0;
}

Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26317    Accepted Submission(s): 15909

Problem Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

 
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

'.' - a black tile 
'#' - a red tile 
'@' - a man on a black tile(appears exactly once in a data set) 

 
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself). 
 
Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0
 
Sample Output
45
59
6
13
 

ac代码:

#include<iostream> //hdu   1312
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Swap(a,b,t) t=a,a=b,b=t
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x));
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f;
const double eps=1e-12;
int n,m,ans;
char temp[25];
const int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
struct s{
int x,y;
char c;
}map[25][25];
queue <s> q;
void dfs(int x,int y)
{
s p,next;
while (!q.empty()){
p=q.front();
q.pop();
for (int k=0;k<4;k++){
next.x=p.x+dir[k][0];
next.y=p.y+dir[k][1];
if (map[next.x][next.y].c=='.'&&next.x<=n&&next.y<=m){
ans++;
q.push(map[next.x][next.y]);
map[next.x][next.y].c='#';
}
}
}
return ;
}
int main()
{
while (cin>>m>>n&&n&&m){
while (!q.empty())
q.pop();
int start_x,start_y;
ans=1;
for(int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
cin>>map[i][j].c;
if (map[i][j].c=='.'){
map[i][j].x=i;
map[i][j].y=j;
}
else if (map[i][j].c=='@'){
map[i][j].x=start_x=i;
map[i][j].y=start_y=j;
q.push(map[i][j]);
}
}
}
dfs(start_x,start_y);
printf("%d\n",ans);
}
return 0;
}

dfs 队列的更多相关文章

  1. poj 3321

    题目链接 题意:一开始1-n都有苹果,Q查询以x为根下存在多少. 树状数组+DFS+队列转换 这题纠结了2天,一开始一点思路都没有,看大神都是吧树状数组转换成队列来做 看了好久都不知道怎么转换的, 解 ...

  2. 最短路 spfa 算法 && 链式前向星存图

    推荐博客  https://i.cnblogs.com/EditPosts.aspx?opt=1 http://blog.csdn.net/mcdonnell_douglas/article/deta ...

  3. hdu4135容斥原理 组合遍历

    容斥原理实现的关键在于:组合遍历,即如何遍历2^n种组合. 容斥原理的三种写法: DFS 队列数组 位数组 #include<stdio.h> #include<iostream&g ...

  4. POJ 2227 The Wedding Juicer (优先级队列+bfs+dfs)

    思路描述来自:http://hi.baidu.com/perfectcai_/item/701f2efa460cedcb0dd1c820也可以参考黑书P89的积水. 题意:Farmer John有一个 ...

  5. Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现

    今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...

  6. 对比dfs与bfs的存储机制以及bfs与队列的关系

    dfs由于是利用递归进行遍历,所以每种情况在时空上不会出现冲突,所以可以利用数组将每种情况的各个元素的值进行存储(即存储当前位) 而bfs由于并不是利用递归,不能将每种情况的值进行不冲突地存储,但由于 ...

  7. 【BZOJ2783】[JLOI2012]树 DFS+栈+队列

    [BZOJ2783][JLOI2012]树 Description 在这个问题中,给定一个值S和一棵树.在树的每个节点有一个正整数,问有多少条路径的节点总和达到S.路径中节点的深度必须是升序的.假设节 ...

  8. DFS、栈、双向队列:CF264A- Escape from Stones

    题目: Squirrel Liss liv Escape from Stonesed in a forest peacefully, but unexpected trouble happens. S ...

  9. 51nod1153(dfs/单调队列)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1153 题意:中文题诶- 思路:一个比较简单的方法是dfs隐式 ...

随机推荐

  1. ABAP SICF服务和Java Servlet的比较

    In my opinion ABAP ICF handler and Java Servlet play the same role in enhancement which enables your ...

  2. AWK入门指南

    一. AWK入门指南 Awk是一种便于使用且表达能力强的程序设计语言,可应用于各种计算和数据处理任务.本章是个入门指南,让你能够尽快地开始编写你自己的程序.第二章将描述整个语言,而剩下的章节将向你展示 ...

  3. Kill占用指定端口的进程的方法

    (1)查询占用指定端口进程的PID 打开cmd命令行,输入netstat -ano|findstr 8080(指定端口号) 最后一列即为占用该端口的进程的PID (2)KILL指定PID的进程 紧接着 ...

  4. postgresql+postgis+pgrouting实现最短路径查询(2)---openlayers+geoserver实现最短路径

    自己的最短路径实现基本上是按照参考博文的1.2和3进行的,实现的时候也是问题不断,只能是一个一个解决. 问题1:自己发布的geoserver服务无法和OSM底图叠加到一起. 解决:参考博文2提到发布服 ...

  5. [USACO19JAN]Exercise Route

    题目 这题的数据有点水,暴力合并\(set\)好像过了 分析一下这个题的性质,发现我们一条非树边就会形成一个环,而我们要求选择两个非树边,就会形成两个环,要求不走重复的点,就是说我们需要走一个大环,且 ...

  6. Mac OS系统下配置hosts的方法

    首先,介绍下什么是hosts Hosts是一个没有扩展名的系统文件,可以用系统自带的记事本等工具打开,作用就是将一些常用的网址域名与其对应的IP地址建立一个关联,当用户在浏览器输入一个需要登录的网址时 ...

  7. Entity Framework——执行sql语句

    EF版本:6.0.0 EF对大量数据或多表连接一次操作耗时较大,或要求响应时间尽可能小,因此采用EF框架执行SQL语句的方案 1DbContext.Database 这个类包含了大量的操作方法,见截图 ...

  8. 视图 b

  9. js 实现加入收藏/加入首页功能

    加入收藏夹实现: html代码如下: <a href="javascript:;" onclick="AddFavorite(window.location.hre ...

  10. 【题解】洛谷P2296 [NOIP2014TG] 寻找道路(SPFA+DFS)

    题目来源:洛谷P2296 思路 一开始看还以为是一道水题 虽然本来就挺水的 本道题的难点在于如何判断是否路径上的点都会直接或者间接连着终点 我们需要在一开始多建一个反向图 然后从终点DFS回去 把路径 ...