uva 539 The Settlers of Catan

来源:互联网 发布:美好的诗句 知乎 编辑:程序博客网 时间:2024/05/17 07:58

原题:
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.
这里写图片描述
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 (2 ≤ n ≤ 25) and the
number of edges m (1 ≤ m ≤ 25). 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 2
0 1
1 2
15 16
0 2
1 2
2 3
3 4
3 5
4 6
5 7
6 8
7 8
7 9
8 10
9 11
10 12
11 12
10 13
12 14
0 0
Sample Output
2
12

中文:
给你一个无向图,现在让你找最长的路径,其中节点可以重复走,但是路径只能走一次。

#include <bits/stdc++.h>using namespace std;int m,n;int vis[30][30];int used[30];vector<int> G[26];int ans;int point[26][26];void dfs(int x,int num){    ans=max(ans,num);    for(int i=0;i<G[x].size();i++)    {        if(!vis[x][G[x][i]])        {            vis[x][G[x][i]]=1;            vis[G[x][i]][x]=1;            dfs(G[x][i],num+1);            vis[x][G[x][i]]=0;            vis[G[x][i]][x]=0;        }    }}int main(){    ios::sync_with_stdio(false);    while(cin>>n>>m,m+n)    {        memset(vis,false,sizeof(vis));        memset(used,false,sizeof(used));        for(int i=0;i<26;i++)            G[i].clear();        for(int i=0;i<m;i++)        {            int a,b;            cin>>a>>b;            used[a]=1;            used[b]=1;            G[a].push_back(b);            G[b].push_back(a);        }        ans=0;        for(int i=0;i<n;i++)        {            if(used[i])            {                memset(vis,0,sizeof(vis));                dfs(i,0);            }        }        cout<<ans<<endl;    }    return 0;}

解答:
直接暴力搜索每个点最长能到的距离即可0s过,很简单的回溯题目。
可以优化的小部分,记录每次搜索的最长路径的结尾节点,如果搜索了一次从s点开始的最长路径是到e点结束,那么从e点开始搜索同样也会是s点结束,所以记录节点e,下次就不用搜索了。

0 0
原创粉丝点击