The Settlers of Catan(uva539简单的回溯)
来源:互联网 发布:设计专业显示器 知乎 编辑:程序博客网 时间:2024/05/16 03:43
题意:求无向图的最长链,点可以重复,边不可以重复
思路:暴力解决
每个点都有可能变成起始点,所以用每个点都暴力找到每个点的所有能走的路,用visit[i][j]保存走过的边,如果边没走过,就走,否则就不走
邻接矩阵简历表,发现有边可以走,就走过去从该点开始往下遍历
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespace std;int map[26][26] = {0};bool visit[26][26] = {0};int Max = -999;void dfs(int u,int n,int cnt){ if(cnt > Max) Max = cnt; for(int i = 0; i < n;i++) { if(map[u][i] && !visit[u][i]) { visit[u][i] = visit[i][u] = true; dfs(i,n,cnt+1); visit[u][i] = visit[i][u] = false; } }}int main(){ int n,m; while(scanf("%d%d",&n,&m) != EOF) { int x,y; if(n == 0 && m == 0) break; memset(map,false,sizeof(map)); for(int i = 0; i < m; i++) { scanf("%d%d",&x,&y); map[x][y] = map[y][x] = true; } Max = -999; for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(map[i][j]) { visit[i][j] = visit[j][i] = true; dfs(j,n,1); visit[i][j] = visit[j][i] = false; } } } printf("%d\n",Max); } return 0;}
- The Settlers of Catan(uva539简单的回溯)
- [回溯]The Settlers of Catan UVA539
- uva539 - The Settlers of Catan
- UVA539- The Settlers of Catan
- UVa 539 - The Settlers of Catan, 简单回溯
- The Settlers of Catan uva dfs+回溯
- 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 ( DFS 回溯 )
- 539--The Settlers of Catan
- 539The Settlers of Catan
- 539 - The Settlers of Catan
- UVaOJ539 - The Settlers of Catan
- UVA657- The Settlers of Catan
- 十年前与十年后的我们,流着泪看完的经典语录!
- 【百日三千】之JAVA细节——短路
- mac 下 node-qrcode的二维码库
- Reactor 和 Proactor 模式
- django 按时间范围查询数据库
- The Settlers of Catan(uva539简单的回溯)
- AspNet_webformaspx页面
- Sublime Text Emmet插件高级使用不完全指南
- Kmeans(K均值)与Kmeans++和KNN(K近邻)算法比较
- Android 读取<meta-data>元素的数据
- Windows Azure 社区新闻综述(#77 版)
- Libevent(1)— 简介、编译、配置
- android 计算TextView文本总行数
- 学习中遇到的小技巧 二 (陆续更新……)