安阳旅游地图规划(未完成)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MaxNum 10
#define INFINITY 9999
#define MaxStringLength 100

typedef struct GNode {
    int NumVertex;
    int NumEdge;
    int Vertexs[MaxNum][MaxNum];
    char VertexData[MaxNum][MaxStringLength];
} Graph;

typedef struct view{
	char viewname[100];
	char viewinformation[1000];
}view;
void enter(view v[],char*name,char*information,int i){
		strcpy(v[i].viewname,name);
		strcpy(v[i].viewinformation,information);
}
void visit(Graph* graph, int vertex) {
    printf("%s ", graph->VertexData[vertex]);
}

Graph* Make_Graph(int NumVertex, int NumEdge) {
    Graph* graph = (Graph*)malloc(sizeof(Graph));
    graph->NumVertex = NumVertex;
    graph->NumEdge = 0;
    for (int i = 0; i < NumVertex; i++) {
        for (int j = 0; j < NumVertex; j++) {
            graph->Vertexs[i][j] = 0;
        }
    }
    return graph;
}

void Add_Edge(Graph* graph, int vertex1, int vertex2, int weight) {
    if (graph == NULL || graph->NumVertex == 0) {
        printf("error");
        return;
    }
    graph->Vertexs[vertex1][vertex2] = weight;
    graph->Vertexs[vertex2][vertex1] = weight;
}

void Add_Vertex_Data(Graph* graph, int vertex, char* name) {
    if (graph == NULL || vertex > graph->NumVertex - 1 || vertex < 0) {
        printf("error");
        return;
    }
    strcpy(graph->VertexData[vertex], name);
}

void DFS(Graph* graph, int vertex, void (*visit)(Graph*, int), bool visited[]) {
    visit(graph, vertex);
    visited[vertex] = true;
    for (int i = 0; i < graph->NumVertex; i++) {
        if (graph->Vertexs[vertex][i] != 0 && !visited[i] && graph->Vertexs[vertex][i] != INFINITY) {
            DFS(graph, i, visit, visited);
        }
    }
}

void dijkstra(Graph* graph, int start, bool visited[]) {
    int T[MaxNum][2];
    for (int i = 0; i < graph->NumVertex; i++) {
        T[i][0] = INFINITY;
        T[i][1] = -1;
        visited[i] = false;
    }
    T[start][0] = 0;
    T[start][1] = -1;
    for (int i = 0; i < graph->NumVertex - 1; i++) {
        int mindist = INFINITY;
        int minindex = -1;
        for (int j = 0; j < graph->NumVertex; j++) {
            if (!visited[j] && T[j][0] < mindist) {
                mindist = T[j][0];
                minindex = j;
            }
        }
        visited[minindex] = true;
        for (int k = 0; k < graph->NumVertex; k++) {
            if (!visited[k] && graph->Vertexs[minindex][k] != 0 && T[minindex][0] + graph->Vertexs[minindex][k] < T[k][0]) {
                T[k][0] = T[minindex][0] + graph->Vertexs[minindex][k];
                T[k][1] = minindex;
            }
        }
    }
    int end;
    printf("请输入目的地编号:");
    scanf("%d", &end);
    printf("距离为:%d\n", T[end][0]);
    printf("路线为:%d ", end);
    int temp = T[end][1];
    while (temp != -1) {
        printf("<- %d ", temp);
        temp = T[temp][1];
    }
}

int main() {
	FILE *fp;
	fp=fopen("data.txt","r");
    bool visited[MaxNum];
    for (int i = 0; i < MaxNum; i++) {
        visited[i] = false;
    }
    int NumVertex = 10;
    int NumEdge = 19;
    Graph* graph = Make_Graph(NumVertex, NumEdge);
    for (int i = 0; i < NumVertex; i++) {
        char* name = (char*)malloc(sizeof(char) * 100);
        printf("请输入%d号景点的名称:\n", i);
        fscanf(fp,"%s", name);
        Add_Vertex_Data(graph, i, name);
    }
    for (int i = 0; i < NumEdge; i++) {
        int v1, v2, weight;
        printf("请输入两个景点的编号和距离:\n");
        fscanf(fp,"%d %d %d", &v1, &v2, &weight);
        printf("从%d号景点到%d号景点的距离%d:\n", v1, v2, weight);
        Add_Edge(graph, v1, v2, weight);
        printf("\n");
    };
    for(int i=0;i<NumVertex;i++){
    	char*name=(char*)malloc(sizeof(char)*100);
    	char*information=(char*)malloc(sizeof(char)*1000);
    	view v[10];
    	fscanf(fp,"%s",name);
    	fscanf(fp,"%d",information);
		enter(v,name,information,i); 
	}
    for (int i = 0; i < graph->NumVertex; i++) {
        printf("%s\n", graph->VertexData[i]);
    }
    
    DFS(graph, 0, visit, visited);
    printf("\n\n");
    int start;
    system("cls");
    printf("                                                                               \n");
    printf("                                                                               \n");                                 
    printf("                  |-------------0殷墟------1袁林-------2中国文字博物馆         \n");
    printf("                  |              |                  |             |            \n");
    printf("6红旗渠------------              |                  |             |            \n");
    printf("    |             |              |---------------4羑里城-----------            \n");
    printf("    |             |              |                  |             |            \n");
    printf("    |             |              |                  |             |            \n");
    printf("7太行大峡谷-------|-------------3长春观----------5岳飞庙-------   |            \n");
    printf("                                                              |   |            \n");
    printf("                                                               8二帝陵         \n");
    printf("                                                                  |            \n");
    printf("                                                                  |            \n");
    printf("                                                               9明福寺         \n");
    printf("                                                                               \n");
    printf("                                                                               \n");
    printf("                                                                               \n");
    printf("                                                                               \n");
    int choice;
    do{
    	scanf("%d",&choice);
    	if(choice==1){
    		
		}
		if(choice==2){
			
		}
    	if(choice==3){
    		
		}
	}while(choice!=0);
    printf("请输入出发地地点编号:");
    scanf("%d", &start);
    dijkstra(graph, start, visited);
    fclose(fp);
    return 0;
}

