dfs 队列

  

 

题目来源  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;
}

转载于:https://www.cnblogs.com/q1204675546/p/9684167.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用邻接矩阵实现图的DFS遍历的队列模板,使用C语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #define MAX_VERTEX_NUM 100 // 最大顶点数 typedef struct { int data[MAX_VERTEX_NUM]; int front; int rear; } Queue; void init(Queue *q) { q->front = q->rear = 0; } bool isEmpty(Queue *q) { return q->front == q->rear; } bool isFull(Queue *q) { return (q->rear + 1) % MAX_VERTEX_NUM == q->front; } bool enqueue(Queue *q, int x) { if (isFull(q)) { return false; } q->data[q->rear] = x; q->rear = (q->rear + 1) % MAX_VERTEX_NUM; return true; } bool dequeue(Queue *q, int *x) { if (isEmpty(q)) { return false; } *x = q->data[q->front]; q->front = (q->front + 1) % MAX_VERTEX_NUM; return true; } typedef struct { int vertex[MAX_VERTEX_NUM]; // 顶点数组 int edge[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; // 邻接矩阵 int vertexNum, edgeNum; // 顶点数和边数 } Graph; void initGraph(Graph *g, int vertexNum) { g->vertexNum = vertexNum; g->edgeNum = 0; for (int i = 0; i < vertexNum; ++i) { for (int j = 0; j < vertexNum; ++j) { g->edge[i][j] = 0; } } } void addEdge(Graph *g, int u, int v) { g->edge[u][v] = 1; g->edgeNum++; } void dfs(Graph *g, int v, bool *visited) { visited[v] = true; printf("%d ", g->vertex[v]); for (int i = 0; i < g->vertexNum; ++i) { if (g->edge[v][i] == 1 && !visited[i]) { dfs(g, i, visited); } } } void bfs(Graph *g, int v, bool *visited) { Queue q; init(&q); visited[v] = true; enqueue(&q, v); while (!isEmpty(&q)) { dequeue(&q, &v); printf("%d ", g->vertex[v]); for (int i = 0; i < g->vertexNum; ++i) { if (g->edge[v][i] == 1 && !visited[i]) { visited[i] = true; enqueue(&q, i); } } } } int main() { int vertexNum = 5; Graph g; initGraph(&g, vertexNum); for (int i = 0; i < vertexNum; ++i) { g.vertex[i] = i; } addEdge(&g, 0, 1); addEdge(&g, 0, 2); addEdge(&g, 1, 2); addEdge(&g, 2, 0); addEdge(&g, 2, 3); addEdge(&g, 3, 3); bool visited[MAX_VERTEX_NUM] = {false}; printf("DFS: "); for (int i = 0; i < vertexNum; ++i) { if (!visited[i]) { dfs(&g, i, visited); } } printf("\n"); for (int i = 0; i < vertexNum; ++i) { visited[i] = false; } printf("BFS: "); for (int i = 0; i < vertexNum; ++i) { if (!visited[i]) { bfs(&g, i, visited); } } printf("\n"); return 0; } ``` 其中,`Queue`结构体表示队列,使用循环队列实现。`Graph`结构体表示图,使用邻接矩阵实现。`initGraph`函数用于初始化图,`addEdge`函数用于添加边。`dfs`函数和`bfs`函数分别表示深度优先遍历和广度优先遍历,其中`bfs`函数使用队列实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值