一.图的遍历

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
int n, m; //行数和列数
const int maxn = 100;
char g[maxn][maxn]; //图
bool vis[maxn][maxn]; //访问标记数组,false表示点没有被访问过
int disx[4] = { 0,0,1,-1 }; //四个
int disy[4] = { 1,-1,0,0 }; //方向
int dx, dy; //起点位置
int counter; //黑瓷砖的数目
bool flag = false; //判断是否有起点
struct node {
int x, y;
}p[maxn];
queue<node> que;
vector<node> vec;
void graph(int n, int m) {
cout << "请输入行数和列数 :";
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cin >> g[i][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
if (g[i][j] == '@') {
dx = i; dy = j;
flag = true;
break;
}
}
if (flag == true) break;
}
if (flag) {
cout << "起始点的坐标是";
printf("( %d , %d )\n\n", dx, dy);
}
else cout << "没有起点" << endl;
}
void bfs() {
vec.clear();
que.empty();
memset(vis, false, sizeof vis);
graph(n, m);
if (!flag) {
counter = 0;
cout << "黑瓷砖的数目是" << counter << endl;
}
else {
counter = 1;
node point;
point.x = dx, point.y = dy;
que.push(point);
vis[point.x][point.y] = true;
vec.push_back(point);
while (!que.empty()) {
node temp = que.front();
node next;
que.pop();
for (int i = 0; i < 4; ++i) {
int x = temp.x + disx[i];
int y = temp.y + disy[i];
next.x = x; next.y = y;
if (g[next.x][next.y] == 'b'&&vis[next.x][next.y] == false) {
vis[next.x][next.y] = true;
que.push(next); ++counter;
vec.push_back(next);
}
}
}
cout << "黑瓷砖的数目是" << counter << endl;
cout << endl;
int number = 0;
cout << "且到达每个黑瓷砖的顺序是 :" << endl;
for (int i = 0; i < vec.size(); ++i) {
if (i != vec.size() - 1) {
++number;
printf("( %d , %d)", vec[i].x, vec[i].y);
cout << " -> ";
if (number % 5 == 0) cout << endl;
}
else printf("( %d , %d)\n", vec[i].x, vec[i].y);
}
}
}
int main() {
bfs();
return 0;
}

糟糕的上机,prim似乎有bug

二.求无向连通图的生成树

#pragma once
#include<algorithm>
#include <fstream>
#include<iostream>
using namespace std;
const int inf = 1000000000;
int p[100];
int maze[100][100];
template<class T>
struct Edge
{
int u, v;
int net;
T key;
Edge() :u(-1), v(-1), key(0) {}
};
template<class T>
class MST {
protected:
Edge<T>*edge;
int maxSize, e;
int head[100];
public:MST(int sz = 100) :maxSize(sz), e(0) {
edge = new Edge<T>[sz];
memset(head, -1, sizeof(head));
}
void addedge(Edge<T>& item);
void establish();
int Kruskal();
int Prim(int n);
};
template<class T>
void MST<T>::addedge(Edge<T>& item)
{
int frm = item.u, to = item.v, w = item.key;
edge[e].u = frm;
edge[e].v = to;
edge[e].key = w;
edge[e].net = head[frm];
head[frm] = e++;
edge[e].u = to;
edge[e].v = frm;
edge[e].key = w;
edge[e].net = head[to];
head[to] = e++;
}
template<class T>
void MST<T>::establish()
{
for (int i = 0; i < 100; i++)
for (int j = 0; j< 100; j++)
if (i == j)maze[i][j] = 0;
else maze[i][j] = inf;
int u, v, w;
Edge<int> temp;
while (cin >> u >> v >> w) {
if (u == 0 && v == 0 && w == 0)break;
temp.u = u, temp.v = v, temp.key = w;
maze[u][v] = maze[v][u] = w;
addedge(temp);
}
return;
}
bool cmp(Edge<int> a, Edge<int> b)
{
return a.key < b.key;
}
int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
}
template<class T>
int MST<T>::Kruskal()
{ ofstream outfile; outfile.open("outdata.txt");
outfile << "Kruskal计算如下:\n";
int ans = 0;
for (int i = 0; i < 50; i++)p[i] = i;
sort(edge, edge + e, cmp);
for (int i = 0; i < e; i++) {
int x = find(edge[i].u);
int y = find(edge[i].v);
if (x != y) { ans += edge[i].key;
// cout << edge[i].u << " " << edge[i].v << endl;
outfile << edge[i].u << " " << edge[i].v<<" 权值: " << edge[i].key << endl; p[x] = y; }
}
outfile << "最小生成树中长度: " << ans << endl;
outfile.close();//关闭文件,保存文件。
return ans;
}
Edge<int> dis[100];
int path[100];
template<class T>
int MST<T>::Prim(int n)
{ bool vis[100];
ofstream outfile;
outfile.open("outdata.txt", ios::binary | ios::app | ios::in | ios::out);
outfile << "Prim计算如下:\n\n\n";
//int dis[100];
for (int i = 0; i < 100; i++)
dis[i].key = inf;
memset(vis, 0, sizeof(vis));
int ans = 0;
dis[3].key = 0;
while (1)
{
int k = 0;
for (int j = 1; j <= n; j++)
{
if (!vis[j] && dis[j].key<dis[k].key) //这一步找未收录顶点中dist值最小的
k = j;
}
if (!k) break;
vis[k] = 1;
ans += dis[k].key;
if (dis[k].u > 0)
outfile <<" "<<dis[k].u << " " << dis[k].v << " 权值" << dis[k].key << "\n" << endl;
for (int j = 1; j <= n; j++)
{
if (dis[j].key>maze[k][j])
{
dis[j].key = maze[k][j];
dis[j].u = k;
dis[j].v = j;
path[j] = k;
}
}
}
outfile << "总长度 " << ans << endl;
outfile.close();//关闭文件,保存文件。 return ans;
}
#include<iostream>
#include"graph.h"
using namespace std;
int main()
{
MST<int> m;
int n;
cin >> n;
m.establish();
m.Kruskal();
m.Prim(n);
getchar();
return 0;
}

