14:07:40 2019-09-02 

学习

 

PTA第17题 六度空间理论

可以在结构体中 加上一个属性来表示层数 

我是利用一个整型变量 Level和 一个数组LevelSize来记录每层数量多少 

需要注意要处理 最远距离小于6的情况

  1 #define _CRT_SECURE_NO_WARNINGS  
  2 #include<stdio.h>
  3 #include<malloc.h>
  4 
  5 int NumberOfConnect[1001];
  6 typedef struct ENode* Edge;
  7 struct ENode
  8 {
  9     int V1, V2;
 10 };
 11 
 12 typedef struct Graph* MGraph;
 13 struct Graph
 14 {
 15     int Nv;
 16     int Ne;
 17     int G[1001][1001];
 18 };
 19 
 20 MGraph CreateMGraph(int MaxVertex)
 21 {
 22     MGraph Graph;
 23     Graph = (MGraph)malloc(sizeof(struct Graph));
 24     Graph->Nv = MaxVertex;
 25     Graph->Ne = 0;
 26     for (int i = 1; i <=Graph->Nv; i++)
 27         for (int j = 1; j <=Graph->Nv; j++)
 28             Graph->G[i][j] = 0;
 29     return Graph;
 30 }
 31 void Insert(MGraph Graph,Edge E)
 32 {
 33     Graph->G[E->V1][E->V2] = 1;
 34     Graph->G[E->V2][E->V1] = 1;
 35 }
 36 MGraph BuildGraph()
 37 {
 38     MGraph Graph;
 39     Edge E;
 40     int Nv;
 41     scanf("%d", &Nv);
 42     Graph = CreateMGraph(Nv);
 43     scanf("%d\n", &(Graph->Ne));
 44     for (int i = 0; i < Graph->Ne; i++)
 45     {
 46         E = (Edge)malloc(sizeof(struct ENode));
 47         scanf("%d %d\n", &(E->V1), &(E->V2));
 48         Insert(Graph, E);
 49     }
 50     return Graph;
 51 }
 52 
 53 int IsEdge(MGraph Graph,int V, int W)
 54 {
 55     return (Graph->G[V][W] == 1) ? 1 : 0;
 56 }
 57 
 58 //利用广度优先搜索
 59 #define Size 1001
 60 int Queue[Size];
 61 int Front = 1;
 62 int Rear = 0;
 63 int size = 0;
 64 int IsEmpty()
 65 {
 66     return (size == 0) ? 1 : 0;
 67 }
 68 int Succ(int Value)
 69 {
 70     if (Value < Size)
 71         return Value;
 72     else
 73         return 0;
 74 }
 75 void EnQueue(int V)
 76 {
 77     Rear = Succ(Rear + 1);
 78     Queue[Rear] = V;
 79     size++;
 80 }
 81 int DeQueue()
 82 {
 83     int V = Queue[Front];
 84     Front = Succ(Front + 1);
 85     size--;
 86     return V;
 87 }
 88 void InitializeQueue()
 89 {
 90     for (int i = 0; i < Size; i++)
 91         Queue[i] = 0;
 92     Front = 1;
 93     Rear = 0;
 94     size = 0;
 95 }
 96 int visited[1001];
 97 void InitializeVisited()
 98 {
 99     for (int i = 0; i < 1001; i++)
100         visited[i] = 0;
101 }
102 int BFS(MGraph Graph, int V)
103 {
104     InitializeVisited();
105     InitializeQueue();
106     EnQueue(V);
107     visited[V] = 1;
108     int Level = 0;  //从第0层开始
109     int LevelSize[1001] = {0};
110     LevelSize[Level] = 1;    //第0层自己算一个元素
111     NumberOfConnect[V]++;    //令该顶点可以访问的个数加1
112     while (!IsEmpty())
113     {
114         int W = DeQueue();
115         LevelSize[Level]--;
116         for (int i = 1; i <= Graph->Nv; i++)
117             if (!visited[i]&&IsEdge(Graph, W, i))
118             {
119                 LevelSize[Level+1]++;
120                 EnQueue(i);
121                 visited[i] = 1;
122                 NumberOfConnect[V]++;
123             }
124         if (LevelSize[Level] == 0)
125             Level++;
126         if (Level==6)
127             return NumberOfConnect[V];
128     }
129     //当 达不到6 层时
130     return NumberOfConnect[V];
131 }
132 void OutPut(MGraph Graph,int V,float num)
133 {
134     printf("%d: %.2f%%\n", V, (num / Graph->Nv) * 100);
135 }
136 void SDS(MGraph Graph)
137 {
138     for (int i = 1; i <= Graph->Nv; i++)
139     {
140         int num = BFS(Graph,i);
141         OutPut(Graph,i,num);
142     }
143 }
144 int main()
145 {
146     MGraph G;
147     G = BuildGraph();
148     SDS(G);
149     return 0;
150 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-08
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案