当前位置:网站首页>Six dimensional space (C language)
Six dimensional space (C language)
2022-07-03 08:50:00 【Cap07】
Global variables.h( Declaration of global variables ):
// Store the declaration of global variables , Pay attention to add extern
extern int Graph[100][100]; // Adjacency matrix
extern int V, W; // Points and edges
extern int visited[100]; // Determine whether to access Global variables.cpp( Setting of global variables ):
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"Global variables.h"
// Set global variables
int Graph[100][100]; // Adjacency matrix
int V, W; // Points and edges
int visited[100]; // Determine whether to access head.h( The header file ):
void BuildGraph();
void SDS();
int BFS(int v);head.cpp( Implementation of functions in header file ):
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
#include"Global variables.h"
void BuildGraph() { // Set up a map
int i, j, n1, n2;
for (i = 0; i < V; i++) { // initialization
for (j = 0; j < V; j++) {
Graph[i][j] = 0;
}
}
for (i = 0; i < W; i++) { // Set up a map
scanf("%d%d", &n1, &n2);
Graph[n1][n2] = 1;
Graph[n2][n1] = 1;
}
}
void SDS() {
int i, j;
float rate;
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++)
visited[j] = 0;
rate = (float)BFS(i) / V;
printf("%d: %.2f%%\n", i + 1, rate * 100);
}
}
int BFS(int v) {
int cnt = 1, level = 0, last = v, i, n, tail;
int que[1000] = {}, rear, front; // Simulation queue
rear = front = 0;
visited[v] = 1;
que[rear] = v;
rear++;
while (rear > front) {
n = que[front];
front++;
for (i = 0; i < V; i++) {
if (visited[i] == 0 && Graph[n + 1][i + 1] == 1) {
visited[i] = 1;
que[rear] = i;
rear++;
cnt++;
tail = i;
}
}
if (n == last) {
level++;
last = tail;
}
if (level == 6) {
break;
}
}
return cnt;
}
ceshi1.cpp( The main function ):
#define _CRT_SECURE_NO_WARNINGS // Six degrees of space
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
#include"Global variables.h"
int main() {
scanf("%d%d", &V, &W);
BuildGraph();
SDS();
return 0;
}test result :

边栏推荐
- How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
- First Servlet
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- [rust note] 10 operator overloading
- Unity editor expansion - the design idea of imgui
- Unity editor expansion - the framework and context of unity imgui
- Log4j2 vulnerability recurrence and analysis
- 数据库原理期末复习
- [concurrent programming] atomic operation CAS
- Osganimation library parsing
猜你喜欢

Alibaba canal actual combat

Monotonic stack -42 Connect rainwater

单调栈-84. 柱状图中最大的矩形

VIM learning notes from introduction to silk skating

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

Unity editor expansion - draw lines

First Servlet

Dom4j traverses and updates XML

第一个Servlet

Annotations simplify configuration and loading at startup
随机推荐
【Rust笔记】02-所有权
How to delete CSDN after sending a wrong blog? How to operate quickly
如何应对数仓资源不足导致的核心任务延迟
Installation of PHP FPM software +openresty cache construction
LinkedList set
Unity editor expansion - draw lines
URL backup 1
Redis cluster series 4
注解简化配置与启动时加载
Convert video to GIF
[concurrent programming] atomic operation CAS
Visual Studio (VS) shortcut keys
[rust note] 10 operator overloading
Unity editor expansion - the framework and context of unity imgui
Monotonic stack -42 Connect rainwater
Unity editor expansion - the design idea of imgui
[concurrent programming] concurrent tool class of thread
Monotonic stack -84 The largest rectangle in the histogram
【Rust 笔记】13-迭代器(上)
Query XML documents with XPath