殷墟

袁林

文字博物馆

长春观

羑里城

岳飞庙

红旗渠

太行大峡谷

二帝陵

明福寺

0 1 5

0 2 8

0 3 30

0 4 20

0 6 70

1 2 5

1 4 20

2 4 20

2 8 75

3 4 40

3 5 35

3 6 60

3 7 30

4 5 5

4 8 60

5 8 55

5 9 60

6 7 30

8 9 40

a

b

c

d

e

f

g

h

i

j

0 1 5

0 2 8

0 3 30

0 4 20

0 6 70

1 2 5

2 4 21

2 8 75

3 4 40

3 5 35

3 6 60

3 7 30

4 8 60

5 8 55

5 9 60

6 7 30

8 9 40

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#define MaxNum 10
#define INFINITY 9999
#define MaxStringLength 100
 
typedef struct GNode {
    int NumVertex;
    int NumEdge;
    int Vertexs[MaxNum][MaxNum];
    char VertexData[MaxNum][MaxStringLength];
} Graph;
 
typedef struct view{
	char viewname[100];
	char viewinformation[1000];
}view;
void enter(view v[],char*name,char*information,int i){
		strcpy(v[i].viewname,name);
		strcpy(v[i].viewinformation,information);
}
void print_view(view v[]){
	for(int i=0;i<10;i++){
		printf("景点名称:%s\n",v[i].viewname);
		printf("景点介绍:%s\n",v[i].viewinformation);
		printf("\n");
	}
}
void visit(Graph* graph, int vertex) {
    printf("%s ", graph->VertexData[vertex]);
}
 
Graph* Make_Graph(int NumVertex, int NumEdge) {
    Graph* graph = (Graph*)malloc(sizeof(Graph));
    graph->NumVertex = NumVertex;
    graph->NumEdge = 0;
    for (int i = 0; i < NumVertex; i++) {
        for (int j = 0; j < NumVertex; j++) {
            graph->Vertexs[i][j] = 0;
        }
    }
    return graph;
}
 
void Add_Edge(Graph* graph, int vertex1, int vertex2, int weight) {
    if (graph == NULL || graph->NumVertex == 0) {
        printf("error");
        return;
    }
    graph->Vertexs[vertex1][vertex2] = weight;
    graph->Vertexs[vertex2][vertex1] = weight;
}
 
void Add_Vertex_Data(Graph* graph, int vertex, char* name) {
    if (graph == NULL || vertex > graph->NumVertex - 1 || vertex < 0) {
        printf("error");
        return;
    }
    strcpy(graph->VertexData[vertex], name);
}
 
void DFS(Graph* graph, int vertex, void (*visit)(Graph*, int), bool visited[]) {
    visit(graph, vertex);
    visited[vertex] = true;
    for (int i = 0; i < graph->NumVertex; i++) {
        if (graph->Vertexs[vertex][i] != 0 && !visited[i] && graph->Vertexs[vertex][i] != INFINITY) {
            DFS(graph, i, visit, visited);
        }
    }
}
 
void dijkstra(Graph* graph, int start, bool visited[]) {
    int T[MaxNum][2];
    for (int i = 0; i < graph->NumVertex; i++) {
        T[i][0] = INFINITY;
        T[i][1] = -1;
        visited[i] = false;
    }
    T[start][0] = 0;
    T[start][1] = -1;
    for (int i = 0; i < graph->NumVertex - 1; i++) {
        int mindist = INFINITY;
        int minindex = -1;
        for (int j = 0; j < graph->NumVertex; j++) {
            if (!visited[j] && T[j][0] < mindist) {
                mindist = T[j][0];
                minindex = j;
            }
        }
        visited[minindex] = true;
        for (int k = 0; k < graph->NumVertex; k++) {
            if (!visited[k] && graph->Vertexs[minindex][k] != 0 && T[minindex][0] + graph->Vertexs[minindex][k] < T[k][0]) {
                T[k][0] = T[minindex][0] + graph->Vertexs[minindex][k];
                T[k][1] = minindex;
            }
        }
    }
    int end;
    printf("请输入目的地编号:");
    scanf("%d", &end);
    printf("最短距离为:%dkm\n", T[end][0]);
    printf("最短路线为:%s ", graph->VertexData[end]);
    int temp = T[end][1];
    while (temp != -1) {
        printf("<- %s ", graph->VertexData[temp]);
        temp = T[temp][1];
    }
}
 
