A-C/ex5_1.c

131 lines
2.6 KiB
C
Raw Normal View History

2022-05-11 13:33:37 +00:00
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#define MAX 100
int visited[MAX];
//<2F>ڽӾ<DABD><D3BE><EFBFBD>
typedef struct graph
{
int n; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int e; //<2F><><EFBFBD><EFBFBD>
char vexs[MAX]; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int edges[MAX][MAX]; //<2F>ߵ<EFBFBD><DFB5><EFBFBD><EFBFBD>Ӿ<EFBFBD><D3BE><EFBFBD>
}graph,*MGraph;
void CreateMGraph(MGraph G){
char ch1,ch2;
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d", &G->n);
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d", &G->e);
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD>\n");
for(int i=0; i<G->n; i++){
getchar();
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><><EFBFBD><EFBFBD><EFBFBD>", i+1);
scanf("%c", &(G->vexs[i]));
}
for(int i=0; i<G->n; i++){
for(int j=0; j<G->n; j++){
G->edges[i][j]=0;
}
}
for(int i=0; i<G->e; i++){
getchar();
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> %d <20><><EFBFBD>ߣ<EFBFBD><DFA3><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1,<2C><><EFBFBD><EFBFBD>2<EFBFBD><32><EFBFBD><EFBFBD>", i+1);
scanf("%c,%c",&ch1,&ch2);
for(int j=0; j<G->n; j++){
for(int k=0; k<G->n; k++){
if(ch1==G->vexs[j]&&ch2==G->vexs[k]){
G->edges[j][k]=1;
G->edges[k][j]=1; // <20><><EFBFBD><EFBFBD>
}
}
}
}
}
void DisplayMGraph(MGraph G){
printf("ͼ<EFBFBD><EFBFBD><EFBFBD>ڽӾ<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD>\n");
for(int i=0; i<G->n; i++){
for(int j=0; j<G->n; j++){
printf("%5d", G->edges[i][j]);
}
printf("\n");
}
}
void clear(){
for(int i=0; i<MAX; i++){
visited[i] = 0;
}
}
void DFS(MGraph G, int v){
printf("%c", G->vexs[v]);
visited[v] = 1;
for(int i=0; i<G->n; i++){
if(G->edges[v][i]!=0 && visited[i]==0){
DFS(G, i);
}
}
}
void BFS(MGraph G, int v){
int Qu[MAX], front=0, rear=0, w;
printf("%c", G->vexs[v]);
visited[v]=1;
rear = (rear + 1) % MAX;
Qu[rear] = v;
while(front != rear){
front = (front + 1) % MAX;
w = Qu[front];
for(int i=0; i<G->n; i++){
if(G->edges[w][i]!=0 && visited[i]==0){
printf("%c", G->vexs[i]);
visited[i]=1;
rear = (rear + 1) % MAX;
Qu[rear] = i;
}
}
}
}
int main(){
int choice;
graph G;
for(;;){
printf("\t\t1.<2E><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD>\n");
printf("\t\t2.<2E><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ڽӾ<DABD><D3BE><EFBFBD>\n");
printf("\t\t3.<2E>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><C8B1><EFBFBD>\n");
printf("\t\t4.<2E>ڽӾ<DABD><D3BE><EFBFBD><EFBFBD>Ĺ<EFBFBD><C4B9>ȱ<EFBFBD><C8B1><EFBFBD>\n");
printf("\t\t0.<2E>˳<EFBFBD>\n");
printf("<EFBFBD><EFBFBD>ѡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
scanf("%d",&choice);
if(choice==0) break;
switch (choice) {
case 1:
CreateMGraph(&G);
break;
case 2:
DisplayMGraph(&G);
break;
case 3:
clear();
printf("<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>");
DFS(&G, 0);
printf("\n");
break;
case 4:
clear();
printf("<EFBFBD><EFBFBD><EFBFBD>ȱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>£<EFBFBD>");
BFS(&G, 0);
printf("\n");
break;
default:
break;
}
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>...");
getch();
system("cls");
}
printf("<EFBFBD><EFBFBD>лʹ<EFBFBD>ã<EFBFBD><EFBFBD>ټ<EFBFBD>\n");
return 0;
}