當前位置:首頁 » 編程語言 » c語言深度搜索

c語言深度搜索

發布時間: 2022-04-14 06:53:05

⑴ 初學編程,八數碼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");

}

熱點內容
米號源碼 發布:2025-01-20 21:55:30 瀏覽:892
電信四川dns伺服器ip 發布:2025-01-20 21:54:51 瀏覽:91
電腦彈出腳本錯誤還能繼續使用嗎 發布:2025-01-20 21:42:29 瀏覽:585
安卓私密照片在哪裡 發布:2025-01-20 21:41:05 瀏覽:4
同濟復試編譯原理 發布:2025-01-20 21:33:54 瀏覽:309
c語言判斷字母 發布:2025-01-20 21:31:09 瀏覽:423
ftp伺服器搭建linux 發布:2025-01-20 21:26:05 瀏覽:334
安卓手機瀏覽器如何翻譯英文網頁 發布:2025-01-20 21:21:01 瀏覽:422
刺客信條梟雄怎麼調成低配置 發布:2025-01-20 21:20:51 瀏覽:709
nss存儲 發布:2025-01-20 21:04:47 瀏覽:35