DFS BFS代码

#define maxnum 30

#include<bits_stdc++.h>

int visited[maxnum]={0};

using namespace std;

typedef struct bian//边

{

int mark;//标记是否搜索

int ivex,jvex;//两顶点位置

bian *ilink,*jlink;//指向两顶点的其他边

int info;//信息

} bian,*pbian;

typedef struct dian//点

{

char name;

bian *first;//指向边

} dain;

typedef struct graph//图

{

dian dj[maxnum];

} graph;

typedef struct//队列

{

int base[maxnum];

int f;//front

int r;//rear

}que;

void initq(que &q)//初始化队列

{

q.f=q.r=0;

}

void enq(que &q,int e)//队尾插入

{

q.base[q.r]=e;

q.r=q.r+1;

}

void deq(que &q,int &e)//队头删除

{

e=q.base[q.f];

q.f=q.f+1;

}

int getlc(graph &g,char c,int n)//将顶点信息转化为位置

{

for(int i=0; i<n; i++)

if(c==g.dj[i].name)

return i;

}

void creatgraph(graph &g,int &n)//图的创建

{

int k,m;

cout<<"请输入图顶点个数:"<<endl;

cin>>n;

cout<<endl<<"请输入顶点信息(名称):"<<endl;

for(k=0; k<n; k++)

{

cin>>g.dj[k].name;

g.dj[k].first=NULL;

}

cout<<endl<<"请输入边个数:"<<endl;

cin>>m;

char a,b;

int mark,i,j;

pbian p1,p2;

cout<<endl<<"请输入边关系:"<<endl;

for(k=0; k<m; k++)

{

cin>>a>>b;

i=getlc(g,a,n);

j=getlc(g,b,n);

p1=(pbian)malloc(sizeof(bian));

p1->ivex=i;

p1->jvex=j;

p1->ilink=NULL;

p1->jlink=NULL;

p2=g.dj[i].first;

if(p2==NULL)

g.dj[i].first=p1;

else

{

mark=0;

while(mark==0)

{

if(p2->ivex==i&&p2->ilink==NULL) mark=1;

else if(p2->jvex==i&&p2->jlink==NULL) mark=2;

else if(p2->ivex==i) p2=p2->ilink;

else p2=p2->jlink;

}

if(mark==1) p2->ilink=p1;

else p2->jlink=p1;

}

p2=g.dj[j].first;

if(p2==NULL)

g.dj[j].first=p1;

else

{

mark=0;

while(mark==0)

{

if(p2->ivex==j&&p2->ilink==NULL) mark=1;

else if(p2->jvex==j&&p2->jlink==NULL) mark=2;

else if(p2->ivex==j) p2=p2->ilink;

else p2=p2->jlink;

}

if(mark==1) p2->ilink=p1;

else p2->jlink=p1;

}

}

}

void disp(graph &g,int n)//显示对应关系

{

cout<<"位置     名称"<<endl;

for(int i=0; i<n; i++)

cout<<i<<"        "<<g.dj[i].name<<endl;

}

void visit(graph &g,int v)//visit 函数

{

cout<<g.dj[v].name;

visited[v]=1;

}

void dfs(graph &g,int i)//dfs

{

if(visited[i]==0)

visit(g,i);

bian *p;

p=g.dj[i].first;

if(p==NULL)return ;

else

{

int m=0;

while(m==0)

{

if(p->ivex==i)

{

if(visited[p->jvex]==0)

{

dfs(g,p->jvex);

}

if(p->ilink==NULL)m=1;

else p=p->ilink;

}

else

{

if(visited[p->ivex]==0)

{

dfs(g,p->ivex);

}

if(p->jlink==NULL)m=1;

else p=p->jlink;

}

}//while

}//else

}

void bfs(graph &g)//bfs

{

int i,u;

bian *t;

cout<<"请输入开始遍历的点:"<<endl;

cin>>i;

que q;//队列q

initq(q);

if(visited[i]==0)

{

visit(g,i);

enq(q,i);

}

while(q.f!=q.r)//队不空

{

deq(q,u);

t=g.dj[u].first;

if(t==NULL)return;

int m=0;

while(m==0)

{

if(t->ivex==u)

{

if(visited[t->jvex]==0)

{

visit(g,t->jvex);

enq(q,t->jvex);

}

if(t->ilink==NULL)m=1;

else t=t->ilink;

}

else

{

if(visited[t->ivex]==0)

{

visit(g,t->ivex);

enq(q,t->ivex);

}

if(t->jlink==NULL)m=1;

else t=t->jlink;

}

}//while

}//while

}