Kruskal和prime算法的类实现,图的遍历BFS算法。的更多相关文章

  1. 图的遍历BFS广度优先搜索

    图的遍历BFS广度优先搜索 1. 简介 BFS(Breadth First Search,广度优先搜索,又名宽度优先搜索),与深度优先算法在一个结点"死磕到底"的思维不同,广度优先 ...

  2. 图的遍历BFS

    图的遍历BFS 广度优先遍历 深度优先遍历 可以进行标记 树的广度优先遍历,我们用了辅助的队列 bool visited[MAX_VERTEX_NUM] //访问标记数组 //广度优先遍历 void ...

  3. 图的遍历——BFS

    原创 裸一篇图的BFS遍历,直接来图: 简单介绍一下BFS遍历的过程: 以上图为例子,从0开始遍历,访问0,按大小顺序访问与0相邻的所有顶点,即先访问1,再访问2: 至此顶点0已经没有作用了,因为其本 ...

  4. 模板 图的遍历 bfs+dfs 图的最短路径 Floyed+Dijkstra

    广搜 bfs //bfs #include<iostream> #include<cstdio> using namespace std; ],top=,end=; ][]; ...

  5. 图的遍历——BFS(队列实现)

    #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> ...

  6. 图之强连通--Tarjan算法

    强连通分量 简介 在阅读下列内容之前,请务必了解图论基础部分. 强连通的定义是:有向图 G 强连通是指,G 中任意两个结点连通. 强连通分量(Strongly Connected Components ...

  7. C#与数据结构--图的遍历

    http://www.cnblogs.com/abatei/archive/2008/06/06/1215114.html 8.2 图的存储结构 图的存储结构除了要存储图中各个顶点的本身的信息外,同时 ...

  8. 算法笔记_144:有向图强连通分量的Tarjan算法(Java)

    目录 1 问题描述 2 解决方案 1 问题描述 引用自百度百科: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G是一个强连 ...

  9. tarjan算法,一个关于 图的联通性的神奇算法

    一.算法简介 Tarjan 算法一种由Robert Tarjan提出的求解有向图强连通分量的算法,它能做到线性时间的复杂度. 我们定义: 如果两个顶点可以相互通达,则称两个顶点强连通(strongly ...

随机推荐

  1. Could not parse multipart servlet request; nested exception is org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed.

    org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nes ...

  2. LIBTUX_CAT:466: ERROR: tpopen TPERMERR xa_open returned XAER_INVAL

    tmboot 启动Tuxedo服务失败,从ULOG日志中看到以下错误: 100534.MATHXH!TMS_ORA10G.22600.4076.0: LIBTUX_CAT:466: ERROR: tp ...

  3. 【redis持久化】redis持久化理解

    1.以下内容仅为个人理解和总结,仅供参考,万万不可全盘真信,内容会进行实时改进和修正 2.redis持久化: 参考链接1.https://redis.io/topics/persistence  -- ...

  4. 如何在VMware8虚拟机里安装Xp GHOST系统 解决不能启动Xp系统方法

    好久没有装系统了.之前直接在硬盘中装,装个xp(c盘内).win7(d盘内).centos(虚拟机内)三系统同在一台笔记本电脑上.走了点弯路,这次记录下在虚拟机内装ghost xp. 安装步骤: 1. ...

  5. [转]ThreadLocal使用

    引言 ThreadLocal的官方API解释为: “该类提供了线程局部 (thread-local) 变量.这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线 ...

  6. python3 --- locale命名空间让程序更加安全了

    [简介] 由于python-2.x 并没有locale这个层次的命名空间,所以临时变量有可能会泄漏,进而影响到了包涵它的命名空间 [看一下pyhont-2.x是怎么泄漏临时变量的] python Py ...

  7. 原创科幻短篇《Bug》

    这回不是纯科幻,夹了点玄幻. 以下正文: 大一的时候,李双休谈了个女朋友,俩人学校相距不远,周末约一起看电影.那是李双休第一次自己坐公交,坐反了,绕城一周,电影开始后一个小时才到,就赶上看了个片尾彩蛋 ...

  8. ubuntu设置分辨率

    前言 装过ubuntu的虚拟机人应该都知道,刚刚装完系统时,分辨率小的令人发指,根本就不能愉快的使用,所以必须调整,但是有些分辨率ubuntu里面也没有,这就需要我们自己自定义. 自定义分辨率 1. ...

  9. idea 使用正则表达式 进行匹配替换

    关于正则表达式 可以参考相应的笔记 另外 如果要提取正则表达式中匹配到的内容,使用$1 - $... 按顺序取(第一个表达式 到 第N个表达式匹配到的数据),  这点和linux正则获取的方式是一样的

  10. 不同语言的水仙花性能比较【Test1W】

    看了大佬@鱼丸粗面一碗的文章:<这段代码,c 1秒,java 9秒,c# 14秒,而python...>,基于水仙花数的各种语言1W次性能比较,觉得很有意思.于是开启cv大法,把我有环境的 ...