一.图的遍历
#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 )
", 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)
", 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计算如下:
";
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计算如下:
";
//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 << "
" << 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;
}