int main() {
	FILE *fp;
	fp=fopen("data.txt","r");
    bool visited[MaxNum];
    for (int i = 0; i < MaxNum; i++) {
        visited[i] = false;
    }
    int NumVertex = 10;
    int NumEdge = 19;
    Graph* graph = Make_Graph(NumVertex, NumEdge);
    for (int i = 0; i < NumVertex; i++) {
        char* name = (char*)malloc(sizeof(char) * 100);
        printf("请输入%d号景点的名称:\n", i);
        fscanf(fp,"%s", name);
        Add_Vertex_Data(graph, i, name);
    }
    for (int i = 0; i < NumEdge; i++) {
        int v1, v2, weight;
        printf("请输入两个景点的编号和距离:\n");
        fscanf(fp,"%d %d %d", &v1, &v2, &weight);
        printf("从%d号景点到%d号景点的距离%d:\n", v1, v2, weight);
        Add_Edge(graph, v1, v2, weight);
        printf("\n\n");
    };
    view v[10];
    for(int i=0;i<10;i++){
    	char*name=(char*)malloc(sizeof(char)*100);
    	char*information=(char*)malloc(sizeof(char)*1000);
    	printf("请输入景点名称:"); 
    	fscanf(fp,"%s",name);
    	printf("请输入景点信息:");
    	fscanf(fp,"%s",information);
		enter(v,name,information,i); 
	}
    printf("\n\n");
    int choice;
	int start;
    do{
    	
        printf("                                                                               \n");
        printf("                                                                               \n");                                 
        printf("                  |-------------0殷墟------1袁林-------2中国文字博物馆         \n");
        printf("                  |              |                  |             |            \n");
        printf("6红旗渠------------              |                  |             |            \n");
        printf("    |             |              |---------------4羑里城-----------            \n");
        printf("    |             |              |                  |             |            \n");
        printf("    |             |              |                  |             |            \n");
        printf("7太行大峡谷-------|-------------3长春观----------5岳飞庙-------   |            \n");
        printf("                                                    |         |   |            \n");
        printf("                                                    |          8二帝陵         \n");
        printf("                                                    |             |            \n");
        printf("                                                    |             |            \n");
        printf("                                                    |----------9明福寺         \n");
        printf("                                                                               \n");
        printf("                                                                               \n");
        printf("*******************************河南安阳旅游地图********************************\n");
        printf("                                                                               \n");
    	printf("              【1】景点名称及相关信息\n");
    	printf("              【2】深度遍历景点\n");
		printf("              【3】获取最短路径\n");
		printf("               请输入你的选项:\n");
		scanf("%d",&choice);
		printf("\n\n\n");
		if(choice==1){
			print_view(v);
			printf("\n");
		} 
		if(choice==2){
			int num;
			printf("请输入作为遍历起点的景点编号:");
			scanf("%d",&num);
			DFS(graph, num, visit, visited);
			printf("\n");
		}
		if(choice==3){
			printf("请输入出发地地点编号:\n");
            scanf("%d", &start);
            dijkstra(graph, start, visited);
            printf("\n");
		}
	}while(choice!=0);
	fclose(fp);
    return 0;
}
相关推荐
pianmian11 小时前
python数据结构基础(7)
数据结构·算法
好奇龙猫3 小时前
【学习AI-相关路程-mnist手写数字分类-win-硬件:windows-自我学习AI-实验步骤-全连接神经网络(BPnetwork)-操作流程(3) 】
人工智能·算法
sp_fyf_20244 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-11-01
人工智能·深度学习·神经网络·算法·机器学习·语言模型·数据挖掘
香菜大丸4 小时前
链表的归并排序
数据结构·算法·链表
jrrz08284 小时前
LeetCode 热题100(七)【链表】(1)
数据结构·c++·算法·leetcode·链表
oliveira-time4 小时前
golang学习2
算法
南宫生5 小时前
贪心算法习题其四【力扣】【算法学习day.21】
学习·算法·leetcode·链表·贪心算法
懒惰才能让科技进步6 小时前
从零学习大模型(十二)-----基于梯度的重要性剪枝(Gradient-based Pruning)
人工智能·深度学习·学习·算法·chatgpt·transformer·剪枝
Ni-Guvara6 小时前
函数对象笔记
c++·算法
泉崎7 小时前
11.7比赛总结
数据结构·算法