您的位置:首页 > 其它

队列实现简单的dfs算法

2016-04-23 12:55 302 查看
以具体的题目为例:数细胞

一矩形阵列由数字0到9组成,数字1到9代表细胞,细胞的定义为沿细胞数字上下左右还是细胞数字则为同一细胞,求给定矩形阵列的细胞个数

sample input:

4 10

1 2 3 4 5 1 1 1 6 7

1 0 3 4 5 6 1 5 1 0

2 0 4 5 6 6 1 6 7 1

0 0 6 0 6 6 1 0 8 9

sample output:

1

主要思想:

利用队列先进后出的特性来对数据进行遍历,同时修改原数据表示已走过,每走一步探索该节点的附近是否可走,如果是就进队,然后出队获取下一步坐标,以此类推可遍历整个数组

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct qnode
{
int x, y;
qnode *next;
}Qnode;

typedef struct
{
Qnode *front;
Qnode *rear;
}Liqueue;

int a[100][100] = {0};

void Initqueue(Liqueue *&L)
{
L = (Liqueue *)malloc(sizeof(Liqueue));
L->front = L->rear = NULL;
}

void Inqueue(Liqueue *&L, int x, int y)
{
Qnode *p;
p = (Qnode *)malloc(sizeof(Qnode));
p->next = NULL;
p->x = x;
p->y = y;
if (L->rear == NULL)
L->front = L->rear = p;
else
{
L->rear->next = p;
L->rear = p;
}
}

int Outqueue(Liqueue *&L, int &c, int &d)
{
Qnode *t;
if (L->rear == NULL)
return 0;
t = L->front;
if (L->front == L->rear)
L->front = L->rear = NULL;
else
L->front = L->front->next;
c = t->x;
d = t->y;
return 1;
}

void Change(int m, int n)//将所有数据转化为0和1方便运算
{
int i, j;
for (i = 0;i < m;i++)

4000
{
for (j = 0;j < n;j++)
{
if (a[i][j] != 0)
a[i][j] = 1;
}
}
}

int Find(Liqueue *L, int m, int n)
{
int counter = 0, c = 0, d = 0;
int i, j;
for (i = 0;i < m;i++)
for (j = 0;j < n;j++)
if (a[i][j] == 1)
{
Inqueue(L, i, j);
a[i][j] = 0;
while (L->rear != NULL)//当队列不为空时循环进行
{
Outqueue(L, c, d);
if (c - 1 >= 0 && a[c - 1][d] == 1)//上
Inqueue(L, c - 1, d);
if (c + 1 >= 0 && a[c + 1][d] == 1)//下
Inqueue(L, c + 1, d);
if (d - 1 >= 0 && a[c][d - 1] == 1)//左
Inqueue(L, c, d - 1);
if (d + 1 >= 0 && a[c][d + 1] == 1)//右
Inqueue(L, c, d + 1);
a[c][d] = 0;
}
counter++;
}
return counter;
}
int main()
{
Liqueue *L;
Initqueue(L);
int i, j, m, n;
cin >> m;
cin >> n;
for (i = 0;i < m;i++)
for (j = 0;j< n;j++)
cin >> a[i][j];
Change(m, n);
cout << Find(L, m, n);
return 0;
}

//递归算法

#include<iostream>
#include<stdio.h>
using namespace std;
int date[100][100],M=0;
int m,n;
void stt(int x,int y)
{
if(date[x][y]!=0)
{
date[x][y]=0;
if(x+1<m)
stt(x+1,y);

if(y+1<n)
stt(x,y+1);

if(x-1>=0)
stt(x-1,y);

if(y-1>=0)
stt(x,y-1);

}

}

int main()
{
int i,j;
cin>>m>>n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>date[i][j];
for(i=0;i<m;i++)
for(j=0;j<n;j++)
if(date[i][j]!=0)
{
M++;
stt(i,j);
}

cout<<M;
return 0;
}
//dfs算法
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
int a[100][100];
int vist[100][100];
int m, n;
void dfs(int x, int y)
{
int i;
int next[4][2] = { { 0,1 },{ 1,0 },{ 0,-1 },{ -1,0 } };
for (i = 0;i < 4;i++)
{
int tx = x + next[i][0];
int ty = y + next[i][1];
if (!vist[tx][ty] && a[tx][ty] != 0 && 0 <= tx&&tx <= m && 0 <= ty&&ty <= n)
{
vist[tx][ty] = 1;
dfs(tx, ty);
}
}
return;
}
int main()
{
int count=0;
cin >> m >> n;
int i,j;
for (i = 0;i < m;i++)
for (j = 0;j < n;j++)
cin >> a[i][j];
for (i = 0;i < m;i++)
for (j = 0;j < n;j++)
if (a[i][j] != 0 && !vist[i][j])
{
dfs(i, j);
count++;
}
cout << count;
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: