利用二维矩阵求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 ...
随机推荐
- C语言中的函数
C语言中的函数 目录 概述——对函数的理解 C语言中函数的定义和声明 函数允许的参数类型 函数允许的返回类型 递归 概述 由于有些代码段在编写程序的时候经常会用到,此时我们为了减少代码文件的长度和增加 ...
- php内核探索
http://www.nowamagic.net/librarys/veda/special/PHP%E5%86%85%E6%A0%B8%E6%8E%A2%E7%B4%A2 关注PHP 源代码 Zen ...
- 性能监视器(SSAS)
使用性能监视器,您可以通过性能计数器监视 Microsoft SQL Server Analysis Services (SSAS) 实例的性能. 性能监视器是用于跟踪资源使用情况的 Microsof ...
- 疑难杂症:NoSuchMethodError: com.opensymphony.xwork2.util.finder.UrlSet.includeClassesUrl(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;)
严重: Exception starting filter struts2java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finde ...
- PHP抓取网络数据的6种常见方法
http://www.nowamagic.net/academy/detail/12220245 http://www.nowamagic.net/academy/detail/12220245
- 通过继承nsoperation的方法--处理复杂任务
#import <Foundation/Foundation.h> @class TTOperation; @protocol TTOperationDelegate <NSObje ...
- 6. Android框架和工具之 JSON解析
Android进阶笔记17:3种JSON解析工具(org.json.fastjson.gson)
- android studio还不错
今天体验了哈 Android Studio,还不错同Elipse类似
- Uva 10305 - Ordering Tasks 拓扑排序基础水题 队列和dfs实现
今天刚学的拓扑排序,大概搞懂后发现这题是赤裸裸的水题. 于是按自己想法敲了一遍,用queue做的,也就是Kahn算法,复杂度o(V+E),调完交上去,WA了... 于是检查了一遍又交了一发,还是WA. ...
- Myeclipse集成Jboss 6.1控制台不输出日志信息
在使用myeclipse+jboss 6.1开发的时候发现jboss能够正常启动但是myeclipse的控制台却没有任何的信息输出,这使得我没有办法开发,在查找了大部分的资料发现很多说要改什么jbos ...