[回溯]The Settlers of Catan UVA539
来源:互联网 发布:access数据库用途 编辑:程序博客网 时间:2024/05/22 15:44
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
Miguel A. Revilla
1999-01-11
#include<iostream>#include<cstring>using namespace std;int grid[30][30],vis[30][30];int len,maxlen,n;void dfs(int posx,int posy){ len++; int i,j,k; for(i=0;i<n;i++) { if(grid[posy][i]==1&&vis[posy][i]==0&&i!=posx) { vis[posy][i]=1; vis[i][posy]=1; dfs(posy,i); len--; vis[posy][i]=0; vis[i][posy]=0; } } if(len>maxlen) maxlen=len;}int main(){ int m; while(cin>>n>>m&&(n!=0||m!=0)) { memset(grid,0,sizeof(grid)); memset(vis,0,sizeof(vis)); int i,j; for(i=0;i<m;i++) { int x,y; cin>>x>>y; grid[x][y]=grid[y][x]=1; } maxlen=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(grid[i][j]&&vis[i][j]==0) { memset(vis,0,sizeof(vis)); len=0; vis[i][j]=1; vis[j][i]=1; dfs(i,j); } } } cout<<maxlen<<endl; } return 0;}
- [回溯]The Settlers of Catan UVA539
- uva539 - The Settlers of Catan
- UVA539- The Settlers of Catan
- The Settlers of Catan(uva539简单的回溯)
- 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(回溯)
- 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
- IE9在FCKeditor编辑器插入图片时,弹出窗口后没反映,提示调用的对象无效
- win8 ie打不开OA办公系统
- win7如何终止zhudongfangyu.exe进程
- JAVA 3DES-Cipher加密解密
- HOWTO install Oracle 11g on Ubuntu Linux 12.04 (Precise Pangolin) 64bits
- [回溯]The Settlers of Catan UVA539
- hadoop1.2.1eclipse 插件An internal error occurred during: "Connecting to DFS hadoop1.2.1".
- Using OpenCV Java with Eclipse
- The Most Widely Used Java Libraries
- 使用Memcached、Spring AOP构建数据库前端缓存框架
- 从生到死有多远?
- 状态模式lua实现
- 我要进军IT界了
- 【装机吧】电脑上网慢的解决方法