递归搜索之朋友配对数
来源:互联网 发布:java字符串截取前几位 编辑:程序博客网 时间:2024/06/06 09:35
/* *这是一道关于暴力搜索的题目,题目要求输入一组朋友关系,然后输出有多少种 *两两朋友组队在一起的方法 *设计:我们使用递归函数来解决,在这个问题中,将整个问题分为n/2个操作,每个 *操作等同于对两名学生的组队,此问题变为:给定还没有组队的学生名单时,计算出 *两名朋友之间组成一对的组合个数,对名单中两名为朋友关系的学生组队后,还是和 *原来一样需要处理同样的问题,所以用递归函数来求解 *Time:2016/4/10*/#include<iostream>#define MAX_F 100using namespace std;int m,n,res=0;//the num of childrenbool areFreiends[MAX_F][MAX_F];//whether two children are friendbool leftC[MAX_F];int countpair(bool leftchildren[MAX_F]){//find the left children<not pair.>int firstchild = -1;for (int i = 0; i < n; ++i){if (!leftchildren[i]){firstchild = i;break;}}//checkif (firstchild == -1) return 1;//all finded.this is one way to do this jobint ret = 0;//else,choose a child with this childfor (int i = firstchild + 1; i < n; i++){if (!leftchildren[i] && areFreiends[firstchild][i]){leftchildren[i] = leftchildren[firstchild] = true; //findedret += countpair(leftchildren);leftchildren[i] = leftchildren[firstchild] = false;//for next test}}return ret;}int main(){struct { int l, r; }p;for (;;){for (int i = 0; i < n; i++)for (int j = 0; j < n; j++)areFreiends[i][j] = false;cin >> n>>m;//the num of children and relationsfor (int i = 0; i < m; i++){cin >> p.l >> p.r;areFreiends[p.l][p.r] = true;areFreiends[p.r][p.l] = true;}memset(leftC, false,MAX_F);cout << "The result is:" << countpair(leftC) << endl;} exit(EXIT_SUCCESS);}
0 0
- 递归搜索之朋友配对数
- 动态规划之 最优二叉搜索数 递归实现
- 递归之划分数
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- Android 蓝牙开发之搜索、配对、连接、通信大全
- 二叉搜索树之递归
- 【jzoj4824】【配对游戏】【搜索】
- 组合数之递归算法
- 蓝牙讲解上--搜索、配对
- 二叉搜索树之非递归
- 数据结构之搜索二叉树递归&非递归
- 记忆化搜索之二:集合上的动态规划,最优配对问题
- 简单递归之蛇形填数
- 递归递推之计算组合数
- Java各种对象理解(转)
- 输出1-100之间的奇数
- hihocoder 1259 (数位DP)
- C++如何实现单例模式
- [BZOJ2186][Sdoi2008]沙拉公主的困惑
- 递归搜索之朋友配对数
- leetcode 121. Best Time to Buy and Sell Stock-股市交易
- 在Myeclipse 中导入java web项目出现JRE System Library(unbound)和Server Library(unbound)解决方法
- 尽量以const、enum、inline替换#define
- RS232电平 RS485电平 RS422电平
- Java反射学习总结
- CSS3_2D转换
- 自定义UISlider
- 组合数学 随记