UVA 539 - The Settlers of Catan(深搜 回溯)
来源:互联网 发布:30岁程序员找不到工作 编辑:程序博客网 时间:2024/06/04 19:03
The Settlers of Catan
Within Settlers of Catan, the 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities across its uncharted wilderness.
You are employed by a software company that just has decided to develop a computer version of this game, and you are chosen to implement one of the game's special rules:
When the game ends, the player who built the longest road gains two extra victory points.
The problem here is that the players usually build complex road networks and not just one linear path. Therefore, determining the longest road is not trivial (although human players usually see it immediately).
Compared to the original game, we will solve a simplified problem here: You are given a set of nodes (cities) and a set of edges (road segments) of length 1 connecting the nodes. The longest road is defined as the longest path within the network that doesn't use an edge twice. Nodes may be visited more than once, though.
Example: The following network contains a road of length 12.
o o -- o o \ / \ / o -- o o -- o / \ / \o o -- o o -- o \ / o -- o
Input
The input file will contain one or more test cases.The first line of each test case contains two integers: the number of nodes n ( ) and the number of edges m ( ). The next m lines describe the m edges. Each edge is given by the numbers of the two nodes connected by it. Nodes are numbered from 0 to n-1. Edges are undirected. Nodes have degrees of three or less. The network is not neccessarily connected.
Input will be terminated by two values of 0 for n and m.
Output
For each test case, print the length of the longest road on a single line.Sample Input
3 20 11 215 160 21 22 33 43 54 65 76 87 87 98 109 1110 1211 1210 1312 140 0
Sample Output
212
================================
分别从每个点开始深搜,得到最大长度
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,m,ans,vis[33][33];void dfs(int point,int sum){ for(int i=0;i<n;i++) { if(vis[point][i]) { vis[point][i]=vis[i][point]=0; dfs(i,sum+1); vis[point][i]=vis[i][point]=1; } } if(sum>ans) ans=sum; return;}int main(){ int x,y; while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; ans=-1; memset(vis,0,sizeof(vis)); for(int i=0;i<m;i++) { scanf("%d%d",&x,&y); vis[x][y]=vis[y][x]=1; } for(int i=0;i<n;i++) { dfs(i,0); } printf("%d\n",ans); } return 0;}
- UVA 539 - The Settlers of Catan(深搜 回溯)
- UVA 539 The Settlers of Catan(回溯法)
- UVA - 539 The Settlers of Catan (回溯水题)
- uva 539 The Settlers of Catan(回溯)
- The Settlers of Catan uva dfs+回溯
- UVa 539 - The Settlers of Catan, 简单回溯
- UVa 539 - The Settlers of Catan 递归回溯
- UVa - 539 - The Settlers of Catan ( DFS 回溯 )
- 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
- 斐波那契数列
- Window.open()方法参数详解
- Uninstall Kea Coloring Book with WindowsUninstaller.Org Removal Tips
- dm device mapper
- NoSuchMethodError: antlr.collections.AST.getLine()I
- UVA 539 - The Settlers of Catan(深搜 回溯)
- 淘宝记账王3.1【为淘宝卖家设计的记账软件】
- ural 1086
- Java 随机数工具类(可指定字母或数字生成)
- Session的生命周期
- 如何写mysql存储引擎
- Visual SVN + 花生壳 配置公网访问内网SVN服务器
- javascript编程容易出现的错误
- Toad for oracle 快捷键