六度空间
仅仅是菜鸟做个记录而已,代码没有参考性,大神请略过。
//数据结构学习记录
PTA链接原题链接
`
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10001 //最大顶点数
#define INFINITY 65535 /* ∞设为双字节无符号整数的最大值65535*/
#define ERROR 0
int G[MAXN][MAXN], Nv, Ne;
int Visited[MAXN];/* 标记V已访问 */
typedef int Vertex;
struct Node {
int Data;
struct Node *Next;
};
struct QNode {
struct Node *rear;
struct Node *front;
};
typedef struct QNode *Queue;
int IsEmpty(Queue Q);
Queue CreateQueue();
int DeleteQ(Queue PtrQ);
void InsertQ(int item, Queue PtrQ);
void BuildGraph ();
void SDS ();
bool IsEdge(Vertex V, Vertex W);
int BFS (Vertex V);
void Build ();
int main()
{
//Build();
BuildGraph();
for (int i = 0; i < MAXN; i++) {
Visited[i] = false;
}
SDS();
return 0;
}
void Build () //Build()做测试用
{ //用邻接矩阵表示图
int i, j, v1, v2;
//初始化图
//scanf("%d %d", &Nv, &Ne);
Nv = 10; Ne = 9;
G[0][0] = INFINITY;
for (i=1; i<=Nv; i++) {
for (j=1; j<=Nv; j++) {
G[i][j] = INFINITY;
}
}
//插入边
G[1][2] = 1; G[2][1] = 1;
G[2][3] = 1; G[3][2] = 1;
G[3][4] = 1; G[4][3] = 1;
G[4][5] = 1; G[5][4] = 1;
G[5][6] = 1; G[6][5] = 1;
G[6][7] = 1; G[7][6] = 1;
G[7][8] = 1; G[8][7] = 1;
G[8][9] = 1; G[9][8] = 1;
G[9][10] = 1; G[10][9] = 1;
/* for (i=1; i<=Ne; i++) {
scanf("%d %d", &v1, &v2);
G[v1][v2] = 1;
G[v2][v1] = 1;
} */
//图建立完成
}
void BuildGraph ()
{ //用邻接矩阵表示图
int i, j, v1, v2;
//初始化图
scanf("%d %d", &Nv, &Ne);
G[0][0] = INFINITY;
for (i=1; i<=Nv; i++) {
for (j=1; j<=Nv; j++) {
G[i][j] = INFINITY;
}
}
//插入边
for (i=1; i<=Ne; i++) {
scanf("%d %d", &v1, &v2);
G[v1][v2] = 1;
G[v2][v1] = 1;
}
//图建立完成
}
void SDS ()
{
int V, count;
double out;
for (V=1; V<=Nv; V++) {
for (int i = 0; i < MAXN; i++) {
Visited[i] = false;
}
count = BFS(V);
double f=100.00*count/Nv;
printf("%d: %.02f%%\n", V, f);
//printf("%d: 有%d个符合理论\n", V, count);
out = count/Nv;
//printf("%d: %.2lf\n", V, out);
//Output(count/Nv);
}
/* V=4;
count = BFS(V);
printf("%d有%d个符合理论\n", V, count);
*/
}
/* IsEdge(Graph, V, W)检查<V, W>是否图Graph中的一条边,即W是否V的邻接点。 */
bool IsEdge(Vertex V, Vertex W)
{
return G[V][W]<INFINITY ? true : false;
}
int BFS (Vertex V)
{
int count, level, last, tail;
Vertex W;
Visited[V] = true; count = 1;
level = 0; // 当前顶点所在层数
last = V; // 当前访问的这一层的最后一个节点是谁
Queue Q;
Q = CreateQueue();
InsertQ(V, Q);
//tail = 2;
while (!IsEmpty(Q)) {
V = DeleteQ(Q); /* 弹出V */
for (W=1; W<=Nv; W++) {
if (!Visited[W] && IsEdge(V, W)) {
Visited[W] = true;
InsertQ(W, Q); /* W入队列 */
// printf("%d入队\n", W);
count++;
tail = W;
}
}
if (V == last) {
level++; last = tail;
//printf("V = %d level = %d\n", V, level);
}
if (level == 6) {
//printf("跳出BFS\n");
break;
}
}
return count;
}
int IsEmpty(Queue Q) {
return(Q->front == NULL);
};
Queue CreateQueue() {
Queue PtrQ;
PtrQ = (Queue)malloc(sizeof(struct QNode));
struct Node *rear;
struct Node *front;
rear = (Node*)malloc(sizeof(struct Node));
rear = NULL;
front = (Node*)malloc(sizeof(struct Node));
front = NULL;
PtrQ->front = front;
PtrQ->rear = rear;
return PtrQ;
};
int DeleteQ(Queue PtrQ) {
struct Node *FrontCell;
int FrontElem;
if (IsEmpty(PtrQ)) {
printf("队列空");
return ERROR;
}
FrontCell = PtrQ->front;
if (PtrQ->front == PtrQ->rear)
PtrQ->front = PtrQ->rear = NULL;
else {
PtrQ->front = PtrQ->front->Next;
}
FrontElem = FrontCell->Data;
free(FrontCell);
return FrontElem;
}
void InsertQ(int item, Queue PtrQ) {
struct Node *FrontCell;
FrontCell = (Node*)malloc(sizeof(struct Node));
FrontCell->Data = item;
FrontCell->Next = NULL;
if (IsEmpty(PtrQ)) {
PtrQ->front = FrontCell;
PtrQ->rear = FrontCell;
}
else {
PtrQ->rear->Next = FrontCell;
PtrQ->rear = FrontCell;
}
};