Ultraviolet A 539-the Settlers of Catan, simple backtracking

Source: Internet
Author: User
539-
Settlers of Catan
3761 54.91% 1773 93.06%

Question link:

Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 108 & page = show_problem & problem = 480

Question type: backtracking

Original question:

WithinSettlers of Catan, The 1995 German game of the year, players attempt to dominate an island by building roads, settlements and cities within SS 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 vicw.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

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

Question:

Enter an undirected graph and then output the longest path of the graph. Point can be repeated, but the side cannot be repeated.


Ideas and summary:

A simple backtracking recursive question is to enumerate all vertices directly, then start recursive search from this point, record the number of paths, and finally update and maintain a maximum value.


Code:

#include<iostream>#include<cstdio>#include<cstring>#define MAXN 30using namespace std;int n,m, G[MAXN][MAXN], vis[MAXN][MAXN], maxNum;void dfs(int u, int num){    for(int v=0; v<n; ++v){        if(G[u][v] && !vis[u][v]){            vis[u][v] = vis[v][u] = 1;            dfs(v, num+1);            vis[u][v] = vis[v][u] = 0;        }    }    if(num > maxNum) maxNum = num;}int main(){#ifdef LOCAL    freopen("input.txt","r",stdin);#endif    int a,b;    while(~scanf("%d %d", &n, &m)){        if(!n && !m) break;        memset(G, 0, sizeof(G));        for(int i=0; i<m; ++i){            scanf("%d %d", &a, &b);            ++G[a][b];  ++G[b][a];        }                maxNum = -2147483645;        for(int i=0; i<n; ++i){            memset(vis, 0, sizeof(vis));            dfs(i, 0);        }        printf("%d\n",maxNum);    }     return 0;}


-- The meaning of life is to give it meaning.

 


Original Http://blog.csdn.net/shuangde800 ,
D_double(For details, refer)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.