当前位置:网站首页>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 :
边栏推荐
- Intersectionpicker in osgearth
- Unity editor expansion - draw lines
- Message queue for interprocess communication
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- Life cycle of Servlet
- Collection interface
- Unity editor expansion - window, sub window, menu, right-click menu (context menu)
- PHP uses foreach to get a value in a two-dimensional associative array (with instances)
- How does unity fixedupdate call at a fixed frame rate
猜你喜欢
[rust notes] 02 ownership
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Mortgage Calculator
Deeply understand the underlying data structure of MySQL index
Unity learning notes
Drawing maze EasyX library with recursive backtracking method
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
Annotations simplify configuration and loading at startup
二进制转十进制,十进制转二进制
[concurrent programming] explicit lock and AQS
随机推荐
Deeply understand the underlying data structure of MySQL index
【Rust笔记】06-包和模块
注解简化配置与启动时加载
数据库原理期末复习
Mortgage Calculator
Redux - learning notes
Concurrent programming (III) detailed explanation of synchronized keyword
Dom4j traverses and updates XML
Query XML documents with XPath
【Rust 笔记】12-闭包
Graphics_ Games101/202 learning notes
LinkedList set
Dealing with duplicate data in Excel with xlwings
How to deal with the core task delay caused by insufficient data warehouse resources
Annotations simplify configuration and loading at startup
Monotonic stack -42 Connect rainwater
Transmit pictures with Base64 encoding
Markdown directory generation
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
如何应对数仓资源不足导致的核心任务延迟