利用二维矩阵求spanning tree
只做了9个节点的,无权值,使用了n-1个=8个循环,非常麻烦。一级一级判断是否连接,连接及记录所在节点,以后不再使用,确保无回路。
验证后无回路,但只试过几种情况。
代码如下:
#include<iostream>
using namespace std; int w[][];
int e[][];
void Shuru()
{ for(int i=;i<;i++)
{
for(int j=;j<;j++)
{cout<<"请输入节点"<<i+<<"与节点"<<j+<<"是否存在连通。yes=1;no=0"<<endl;
cin>>w[i][j];
}
}
} void Panduan()
{ for(int p=;p<;p++)
{for(int q=;q<;q++)
{e[p][q]=;}//赋值
}
int d[];
for(int k=;k<;k++)
{d[k]=;}
d[]=; //判断部分
for(int a1=;a1<;a1++)
{
if(w[][a1]==&&d[a1]==)
{e[][a1]=; e[a1][]=;
d[a1]=a1;
for(int a2=;a2<;a2++)
{
if(w[a1][a2]==&&d[a2]==)
{e[a1][a2]=;
e[a2][a1]=;
d[a2]=a2;
for(int a3=;a3<;a3++)
{
if(w[a2][a3]==&&d[a3]==)
{ e[a2][a3]=;
e[a3][a2]=;
d[a3]=a3;
for(int a4=;a4<;a4++)
{
if(w[a3][a4]==&&d[a4]==)
{ e[a3][a4]=;
e[a4][a3]=;
d[a4]=a4;
for(int a5=;a5<;a5++)
{
if(w[a4][a5]==&&d[a5]==)
{ e[a4][a5]=;
e[a5][a4]=;
d[a5]=a5;
for(int a6=;a6<;a6++)
{
if(w[a5][a6]==&&d[a6]==)
{ e[a5][a6]=;
e[a6][a5]=;
d[a6]=a6;
for(int a7=;a7<;a7++)
{
if(w[a6][a7]==&&d[a7]==)
{e[a6][a7]=;
e[a7][a6]=;
d[a7]=a7;
for(int a8=;a8<;a8++)
{
if(w[a7][a8]==&&d[a8]==)
{e[a7][a8]=;
e[a8][a7]=;
d[a8]=a8;
}
}
} }
} }
} }
}
}
}
}
}
}
}
}
}
void main()
{
Shuru();
Panduan();
for(int z=;z<;z++)
{
{for(int x=;x<;x++)
cout<<e[z][x]<<"\t";}
cout<<endl;
} }
示例:

结果:

利用二维矩阵求spanning tree的更多相关文章
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)
问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时 ...
- 【leetcode】74. 搜索二维矩阵
题目链接:传送门 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 ...
- Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵
11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...
- lintcode:搜索二维矩阵II
题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...
- Python小代码_5_二维矩阵转置
使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...
随机推荐
- 0c-41-ARC使用特点及注意事项
1.ARC特点总结 1)不允许调用release,retain,retainCount 2)允许重写dealloc,但是不允许调用[super dealloc] 3)@property的参数: str ...
- oc-13-多文件
Dog.h #import <Foundation/Foundation.h> @interface Dog : NSObject { @public NSString *_name; i ...
- 自增锁ID复用问题
mysql> select * from pp; +----+------+ | id | name | +----+------+ | xx | | xx | | xx | | xx | | ...
- Golang学习 - fmt 包
------------------------------------------------------------ // Print 将参数列表 a 中的各个参数转换为字符串并写入到标准输出中. ...
- LeetCode33 Search in Rotated Sorted Array
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- C# 自定义重绘TabControl
using System.Drawing; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Runti ...
- iOS 应用有用户评论功能 因为潜在色情信息被退回解决方案
应用的每一次退回都是一次很好的学习机会 这次是说我的应用没有对色情的内容进行监管或屏蔽的管理机制 这里我主要是 评论页面 违法 如下 以下提供几种解决方案: 1.按照苹果给的建议 我们添加协议进去 ...
- Android(java)学习笔记68:同步代码块 和 同步方法 的应用
1. 同步代码块 和 同步方法 代码示例: (1)目标类,如下: package cn.himi.text; public class SellTicket implements Runnable { ...
- JavaScript 中的算术运算
JavaScript中算术运算在溢出(overflow).下溢(underflow)或被零整除时不会报错,当数字运算结果超过了JavaScript所能表示的数字上限(溢出),结果为一个特殊的无穷大(i ...
- Unhandled Error in Silverlight Application “Syncfusion.Silverlight.Olap.Gauge.OlapGauge”的类型初始值设定项引发异常
Silverlight 在运行时,如果出现如下错误: 检查生成的xap文件,解压出来,看是否里面包含该DLL:Syncfusion.Silverlight.Olap.Gauge.OlapGauge