UVa 539:the Settlers of Catan, simple backtracking

Source: Internet
Author: User

Topic Link:

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

Topic Type: Backtracking method

Original title:

Within Settlers of Catan, the 1995 German Game of the year, players attempt to dominate a island by building roads, Settl Ements and cities across its uncharted wilderness.

You are employed by a software the just has decided to develop a computer version of this game, and you are chose N to implement one of the game ' s special rules:

When the game ends, the player who built the longest road gains two extra victory.

The problem is this players usually build complex road networks and not just one linear path. Therefore, determining the longest road is not trivial (although human, players usually to the it immediately).

Compared to the original game, we'll 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 this doesn ' t use an edge twice. Nodes May is 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 2
0 1
1 2
0
2
1 2 2 3 3 4 3 5 4 6 5 7 6 8 7 8 7 9
8
9 (
0 0 in)

Sample output:

2
12

The main effect of the topic:

Enter a non-direction graph, and then ask to output the longest path of the graph. Points can be repeated walk, but the side cannot repeat go.

Ideas and Summary:

A very simple backtracking recursive question, enumerate all points directly, then start the recursive search from this point, record the number of paths, and finally update the maintenance of a maximum value.

Code:

#include <iostream> #include <cstdio> #include <cstring> #define MAXN using 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; }

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.