-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
第七次作业广度优先遍历最短路径 #143
Open
springwintersummer
wants to merge
3
commits into
CUCCS:master
Choose a base branch
from
springwintersummer:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
第七次作业广度优先遍历最短路径 #143
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
#include<stdio.h> | ||
#include<stdlib.h> | ||
#include <malloc.h> | ||
|
||
#define INFINITY INT_MAX //���ֵΪ���� | ||
#define MAX_VERTEX_NUM 20 //����� | ||
|
||
typedef enum { | ||
DG,DN,UGD,UDN | ||
}GraphKind;//{����ͼ��������������ͼ��������} | ||
|
||
typedef int VRType, InfoType; | ||
|
||
typedef struct ArcCell { | ||
VRType adj;//VRType�Ƕ����ϵ���ͣ�����Ȩͼ����1��0 | ||
//��ʾ���ڷ���Ȩͼ����ΪȨֵ���� | ||
InfoType *info;//�û������Ϣ��ָ�� | ||
}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; | ||
|
||
typedef struct { | ||
AdjMatrix arcs; // �ڽӾ��� | ||
int vexnum, arcnum; // ͼ�ĵ�ǰ�������ͻ��� | ||
}Graph; | ||
|
||
typedef enum { | ||
OK, | ||
ERROR, | ||
OVERFLOW | ||
}Status; | ||
|
||
Status CreateGraph(Graph *G) { | ||
//�����ڽӾ���ʾ����������֪��ͼ | ||
G->vexnum = 9; //��ͼ�Ķ����� | ||
G->arcnum = 12; //��ͼ�Ļ��� | ||
for (int i = 0; i < G->vexnum; i++) { | ||
for (int j = 0; j < G->vexnum; j++) { | ||
G->arcs[i][j].adj = INFINITY; //��ʼ���ڽӾ���,INFINITY��ʾ������ | ||
} | ||
}//��ʼ��ͼ | ||
//���ڵĶ��㸳ֵ | ||
Add(G, 0, 1); Add(G, 0, 2); Add(G, 0, 3); Add(G, 0, 6); | ||
Add(G, 1, 2); Add(G, 3, 4); Add(G, 3, 5); Add(G, 4, 5); | ||
Add(G, 5, 7); Add(G, 6, 7); Add(G, 6, 8); Add(G, 7, 8); | ||
//��ʾ0-1,0-2.�������������� | ||
return OK; | ||
} | ||
|
||
Status Add(Graph*G, int x, int y) { | ||
//�����ڽӾ������ڸ�ֵΪ1 | ||
if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) { | ||
return ERROR;//��������ͼ�ķ�Χ���ͷ���ERROR | ||
} | ||
G->arcs[x][y].adj = G->arcs[y][x].adj = 1;//����ͼ�����ڽӶ�Ϊ1���ҵ���������ʱ��������ֵ | ||
return OK; | ||
} | ||
/*-------------ͼ������------------------*/ | ||
|
||
typedef int ElemType; | ||
|
||
#define MAXQSIZE 100 //�������� | ||
|
||
typedef struct QNode { | ||
ElemType data; | ||
struct QNode *priou;//�ڵ����һ���ڵ� | ||
struct QNode *next;//�ڵ����һ���ڵ� | ||
}QNode, LinkList, *QueuePtr; | ||
|
||
typedef struct { | ||
QueuePtr front;//ǰ��� | ||
QueuePtr rear;//����� | ||
}LinkQueue; | ||
|
||
Status InitQueue(LinkQueue *Q) { | ||
//���еij�ʼ�� | ||
Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode)); | ||
if (!(Q->front)) { | ||
return ERROR; | ||
} | ||
Q->front->next = Q->rear->next = NULL;//ʹ����Ϊ�� | ||
return OK; | ||
} | ||
|
||
Status EnQueue(LinkQueue *Q, ElemType e) { | ||
//����еIJ��� | ||
QueuePtr p; | ||
p = (QueuePtr)malloc(sizeof(QNode)); | ||
if (!p) { | ||
return ERROR; | ||
} | ||
p->data = e;//���浽����������� | ||
p->next = NULL;//��һ���ڵ�Ϊ�� | ||
p->priou = Q->front; | ||
Q->rear->next = p; | ||
Q->rear = p; | ||
return OK; | ||
} | ||
|
||
Status DeQueue(LinkQueue *Q, ElemType *e) { | ||
//������� | ||
if (QueueEmpty(*Q)) { | ||
//����Ϊ��ʱֹͣ����������ERROR | ||
return ERROR; | ||
} | ||
Q->front = Q->front->next;//ͷ���ָ��������һ���ڵ� | ||
*e = Q->front->data; | ||
return OK; | ||
} | ||
|
||
typedef enum { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 所有代码都 |
||
false, | ||
true | ||
}bool; | ||
|
||
bool QueueEmpty(LinkQueue Q) { | ||
//�ж����Dz���һ���ն��� | ||
if (Q.front == Q.rear) { | ||
return true;//ͷ���ͺ�һ���ڵ�Ϊͬһ������ô�������Ϊ�� | ||
} | ||
return false;//������в�Ϊ�� | ||
} | ||
|
||
int FirstAdjVex(Graph G, int i) { | ||
//���ص�һ���ڽӶ��㣬û�еĻ�����-1 | ||
for (int k = 0; k < G.vexnum; k++) { | ||
if (G.arcs[i][k].adj == 1) { | ||
return k;//������ڽӽڵ㣬��������ڽӵĽڵ� | ||
} | ||
} | ||
return -1; | ||
} | ||
|
||
int NextAdjVex(Graph G, int i, int j) { | ||
//������һ���ڽӶ��㣬û�еĻ�����-1 | ||
for (int k = j + 1; k < G.vexnum; k++) { | ||
if (G.arcs[i][k].adj == 1) { | ||
return k; | ||
} | ||
} | ||
return -1; | ||
} | ||
/*----------------���еIJ���------------*/ | ||
#define FALSE 0 | ||
#define TRUE 1 | ||
|
||
int visited[MAX_VERTEX_NUM]; | ||
|
||
void BFSTraverse(Graph G, int a, int b) { | ||
//��������ȵķǵݹ����ͼG��ʹ�ø�������Q�ͷ��ʱ�־����visited. | ||
LinkQueue Q; //�������� | ||
for (int v = 0; v < G.vexnum; ++v) { | ||
visited[v] = FALSE; //��ʼ�����ʱ�־ | ||
} | ||
InitQueue(&Q); // �ÿյĸ�������Q | ||
EnQueue(&Q, a); //������� | ||
visited[a] = TRUE; | ||
bool flag = false; | ||
int u, w; | ||
while (!QueueEmpty(Q)) //���в�Ϊ��ʱִ�� | ||
{ | ||
DeQueue(&Q, &u);//������� | ||
for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)) | ||
{ | ||
//���ν����ڶ������� | ||
if (w == b) { | ||
EnQueue(&Q, w); //������ | ||
PrintFoot(Q, a); //���·�� | ||
flag = true; | ||
break; | ||
}//if | ||
if (!visited[w]) { | ||
//wΪu��δ���ʵ��ڽӶ��� | ||
EnQueue(&Q, w); //����� | ||
visited[w] = true; | ||
}//if | ||
}//for | ||
if (flag) { | ||
break; | ||
}//if | ||
}//while | ||
}//BFSTraverse | ||
/*---------ͼ�ı���--------------*/ | ||
|
||
Status PrintFoot(LinkQueue Q, int start) { | ||
//������·�� | ||
int foot[MAX_VERTEX_NUM];///����� | ||
int i; | ||
QueuePtr p;//����һ������ | ||
p = Q.rear;//pָ���β�ڵ� | ||
for (i = 0; i < MAX_VERTEX_NUM; i++) { | ||
foot[i] = -1; | ||
}//���еij�ʼ�� | ||
foot[0] = p->data; | ||
p = p->priou; | ||
for (i = 1; p->data != start; i++) { | ||
foot[i] = p->data; | ||
p = p->priou; | ||
} | ||
foot[i] = p->data; | ||
for (; i >= 0; i--) { | ||
//������� | ||
if (foot[i] >= 0) { | ||
printf("%d ", foot[i] + 1); | ||
} | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
Graph G; | ||
CreateGraph(&G);//����һ��ͼͼ�����ҳ�ʼ����1 | ||
for (int i = 0; i < 9; i++) { | ||
for (int j = 0; j < 9; j++) { | ||
if (j != i) { | ||
printf("%d<->%d:", i + 1, j + 1); | ||
BFSTraverse(G, i, j);//��ȱ��� | ||
printf("\n"); | ||
}//if | ||
}//for j | ||
}//for i | ||
return 0; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
虽然就题论题你这里这样写是能得到正确答案的,但建议以后在实际应用编程时,尽量把这种参数配置类的工作交给调用者(函数)在调用函数时执行传参操作,代码的
封装
和可复用
就会一点点积累起来了。