uva - 539 - The Settlers of Catand
来源:互联网 发布:apache 2.2.21 漏洞 编辑:程序博客网 时间:2024/06/05 09:25
类型:
暴力求解中的回溯法
题目要求:
输入n、m,n代表点的个数,m代表这n之间边的个数,找出这个图中的最长路径长度。
解题思路:
不用用数据结构的算法,用回溯就行,主要注意这是一个无向图,从哪个点开始都行。用数组类似邻接矩阵表示图。细节注意:
1、注意是无向图,所以输入的时候要赋值两个,并且要DFS每个结点找最大的。也就是main中的那个循环。
2、DFS函数参数的传递要明白。
AC代码:
#define Local#include <iostream>#include <iomanip>#include <string>#include <cstring>#include <cstdio>#include <queue>#include <stack>#include <algorithm>#include <cmath>using namespace std;int G[30][30], vis[30][30], _max, n, m, num;//num是结点数void Creat_Graph (){int i = 0, a = 0, b = 0;for (i = 0; i < m; i++)//这是m,一边输入边,一边记录点。{cin >> a >> b;G[a][b] = 1;G[b][a] = 1;}}void DFS(int cur, int num){int i = 0;if (num > _max)_max = num;for (i = 0; i < n; i++){if ((G[cur][i] || G[i][cur]) && !vis[cur][i] && !vis[i][cur]){vis[cur][i] = 1, vis[i][cur] = 1;DFS(i, num+1);vis[cur][i] = 0, vis[i][cur] = 0;}}}int main(){#ifdef Localfreopen("a.in", "r", stdin);freopen("a.out", "w", stdout);#endifint i = 0, j = 0;while (cin >> n >> m && n+m){_max = 0;memset(G, 0, sizeof(G));memset(vis, 0, sizeof(vis));Creat_Graph();for (i = 0; i < n; i++)DFS(i, 0);cout << _max << endl;}}
0 0
- uva - 539 - The Settlers of Catand
- UVa 539 The Settlers of Catan
- Uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- UVA 539 - The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 The Settlers of Catan
- uva 539 - The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVA 539 The Settlers of Catan
- uva 539 The Settlers of Catan
- UVa 539 - The Settlers of Catan
- uva 539 - The Settlers of Catan
- uva 539 The Settlers of Catan
- android 功能模块之通讯模块五
- android功能模块之通讯模块六
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- 实时开发框架Meteor API解读系列<三>Publish and Subscribe-(1)
- 个人助手
- uva - 539 - The Settlers of Catand
- android功能模块之通讯模块七
- Arduino之Led1
- Eclipse debug调试
- android小项目之新闻客户端一
- LeetCode OJ:Set Matrix Zeroes
- oracle存储过程的创建
- python yield用法
- TCP套接字网络通信