只做了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的更多相关文章

  1. lintcode :搜索二维矩阵

    题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...

  2. IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)

    问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10   分析: 2*2子数组的最大和.遍历求和,时 ...

  3. 【leetcode】74. 搜索二维矩阵

    题目链接:传送门 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例  ...

  4. Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. lintcode:搜索二维矩阵II

    题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...

  9. Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...

随机推荐

  1. QT程序在windows下部署发布

    转载:http://www.cnblogs.com/Fan_Fan/archive/2010/05/29/1746860.html QT程序在windows下部署发布 以下包括了部分网上收集的,以及q ...

  2. Game: Map Design Considerations 游戏地图设计指南

    依据前文伏击战场景手稿, 用Tile Studio "草草"制作出该场景的地图: 生成的C源码: #ifndef _open_war_1Gfx_c #define _open_wa ...

  3. linux 下 奇怪的 动态库 依赖问题

    转:http://fanwei51880.blog.163.com/blog/static/3240674020111145285375/ 总结如下:1)当你在编译生成静态库的时候, 只需要相应的依赖 ...

  4. iOS-自定义导航栏后侧滑返回功能失效

    iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 在一开始写项目的时候,就要做好一个准备,导航栏是自定义还是使用系统的,后期有什么改动,有什么比较特殊的需求.当然这些在更改需求 ...

  5. K - 4 Values whose Sum is 0(中途相遇法)

    K - 4 Values whose Sum is 0 Crawling in process... Crawling failed Time Limit:9000MS     Memory Limi ...

  6. Zend Studio 上 安装使用Aptana插件(html,css,js代码提示功能) .

    最近装了zend studio 9.0 用了段时间发现写html,css,js代码没提示,要开dreamwaver(对js代码提示也不好).就网上搜索了下,发现了Aptana插件,装上用了下,感觉不错 ...

  7. VMware系统运维(十三)部署虚拟化桌面 Horizon View Agent 5.2安装

    1.先打开安装程序如下所示 2.点击"下一步" 3.不接受,你能装吗?点击"下一步" 4.选择安装位置,点击"下一步" 5.开启3389和3 ...

  8. HDU 1257 最少拦截系统 (DP || 贪心)

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  9. FreeBSD 安裝 Tomcat JAVA JDK1.6 筆記

    首先是安裝軟體 cd /usr/ports/java/jdk16/ make 在這一步,需要你手動到sun.com上下載幾個安裝包,按提示下載好後加入到 /usr/ports/distfiles/,再 ...

  10. hdu 2489 最小生成树状态压缩枚举

    思路: 直接状态压缩暴力枚举 #include<iostream> #include<algorithm> #include<cstdio> #include< ...