利用二维矩阵求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 ...
随机推荐
- 利用URLScan工具过滤URL中的特殊字符(仅针对IIS6)
客户公司搞安全检查,扫描出来我们之前做的系统有一个高危漏洞:IIS tilde directory enumeration,也就是利用“~”字符猜解暴露短文件/文件夹名,比如,采用这种方式构造URL: ...
- GifCam
转载:http://blog.bahraniapps.com/?page_id=21 下载 http://pan.baidu.com/s/1c0vdHIw GifCam Note:GifCam c ...
- tachyon 集群安装
tachyon的集群安装和单机安装差别不大 http://www.cnblogs.com/admln/p/tachyon-local-install.html 不同的地方 1.修改slaves时填入所 ...
- ApplePay高调入华,教你在app里上线ApplePay
ApplePay在中国上线后,就有许多线上app前后脚加入了对其的接入支持,个人比较喜欢的ENJOY也抢在首批接入了ApplePay应用内支付.本文将分享作者的接入经验. ApplePay是苹果公 ...
- Eclipse中配置Tomcat碰到Server Tomcat v6.0 Server at localhost failed to start问题
控制台打印异常如下: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/juli/logg ...
- Win Mingw-64获取
首先得了解下三种异常不同实现: SJLJ: slower but available for every architecture. SEH: fastest but limited to 64-bi ...
- Java基础知识强化106:Java中 int 的各进制之间的转换
1.二.八.十.十六进制之间的转换 下面是示例代码,我们直接通过JDK工具库中的方法实现的,如下: public static Integer valueOf(String s, int radix ...
- Android进阶笔记18:选用合适的IPC方式
1. 相信大家都知道Android进程间通信方式很多,比如AIDL.Messenger等等,接下来我就总结一下这些IPC方式优缺点. 2. IPC方式的优缺点和适用场景 3. 附加:使用Intent实 ...
- 【Linux/Ubuntu学习4】ubuntu 下面安装 vim 的问题
ubuntu 下面安装 vim 的问题 1.输入vim时,显示: 程序“vim”已包含在以下软件包中: * vim * vim-gnome * vim-tiny * vim-gtk * vim-nox ...
- 【开源项目5】测滑菜单MenuDrawer的使用以及解析
在安卓中左右侧滑菜单的使用用的比ios多得多,可能是谷歌带的头吧,几乎所有的谷歌应用都有侧滑菜单.谷歌没有开放这个源码,在一个成熟的开源代码出现之前,大家都是各自为战,偶尔能看到一个勉强实现了的.Me ...