void clearr()

{

for(int i=0;i<maxnum;i++)

{

visited[i]=0;

}

}

int main()

{

graph g;

int t=0;

int n;

creatgraph(g,n);

disp(g,n);

while(t!=3)

{

cout<<endl<<"请选择遍历方式(1:DFS,2:BFS,3:QUIT):"<<endl;

cin>>t;

if(t==1)

{

dfs(g,0);

clearr();

}

else if(t==2)

{

bfs(g);

clearr();

}

else if(t==3)

break;

}

return 0;

}

原文地址:https://www.cnblogs.com/wander-clouds/p/8443740.html

时间: 2024-08-30 15:46:36

DFS BFS代码的相关文章

邻接矩阵DFS,BFS代码实现

// 邻接矩阵的深度和广度优先遍历 #include <stdio.h> #define OK 1 // 执行成功 #define ERROR 0 // 执行失败 #define TRUE 1 // 返回值为真 #define FALSE 0 // 返回值为假 typedef int Status; // 执行状态(OK.ERROR) typedef int Boolean; // 布尔值(TRUE.FALSE) typedef char VertexType; // 顶点元素类型 typed

Dearboy&#39;s Puzzle (poj 2308 搜索 dfs+bfs)

Language: Default Dearboy's Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1202   Accepted: 208 Description Dearboy is a game lover. Recently, he loves playing the game Lian Lian Kan. This game is played on a board with N*M grids

HDU 4771 Stealing Harry Potter&#39;s Precious dfs+bfs

Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, his invisible robe, his wand and his owl. When Hogwarts school is in holiday, Harry Potter has to go back to uncle Vernon's home. But he can't bring his

Dfs/Bfs/记忆化搜索问题 | 问题集合

写在前面 动归和搜索似乎我打得特憋懒. 可能是因为搜索打的太少了??? 然后之前做过的一些题我就不再写了,比如填涂颜色/海战啥的? 然后每一题打两种解法(:Dfs/Bfs 前提是在题目里两种都能A P1596 湖计数 题目描述 Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <=

【dfs/bfs+set+快速幂】swjtuOJ 2094

[dfs/bfs+set+快速幂]swjtuOJ 2094 [注:交大的看到这篇文章要学会自己写,不要为了比赛而比赛!~] 题目大意 问题一:主人公去度假,问经过a^b天后是星期几(简单题) 问题二:一个天平,n个重物,每个物体的重量wi已知,问能称出的所有重量有多少种? 问题二要注意到天平两侧都可以放重物,每一个重物的权值都可以赋值为w,0,-w,相当于三分,我们知道二分可以用二进制位运算进行枚举,例如:枚举所有子集 int j,k,top=0; int t = 1 << n; for(in

POJ 3083 -- Children of the Candy Corn(DFS+BFS)TLE

POJ 3083 -- Children of the Candy Corn(DFS+BFS) 题意: 给定一个迷宫,S是起点,E是终点,#是墙不可走,.可以走 1)先输出左转优先时,从S到E的步数 2)再输出右转优先时,从S到E的步数 3)最后输出S到E的最短步数 解题思路: 前两问DFS,转向只要控制一下旋转方向就可以 首先设置前进方向对应的数字 向上--N--0 向右--E--1 向下--S--2 向左--W--3 比如说右转优先,即为向右,向前,向左,向后,即逆时针方向for(int i

FZU1205/SDUT1157_小鼠迷宫问题(DFS+BFS)

解题报告 http://blog.csdn.net/juncoder/article/details/38146041 题目传送门 题意 求最短路和最短路的路数. 思路: BFS+DFS,先求出最短路.在DFS搜等于最短路的条数. 不加优化SDUTOJ过了,数据就是水. 确定了最短路的长度,加上奇偶剪枝FOJ也过了. #include <queue> #include <cmath> #include <cstdio> #include <cstring>

poj3083——dfs+bfs综合题

POJ 3083   dfs+bfs+模拟 Children of the Candy Corn Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10564   Accepted: 4539 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through

POJ 3083:Children of the Candy Corn(DFS+BFS)

Children of the Candy Corn Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9311 Accepted: 4039 Description The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, ch