c语言深度搜索
⑴ 初学编程,八数码c语言深度搜索程序编译通不过,求高手们帮帮忙--急
帮你改好了,一共有8个错误:
主要是:
1. output()函数少了一个花括号
2. 一些该传地址的地方传了值
3. 有一个变量没定义:eight()函数中的open_link_point,我先改为open_point了
下面是改后的代码,改的地方我用/////////////注明了。
#include<stdio.h> 
#include<math.h> 
#include<stdlib.h> 
#define max_layer 5 /*嵟戝?揥?悢岹掕?*/ 
#define true 1 
#define fail 0 
#define null 0 
struct link 
{ 
int data[3][3];/*敧悢?忬?*/ 
int layer; /*??揰揑?悢*/ 
struct link *next; 
struct link *prior; 
}; 
struct link *close_head; /*Close昞揑崻?揰*/ 
struct link *open_head; /*Open昞揑崻?揰*/ 
/*****************************************************/ 
/* 敓悢柤徧丗output() */ 
/* 岟擞?柧丗?弌巜?P巜岦揑?揰揑悢悩 */ 
/****************************************************/ 
void output(struct link *p) 
{ 
int i,j; 
while(p!=NULL) 
{for(i=0;i<3;i++) /*岘?弌峊惂*/ 
{ 
for(j=0;j<3;j++) /*楍?弌峊惂*/ 
printf("%d ",p->data[i][j]);/*?弌i岘j楍忋揑悢悩*/ 
printf("\n"); /*??弌堦岘悢悩丆夞??岘*/ 
} 
printf("---------------------\n"); /*?弌堦忦墶?埲嬫旸洜枊忋懘懠?揰悢悩*/ 
p--; 
} 
}////////////////////1
/* 敓悢柤徧丗compare*/ 
/* 岟擞?柧丗彨巜?Operate巜岦揑?揰拞揑悢悩梌择?悢?dest拞揑悢悩?岘斾?*/ 
int compare(struct link *q,int dest[3][3]) 
{ 
int i,j,count=0; 
for(i=0;i<3;i++) /*岘斾?峊惂*/ 
{ 
for(j=0;j<3;j++) /*楍斾?峊惂*/ 
{ 
if(q->data[i][j]==dest[i][j])/*斾?i岘j楍忋揑悢悩*/ 
count++; /*?悢婍壛堦*/ 
else /*扅梫??桳堦槩悢悩晄憡摍*/ 
{ 
/*懄晓夞 fail丆愰崘斾?幐?*/ 
j=3; /*?惂悇弌for弞?*/ 
i=3; /*?惂悇弌for弞?*/ 
return 0; 
} 
} 
} 
if(count==9)/*憡摍揑悢悩揑槩悢梌?悢暯曽憡摍*/ 
return 1; /*昞帵悢悩搒??憡摍丆晓夞true */ 
}
/* 敓悢柤徧丗eight()*/ 
/* 岟擞?柧丗捠?怺搙?揥揑曽帏漄弌敧悢???樃弶峦忬?摓栚?忬?揑楬宎 */ 
int eight(struct link *open_head,int dest[3][3]) 
{ 
int i,j,zero_x,zero_y; /*0揑墶嵖?丆0揑?嵖?*/ 
struct link *new_point; /*?栋open昞揑堦槩??巜?*/ 
struct link *open_point=open_head;/*open昞憖嶌巜?1*/ ////////////2
struct link *close_point; 
while(open_point!=NULL) ///////////////////3open_link_point
{ 
close_point=open_point; 
open_point->prior->next=NULL; 
open_point--; 
if(compare(close_point,dest)==1) 
{ 
printf("find solution"); 
output(close_point); 
return 1; 
} 
else 
{ 
if(close_point->layer>max_layer) 
{ 
close_point->next=open_point; ////////////4
close_point++; 
} 
else 
{ 
for(i=0;i<3;i++)/*?庢0揑嵖?*/ 
{ 
for(j=0;j<3;j++) 
{ 
if(close_point->data[i][j]==0) /*data or dest*/ 
{ 
zero_x=i;/*墶嵖?*/ 
zero_y=j;/*?嵖?*/ 
j=3; /*?惂戅弌弞?*/ 
i=3; /*?惂戅弌弞?*/ 
} 
} 
} 
if((zero_x-1)>=0)/*墲忋堏*/ 
{ /*怽?撪懚嬻?*/ 
new_point=(struct link *)malloc(sizeof(struct link)); 
for(i=0;i<3;i++)/*?怴?揥揑?揰??*/ 
{ 
for(j=0;j<3;j++) 
new_point->data[i][j]=close_point->data[i][j]; 
} 
new_point->data[zero_x][zero_y]=new_point->data[zero_x-1][zero_y]; 
new_point->data[zero_x-1][zero_y]=0; 
open_point->next=new_point; ////////////////5
open_point++; 
} 
if((zero_x+1)<3)/*墲压堏*/ 
{ /*怽?撪懚嬻?*/ 
new_point=(struct link *)malloc(sizeof(struct link)); 
for(i=0;i<3;i++)/*?怴?揥揑?揰??*/ 
{ 
for(j=0;j<3;j++) 
new_point->data[i][j]=close_point->data[i][j]; 
} 
new_point->data[zero_x][zero_y]=new_point->data[zero_x+1][zero_y]; 
new_point->data[zero_x+1][zero_y]=0; 
open_point->next=new_point;///////////////6 
open_point++; 
} 
if((zero_y-1)>=0)/*0墲塃堏*/ 
{ /*怽?撪懚嬻?*/ 
new_point=(struct link *)malloc(sizeof(struct link)); 
for(i=0;i<3;i++)/*?怴?揥揑?揰??*/ 
{ 
for(j=0;j<3;j++) 
new_point->data[i][j]=close_point->data[i][j]; 
} 
new_point->data[zero_x][zero_y]=new_point->data[zero_x][zero_y-1]; 
new_point->data[zero_x][zero_y-1]=0; 
open_point->next=new_point;/////////////////////// 7
open_point++; 
} 
if((zero_y+1)<3)/*0墲嵍堏*/ 
{ /*怽?撪懚嬻?*/ 
new_point=(struct link *)malloc(sizeof(struct link)); 
for(i=0;i<3;i++)/*?怴?揥揑?揰??*/ 
{ 
for(j=0;j<3;j++) 
new_point->data[i][j]=close_point->data[i][j]; 
} 
new_point->data[zero_x][zero_y]=new_point->data[zero_x][zero_y+1]; 
new_point->data[zero_x][zero_y+1]=0; 
open_point->next=new_point;////////////////////////8 
open_point++; 
} 
} 
} 
} 
if(open_point=NULL) 
printf("no solution"); 
} 
/* 敓悢柤徧丗main*/ 
void main() 
{ 
int i,j; 
int destination[3][3]; /*择?悢?丆梡埲懚曻栚?忬?*/ 
struct link *open_head=(struct link *)malloc(sizeof(struct link)); /*怽?堦槩?揰嬻?* 
printf("The max dimention is 3"); /*采恽梡?敧悢?揑?悢戝雕*/ 
printf("Please input the initial state of <eight puzzle>:\n"); 
for(i=0;i<3;i++) /*?庢敧悢?揑弶峦忬?*/ 
{ 
for(j=0;j<3;j++) 
scanf("%d",&open_head->data[i][j]); /*攃弶峦忬?懚曻嵼怽?揑?揰拞丆懄Open昞*/ 
} 
printf("Please input the final state of <eight puzzle>:\n"); 
for(i=0;i<3;i++) /*?庢敧悢?揑栚?忬?*/ 
{ 
for(j=0;j<3;j++) 
scanf("%d",&destination[i][j]); /*攃栚?忬?悢悩懚曻嵼destination[][]拞*/ 
} 
open_head->layer=0; /*弶峦壔弶峦忬??揰揑?悢?0丆昞帵???岘?揥 */ 
eight(open_head,destination); 
}
⑵ 谁有数据结构c语言版的深度优先搜索和广度优先搜索源代码
include;iostream.h;const int n=8;const int e=15;typedef int elemtype ;bool visited[n+1];class link{public: elemtype data; link *next;};class graph{public: link a[n+1]; void creatlink() { int i,j,k; link *s; for(i=1;i=n;i++) { a[i].data=i; a[i].next=NULL; } for(k=1;k=e;k++) { cout;请输入一条边;; cin;;i;;j; coutendl; s=new link; s-;data=j; s-;next=a[i].next; a[j].next=s; } } void dfs1(int i) { link *p; couta[i].data; ;; visited[i]=true; p=a[i].next; while(p!=NULL) { if(!visited[p-;data]) dfs1(p-;data); p=p-;next; } } void bfs1(int i) { int q[n+1]; int f,r; link *p; f=r=0; couta[i].data; ;; visited[i]=true; r++;q[r]=i; while(fr) { f++;i=q[r]; p=a[i].next; while(p!=NULL) { if(!visited[p-;data]) { couta[p-;data].data; ;; visited[p-;data]=true; r++;q[r]=p-;data; } p=p-;next; } } }};void main(){ link *p;int yn=1; graph g; g.creatlink(); while(yn){ for(int i=1;i=n;i++) { p=g.a[i].next; coutg.a[i].data;-;;; while(p-;next!=NULL) { coutp-;data;-;;; p=p-;next; } coutp-;dataendl; } for(i=1;i=n;i++) visited[i]=false; cout;输入深度优先搜索开始访问的顶点;; cin;;i; coutendl; cout;从;i;出发的深度优先搜索遍历序列为;endl; g.dfs1(i); coutendl; for(i=1;i=n;i++) visited[i]=false; cout;请输入广度优先搜索开始访问的顶点;; cin;;i; coutendl; cout;从;i;出发的广度优先搜索遍历序列为;endl; g.bfs1(i); coutendl; cout;继续遍历吗(1/0)?;; cin;;yn; }}/*图为:1 2 35 46 7 8*/
⑶ 求一个C语言编程,图的遍历,深度优先和广度优先搜索的程序。要浅显易懂的~~~~
给你一个作为参考吧
#include <iostream> 
#define INFINITY 32767 
#define MAX_VEX 20 //最大顶点个数 
#define QUEUE_SIZE (MAX_VEX+1) //队列长度 
using namespace std; 
bool *visited; //访问标志数组 
//图的邻接矩阵存储结构 
typedef struct{ 
char *vexs; //顶点向量 
int arcs[MAX_VEX][MAX_VEX]; //邻接矩阵 
int vexnum,arcnum; //图的当前顶点数和弧数 
}Graph; 
//队列类 
class Queue{ 
public: 
void InitQueue(){ 
base=(int *)malloc(QUEUE_SIZE*sizeof(int)); 
front=rear=0; 
} 
void EnQueue(int e){ 
base[rear]=e; 
rear=(rear+1)%QUEUE_SIZE; 
} 
void DeQueue(int &e){ 
e=base[front]; 
front=(front+1)%QUEUE_SIZE; 
} 
public: 
int *base; 
int front; 
int rear; 
}; 
//图G中查找元素c的位置 
int Locate(Graph G,char c){ 
for(int i=0;i<G.vexnum;i++) 
if(G.vexs[i]==c) return i; 
return -1; 
} 
//创建无向网 
void CreateUDN(Graph &G){ 
int i,j,w,s1,s2; 
char a,b,temp; 
printf("输入顶点数和弧数:"); 
scanf("%d%d",&G.vexnum,&G.arcnum); 
temp=getchar(); //接收回车 
G.vexs=(char *)malloc(G.vexnum*sizeof(char)); //分配顶点数目 
printf("输入%d个顶点.\n",G.vexnum); 
for(i=0;i<G.vexnum;i++){ //初始化顶点 
printf("输入顶点%d:",i); 
scanf("%c",&G.vexs[i]); 
temp=getchar(); //接收回车 
} 
for(i=0;i<G.vexnum;i++) //初始化邻接矩阵 
for(j=0;j<G.vexnum;j++) 
G.arcs[i][j]=INFINITY; 
printf("输入%d条弧.\n",G.arcnum); 
for(i=0;i<G.arcnum;i++){ //初始化弧 
printf("输入弧%d:",i); 
scanf("%c %c %d",&a,&b,&w); //输入一条边依附的顶点和权值 
temp=getchar(); //接收回车 
s1=Locate(G,a); 
s2=Locate(G,b); 
G.arcs[s1][s2]=G.arcs[s2][s1]=w; 
} 
} 
//图G中顶点k的第一个邻接顶点 
int FirstVex(Graph G,int k){ 
if(k>=0 && k<G.vexnum){ //k合理 
for(int i=0;i<G.vexnum;i++) 
if(G.arcs[k][i]!=INFINITY) return i; 
} 
return -1; 
} 
//图G中顶点i的第j个邻接顶点的下一个邻接顶点 
int NextVex(Graph G,int i,int j){ 
if(i>=0 && i<G.vexnum && j>=0 && j<G.vexnum){ //i,j合理 
for(int k=j+1;k<G.vexnum;k++) 
if(G.arcs[i][k]!=INFINITY) return k; 
} 
return -1; 
} 
//深度优先遍历 
void DFS(Graph G,int k){ 
int i; 
if(k==-1){ //第一次执行DFS时,k为-1 
for(i=0;i<G.vexnum;i++) 
if(!visited[i]) DFS(G,i); //对尚未访问的顶点调用DFS 
} 
else{ 
visited[k]=true; 
printf("%c ",G.vexs[k]); //访问第k个顶点 
for(i=FirstVex(G,k);i>=0;i=NextVex(G,k,i)) 
if(!visited[i]) DFS(G,i); //对k的尚未访问的邻接顶点i递归调用DFS 
} 
} 
//广度优先遍历 
void BFS(Graph G){ 
int k; 
Queue Q; //辅助队列Q 
Q.InitQueue(); 
for(int i=0;i<G.vexnum;i++) 
if(!visited[i]){ //i尚未访问 
visited[i]=true; 
printf("%c ",G.vexs[i]); 
Q.EnQueue(i); //i入列 
while(Q.front!=Q.rear){ 
Q.DeQueue(k); //队头元素出列并置为k 
for(int w=FirstVex(G,k);w>=0;w=NextVex(G,k,w)) 
if(!visited[w]){ //w为k的尚未访问的邻接顶点 
visited[w]=true; 
printf("%c ",G.vexs[w]); 
Q.EnQueue(w); 
} 
} 
} 
} 
//主函数 
void main(){ 
int i; 
Graph G; 
CreateUDN(G); 
visited=(bool *)malloc(G.vexnum*sizeof(bool)); 
printf("\n广度优先遍历: "); 
for(i=0;i<G.vexnum;i++) 
visited[i]=false; 
DFS(G,-1); 
printf("\n深度优先遍历: "); 
for(i=0;i<G.vexnum;i++) 
visited[i]=false; 
BFS(G); 
printf("\n程序结束.\n"); 
}
⑷ 迷宫最短路径 深度搜索问题 如果找不到出路要输出一个负一应该怎么办c语言问题啊
C语言问题还是真的比较难的。
⑸ 求用到深度搜索,广度所搜的游戏代码--迷宫(C语言)
#include <stdio.h> 
#include <stdlib.h> 
#include <malloc.h> 
#include <graphics.h> 
#include <bios.h> 
#include <conio.h> 
#include <dos.h> 
#define OK 1 
#define ERROR 0 
#define TRUE 1 
#define FALSE 0 
#define F9 0x43 
#define Esc 0x1b 
#define Del 0x53 
#define Home 0x47 
#define End 0x4f 
#define Space 0x20 
#define Up 0x48 
#define Down 0x50 
#define Left 0x4b 
#define Right 0x4d 
#define Enter 0x0d 
#define BKCOLOR LIGHTBLUE 
#define WALLCOLOR RED 
#define CURSORCOLOR BLUE 
#define CIRCLECOLOR CYAN 
#define ERRORCOLOR CYAN 
#define TEXTCOLOR 14 
#define STACK_INIT_SIZE 200 
#define STACKINCREMENT 10 
typedef int Boolean; 
typedef int Status; 
typedef struct { 
int x; 
int y; 
} PosType; 
typedef struct { 
int ord; 
PosType seat; 
int di; 
} SElemType; 
typedef struct { 
int td; 
int foot; 
int mark; 
} MazeType; 
typedef struct { 
SElemType *base; 
SElemType *top; 
int stacksize; 
} Stack; 
int Maze[16][17]; 
MazeType maze[16][17]; 
PosType StartPlace; 
PosType EndPlace; 
void Paint(void); 
void CreatMaze(void); 
void DrawWall(int cx,int cy,int color); 
void DrawCursor(int cx,int cy,int color); 
void Refresh(void); 
void Error(char *message); 
Status InitStack(Stack *s); 
Status DestroyStack(Stack *s); 
Status ClearStack(Stack *s); 
Boolean StackEmpty(Stack *s); 
int StackLength(Stack *s); 
Status Push(Stack *s,SElemType e); 
SElemType Pop(Stack *s,SElemType e); 
Status GetTop(Stack *s,SElemType *e); 
Status StackTraverse(Stack *s,Status (* visit)(SElemType *se)); 
Boolean Pass(PosType curpos); 
void MarkPrint(PosType seat); 
void FootPrint(PosType curpos); 
PosType NextPos(PosType seat,int di); 
Status MazePath(PosType start,PosType end); 
void Paint(void) 
{ 
int i,j; 
int gdriver=DETECT,gmode; 
initgraph(&gdriver,&gmode,".\\"); 
setbkcolor(LIGHTBLUE); 
setcolor(10); 
outtextxy(190,10,"MazePath Game"); 
setcolor(RED); 
for(i=30;i<=450;i+=30) 
line(30,i,480,i); 
for(j=30;j<=480;j+=30) 
line(j,30,j,450); 
rectangle(29,29,481,451); 
rectangle(28,28,482,452); 
rectangle(490,320,630,470); 
setcolor(1); 
outtextxy(500,100,"Complete:F9"); 
outtextxy(500,120,"Start:Home"); 
outtextxy(500,140,"End:End"); 
outtextxy(500,160,"Draw wall:Enter"); 
outtextxy(500,180,"Delete wall:Del"); 
outtextxy(500,200,"Exit:Esc"); 
outtextxy(500,300,"Message:"); 
for(i=0;i<16;i++) 
for(j=0;j<17;j++) 
Maze[j][i]=0; 
for(i=0;i<17;i++) 
{ 
Maze[0][i]=1; 
Maze[15][i]=1; 
} 
for(i=0;i<16;i++) 
{ 
Maze[i][0]=1; 
Maze[i][16]=1; 
} 
} /* Paint */ 
void DrawCursor(int cx,int cy,int color) 
{ 
setcolor(color); 
rectangle(cx-7,cy-7,cx+7,cy+7); 
} /* DrawCursor */ 
void DrawWall(int x,int y,int color) 
{ 
void DrawCursor(int cx,int cy,int color); 
register int i; 
setcolor(color); 
x*=30; y*=30; 
for(i=y;i<=y+30;i++) 
line(x,i,x+30,i); 
for(i=x;i<=x+30;i++) 
line(i,y,i,y+30); 
DrawCursor(x+15,y+15,CURSORCOLOR); 
} /* DrawWall */ 
void Refresh(void) 
{ 
register int i,j; 
setcolor(BKCOLOR); 
for(i=1;i<=480;i++) 
line(1,i,480,i); 
for(j=1;j<=480;j++) 
line(i,1,i,480); 
setcolor(WALLCOLOR); 
rectangle(29,29,481,451); 
rectangle(28,28,482,452); 
rectangle(30,30,480,450); 
for(i=1;i<=15;i++) 
for(j=1;j<=14;j++) 
if(Maze[j][i]) { 
DrawWall(i,j,WALLCOLOR); 
DrawCursor(i*30+15,j*30+15,WALLCOLOR); 
} 
setcolor(CIRCLECOLOR); 
circle(StartPlace.x*30+15,StartPlace.y*30+15,6); 
circle(EndPlace.x*30+15,EndPlace.y*30+15,6); 
setcolor(BKCOLOR); 
for(i=491;i<=629;i++) 
line(i,321,i,469); 
setcolor(BROWN); 
setfillstyle(SOLID_FILL,BROWN); 
fillellipse(StartPlace.x*30+15,StartPlace.y*30+15,6,6); 
setcolor(CYAN); 
setfillstyle(SOLID_FILL,CYAN); 
fillellipse(EndPlace.x*30+15,EndPlace.y*30+15,6,6); 
} /* Refresh */ 
void CreatMaze(void) 
{ 
void Error(char *message); 
char c; 
int i,j; 
Boolean flag=TRUE; 
int x=1,y=1; 
Boolean start=FALSE,end=FALSE; 
DrawCursor(45,45,CURSORCOLOR); 
do 
{ 
c=getch(); 
switch(c) 
{ 
case Up: if(y>1) { 
if(!Maze[y][x]) 
DrawCursor(x*30+15,y*30+15,BKCOLOR); 
else DrawCursor(x*30+15,y*30+15,WALLCOLOR); 
DrawCursor(x*30+15,(--y)*30+15,CURSORCOLOR); } 
break; 
case Down: if(y<14) { 
if(!Maze[y][x]) 
DrawCursor(x*30+15,y*30+15,BKCOLOR); 
else DrawCursor(x*30+15,y*30+15,WALLCOLOR); 
DrawCursor(x*30+15,(++y)*30+15,CURSORCOLOR); } 
break; 
case Left: if(x>1) { 
if(!Maze[y][x]) 
DrawCursor(x*30+15,y*30+15,BKCOLOR); 
else DrawCursor(x*30+15,y*30+15,WALLCOLOR); 
DrawCursor((--x)*30+15,y*30+15,CURSORCOLOR); } 
break; 
case Right: if(x<15) { 
if(!Maze[y][x]) 
DrawCursor(x*30+15,y*30+15,BKCOLOR); 
else DrawCursor(x*30+15,y*30+15,WALLCOLOR); 
DrawCursor((++x)*30+15,y*30+15,CURSORCOLOR); } 
break; 
case Home: if(!start&&!Maze[y][x]) { 
setcolor(6); 
setfillstyle(SOLID_FILL,6); 
fillellipse(x*30+15,y*30+15,6,6); 
start=TRUE; StartPlace.x=x;StartPlace.y=y; } 
break; 
case End: if(!end&&!Maze[y][x]) { 
setcolor(CYAN); 
setfillstyle(SOLID_FILL,CYAN); 
fillellipse(x*30+15,y*30+15,6,6); 
end=TRUE; EndPlace.x=x; EndPlace.y=y; } 
break; 
case Esc: setcolor(TEXTCOLOR); 
outtextxy(540,380,"exit"); 
sleep(1); 
closegraph(); 
exit(1); 
case F9: if(start&&end) { Refresh(); flag=FALSE; } 
else 
{ 
setcolor(TEXTCOLOR); 
outtextxy(493,323," Error: "); 
outtextxy(493,343," You must set "); 
if(!start) outtextxy(493,363," startplace. "); 
else outtextxy(493,363," endplace."); 
} 
break; 
case Enter: if(!(x==StartPlace.x&&y==StartPlace.y)&& 
!(x==EndPlace.x&&y==EndPlace.y)) 
{ 
DrawWall(x,y,WALLCOLOR); 
Maze[y][x]=1; 
} 
break; 
case Del: DrawWall(x,y,BKCOLOR); setcolor(4); 
rectangle(x*30,y*30,x*30+30,y*30+30); 
Maze[y][x]=0; 
if(x==StartPlace.x&&y==StartPlace.y) 
{ 
StartPlace.x=0; 
StartPlace.y=0; 
start=FALSE; 
} 
if(x==EndPlace.x&&y==EndPlace.y) 
{ 
EndPlace.x=0; 
EndPlace.y=0; 
end=FALSE; 
} 
break; 
default: break; 
} 
} 
while(flag); 
for(i=0;i<17;i++) 
for(j=0;j<16;j++) 
{ 
maze[j][i].td=1-Maze[j][i]; 
maze[j][i].mark=0; 
maze[j][i].foot=0; 
} 
} /* CreatMaze */ 
void Error(char *message) 
{ 
closegraph(); 
fprintf(stderr,"Error:%s\n",message); 
exit(1); 
} /* Error */ 
Status InitStack(Stack *s) 
{ 
s->base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); 
if(!s->base) Error("Overflow"); 
s->top=s->base; 
s->stacksize=STACK_INIT_SIZE; 
return OK; 
} /* InitStack */ 
Status DestroyStack(Stack *s) 
{ 
s->top=NULL; 
s->stacksize=0; 
free(s->base); 
s->base=NULL; 
return OK; 
} /* DestroyStack */ 
Status ClearStack(Stack *s) 
{ 
s->top=s->base; 
s->stacksize=STACK_INIT_SIZE; 
return OK; 
} /* ClearStack */ 
Boolean StackEmpty(Stack *s) 
{ 
if(s->top==s->base) return TRUE; 
else return FALSE; 
} /* StackEmpty */ 
int StackLength(Stack *s) 
{ 
if(s->top>s->base) return (int)(s->top-s->base); 
else return 0; 
} /* StackLength */ 
Status Push(Stack *s,SElemType e) 
{ 
if(s->top-s->base>=s->stacksize) 
{ 
s->base=(SElemType *)realloc(s->base, 
(s->stacksize+STACKINCREMENT)*sizeof(SElemType)); 
if(!s->base) Error("Overflow"); 
s->top=s->base+s->stacksize; 
s->stacksize+=STACKINCREMENT; 
} 
*s->top++=e; 
return OK; 
} /* Push */ 
SElemType Pop(Stack *s,SElemType e) 
{ 
if(s->top==s->base) Error("Pop"); 
e=*--s->top; 
return e; 
} /* Pop */ 
Status GetTop(Stack *s,SElemType *e) 
{ 
if(s->top==s->base) Error("GetTop"); 
*e=*(s->top-1); 
return OK; 
} /* GetTop */ 
/*Status StackTraverse(Stack *s,Status (* visit)(SElemType *se)) 
{ 
SElemType p; 
int result; 
if(s->top==s->base) return ERROR; 
p=s->base; 
while(!(p==s->top)) 
{ 
result=(*visit)(p); 
p++; 
} 
return OK; 
} */ 
Boolean Pass(PosType curpos) 
{ 
if(maze[curpos.x][curpos.y].td==1&& 
maze[curpos.x][curpos.y].foot==0&&maze[curpos.x][curpos.y].mark==0) 
return TRUE; 
else return FALSE; 
} /* Pass */ 
void MarkPrint(PosType seat) 
{ 
maze[seat.x][seat.y].mark=-1; 
} /* MarkPrint */ 
void FootPrint(PosType curpos) 
{ 
maze[curpos.x][curpos.y].foot=1; 
} /* FootPrint */ 
PosType NextPos(PosType seat,int di) 
{ 
switch(di) 
{ 
case 1: seat.y++; return seat; 
case 2: seat.x++; return seat; 
case 3: seat.y--; return seat; 
case 4: seat.x--; return seat; 
default: seat.x=0; seat.y=0; return seat; 
} 
} /* NextPos */ 
Status MazePath(PosType start,PosType end) 
{ 
PosType curpos; 
int curstep; 
SElemType e; 
Stack *s=NULL,stack; 
stack.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); 
if(!stack.base) Error("Overflow"); 
stack.top=stack.base; 
stack.stacksize=STACK_INIT_SIZE; 
s=&stack; 
curpos=start; 
curstep=1; 
do 
{ 
if(Pass(curpos)) 
{ 
FootPrint(curpos); 
e.ord=curstep; e.seat=curpos; e.di=1; 
setcolor(BLUE); 
circle(curpos.y*30+15,curpos.x*30+15,6); 
delay(8000); 
Push(s,e); 
if(curpos.x==end.x&&curpos.y==end.y) 
{ 
DestroyStack(s); 
return TRUE; 
} 
curpos=NextPos(curpos,1); 
curstep++; 
} 
else 
{ 
if(!StackEmpty(s)) 
{ 
e=Pop(s,e); 
while(e.di==4&&!StackEmpty(s)) 
{ 
MarkPrint(e.seat); 
setcolor(BLUE); 
circle(e.seat.y*30+15,e.seat.x*30+15,6); 
delay(8000); 
setcolor(BKCOLOR); 
circle(e.seat.y*30+15,e.seat.x*30+15,6); 
e=Pop(s,e); 
curstep--; 
} 
if(e.di<4) 
{ 
e.di++; 
Push(s,e); 
curpos=NextPos(e.seat,e.di); 
} 
} 
} 
} 
while(!StackEmpty(s)); 
DestroyStack(s); 
return FALSE; 
} /* MazePath */ 
void main(void) 
{ 
PosType start,end; 
Paint(); 
CreatMaze(); 
start.x=StartPlace.y; 
start.y=StartPlace.x; 
end.x=EndPlace.y; 
end.y=EndPlace.x; 
if(MazePath(start,end)) 
{ 
setcolor(TEXTCOLOR); 
outtextxy(520,380,"Path found"); 
} 
else 
{ 
setcolor(TEXTCOLOR); 
outtextxy(500,380,"Path not found"); 
} 
while(bioskey(1)==0); 
}
⑹ C语言 图 邻接矩阵 深度优先遍历 DFS搜索
DFS(g,j);
DFSL(ga,p->adjvex);
除了上面两句话,其他没什么问题,首先如果图不连通,当你用从某一点遍历的方法,本身就没办法遍历整个图
⑺ c语言深度优先搜索。代码
#include<stdlib.h>
#include<stdio.h>
structnode/*图顶点结构定义*/
{
intvertex;/*顶点数据信息*/
structnode*nextnode;/*指下一顶点的指标*/
};
typedefstructnode*graph;/*图形的结构新型态*/
structnodehead[9];/*图形顶点数组*/
intvisited[9];/*遍历标记数组*/
/********************根据已有的信息建立邻接表********************/
voidcreategraph(intnode[20][2],intnum)/*num指的是图的边数*/
{
graphnewnode;/*指向新节点的指针定义*/
graphptr;
intfrom;/*边的起点*/
intto;/*边的终点*/
inti;
for(i=0;i<num;i++)/*读取边线信息,插入邻接表*/
{
from=node[i][0];/*边线的起点*/
to=node[i][1];/*边线的终点*/
/*建立新顶点*/
newnode=(graph)malloc(sizeof(structnode));
newnode->vertex=to;/*建立顶点内容*/
newnode->nextnode=NULL;/*设定指标初值*/
ptr=&(head[from]);/*顶点位置*/
while(ptr->nextnode!=NULL)/*遍历至链表尾*/
ptr=ptr->nextnode;/*下一个顶点*/
ptr->nextnode=newnode;/*插入节点*/
}
}
/**********************图的深度优先搜寻法********************/
voiddfs(intcurrent)
{
graphptr;
visited[current]=1;/*记录已遍历过*/
printf("vertex[%d] ",current);/*输出遍历顶点值*/
ptr=head[current].nextnode;/*顶点位置*/
while(ptr!=NULL)/*遍历至链表尾*/
{
if(visited[ptr->vertex]==0)/*如过没遍历过*/
dfs(ptr->vertex);/*递回遍历呼叫*/
ptr=ptr->nextnode;/*下一个顶点*/
}
}
/******************************主程序******************************/
intmain()
{
graphptr;
intnode[20][2]={{1,2},{2,1},/*边线数组*/
{1,3},{3,1},
{1,4},{4,1},
{2,5},{5,2},
{2,6},{6,2},
{3,7},{7,3},
{4,7},{4,4},
{5,8},{8,5},
{6,7},{7,6},
{7,8},{8,7}};
inti;
//clrscr();
for(i=1;i<=8;i++)/*顶点数组初始化*/
{
head[i].vertex=i;/*设定顶点值*/
head[i].nextnode=NULL;/*指针为空*/
visited[i]=0;/*设定遍历初始标志*/
}
creategraph(node,20);/*建立邻接表*/
printf("Contentofthegragh'sADlistis: ");
for(i=1;i<=8;i++)
{
printf("vertex%d->",head[i].vertex);/*顶点值*/
ptr=head[i].nextnode;/*顶点位置*/
while(ptr!=NULL)/*遍历至链表尾*/
{
printf("%d",ptr->vertex);/*印出顶点内容*/
ptr=ptr->nextnode;/*下一个顶点*/
}
printf(" ");/*换行*/
}
printf(" Theendofthedfsare: ");
dfs(1);/*打印输出遍历过程*/
printf(" ");/*换行*/
puts("Pressanykeytoquit...");
//getch();
}
⑻ C语言数据结构算法,连通图的深度优先搜索,存储结构是邻接矩阵,空怎么填啊
voiddfs(inta[][],intv,intn)
{
access(v);
visited[v]=1;
w=0;
while(w<=n&&a[v][w]==0)w++;
while(w<=n)
{
if(visited[w]==0)dfs(a,w,n);
w++;
while((w<=n)&&a[v][w]==0)w++;
}
}
第一空:visited[w]==0
第二空:dfs(a,w,n)
⑼ c语言图的遍历,邻接表存储,深度,广度优先遍历
(1)	图的建立,按采用邻接表作为存储结构。
(2)	从指定顶点出发进行深度优先搜索遍历。
(3)	从指定顶点出发进行广度优先搜索遍历。
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#define MAX_INT 1000
#define MAX_VERTEX_NUM 20
#define MAX_QUEUE_NUMBER 20
typedef struct ArcNode
{
	int adjvex;
	double adj;
	struct ArcNode *nextarc;
}ArcNode;
typedef struct VexNode
{
	char szName[40];
	ArcNode *firstarc;
}VexNode,AdjList[MAX_VERTEX_NUM];
typedef struct 
{
	AdjList vexs;
	int vexnum,arcnum;
}Net;
//定义队列
typedef struct{
	int *elem;
	int front, rear;
}Queue;
void InitQueue(Queue &Q)
{
	Q.elem = new int[MAX_QUEUE_NUMBER];
	Q.front = Q.rear = 0;
}
int EmptyQueue(Queue Q)
{
	if(Q.front==Q.rear)
		return 0;
	else 
		return 1;
}
void DestroyQueue(Queue &Q){
	delete []Q.elem;
	Q.front = Q.rear = 0;
}
void EnterQueue(Queue &Q, int e)
{
	if((Q.rear + 1)%MAX_QUEUE_NUMBER != Q.front)
		Q.elem[Q.rear ]	= e;
	else
		printf("队列满!\n");
	Q.rear = (Q.rear + 1)%MAX_QUEUE_NUMBER;
}
void LeaveQueue(Queue &Q, int &e)
{
	if(Q.rear != Q.front)
		e = Q.elem[Q.front];
	else
		printf("队列空!\n");
	Q.front = (Q.front+1)%MAX_QUEUE_NUMBER;
}
int LocateVex(Net ga,char *name)
{
	int i;
	for(i=0;i<ga.vexnum;i++)
		if(strcmp(name,ga.vexs[i].szName)==0)
			return i;
	return -1;
}
void crt_net(Net &ga)
{
	ArcNode *p;
	char name1[40],name2[40];
	int i,j,k;
	double w;
	printf("请输入顶点数和弧数:");
	scanf("%d%d",&ga.vexnum,&ga.arcnum);
	printf("请依次输入顶点名:\n");
	for(i=0;i<ga.vexnum;i++)
	{
		scanf("%s",ga.vexs[i].szName);
		ga.vexs[i].firstarc=NULL;
	}
	for(k=0;k<ga.arcnum;k++)
	{
		printf("请输入相邻的两个定点和权值:");
		scanf("%s%s%lf",name1,name2,&w);
		i=LocateVex(ga,name1);
		j=LocateVex(ga,name2);
		p=new ArcNode;
		p->adjvex=j;
		p->adj=w;
		p->nextarc=ga.vexs[i].firstarc;
		ga.vexs[i].firstarc=p;
	}
}
void DFS(Net ga,char *name,int *visited)
{
	int v,w; 
	ArcNode *p;
	v=LocateVex(ga,name);
	visited[v]=1;
	printf("%s ",ga.vexs[v].szName);
	p=ga.vexs[v].firstarc;
	while(p!=NULL)
	{
		w=p->adjvex;
		if(visited[w]==0)
			DFS(ga,ga.vexs[w].szName,visited);
		p=p->nextarc;
	}
}
void DFSTravel(Net ga,char *name)
{
	int v,k=0;
	int visited[20];
	for(v=0;v<ga.vexnum;v++)
		visited[v]=0;
	for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
	{
		if(v+1==LocateVex(ga,name))
			k++;
		if(visited[v]==0)
			DFS(ga,ga.vexs[v].szName,visited);
		
	}
}
void BFSTravel(Net ga,char *name)
{
	ArcNode *p;
	int v,w,u,k=0;
    Queue Q;
	int visited[20];
	for(v=0;v<ga.vexnum;v++)
		visited[v]=0;
	InitQueue(Q);
	for(v=LocateVex(ga,name);k!=2;v=(v+1)%(ga.vexnum-1))
	{
		if(v+1==LocateVex(ga,name))
			k++;
		if(visited[v]==0)
		{
			visited[v]=1;
			printf("%s ",ga.vexs[v].szName);
			EnterQueue(Q,v);
			while(EmptyQueue(Q)!=0)
			{
				LeaveQueue(Q,u);
				p=ga.vexs[u].firstarc;
				while(p!=NULL)
				{
					w=p->adjvex;
					if(visited[w]==0)
					{
						printf("%s ",ga.vexs[w].szName);
						visited[w]=1;
						EnterQueue(Q,w);
					}
					p=p->nextarc;
				}
			}
		}
	
	}
}
void main()
{
	char name[40];
	Net ga;
	crt_net(ga);
	printf("请输入深度优先遍历开始点的名:");
	scanf("%s",name);
	printf("深度优先遍历:");
	DFSTravel(ga,name);
	printf("\n");
	printf("请输入广度优先遍历开始点的名:");
	scanf("%s",name);
	printf("广度优先遍历:");
	BFSTravel(ga,name);
	printf("\n");
}
