SZU:D89 The Settlers of Catan

本文介绍了一个寻找无向图中最长道路的问题,通过深度优先搜索(DFS)算法来解决这一挑战。文章提供了两种不同的C语言实现方案,展示了如何有效遍历图并确保不会重复使用相同的边。

 

Judge Info

  • Memory Limit: 65536KB
  • Case Time Limit: 3000MS
  • Time Limit: 3000MS
  • Judger: Number Only Judger

Description

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 (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

 

建模:

  已知n 个点,m 条边,输入两个点来构成一条边,构成一副无向图,两点之间可以有多条边,从任意一个点搜索,要求每条边只能经过一次,找到最长的路径。

 

思路:

  1.输入点的个数n, 边的个数m,并且初始化存整个无向图的二维数组w
  2.存边进入二维数组W
  3.对所有点进行dfs搜索,如果路径最长则更新max
  4.dfs找出所有存在的边,并且记录路径的长度

 

我的代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3  
 4 #define MAXN 30
 5  
 6 int n,m;
 7 int G[MAXN][MAXN];
 8 int vis[MAXN][MAXN];
 9 int maxNum;
10 int k;
11  
12 void dfs(int u, int num){
13   int flag;
14     for(int v=0; v<n; ++v){
15         if(G[u][v] && !vis[u][v]){
16  
17             if(G[u][v] != 1){
18                 flag = 1;
19                 vis[u][v] = 0;
20                 vis[u][v] = 0;
21                 G[u][v] --; 
22                 G[v][u] --;
23             }
24             else {
25                 vis[u][v] = 1;
26                 vis[v][u] = 1;
27          
28             }
29          
30             dfs(v, num+1);
31             if(flag == 1){
32                 G[u][v]++;
33                 G[v][u]++;
34             }
35             flag = 0;
36             vis[u][v] = vis[v][u] = 0;
37         }
38     }
39  
40     if(num > maxNum) maxNum = num;
41 }
42  
43  
44 int main(){
45  
46     int a,b;
47  
48     while(~scanf("%d %d", &n, &m)){
49  
50         if(!n && !m) break;
51         memset(G, 0, sizeof(G));
52         for(int i=0; i<m; ++i){
53             scanf("%d %d", &a, &b);
54             ++G[a][b];  
55         ++G[b][a];
56         }
57  
58         maxNum = -9999;
59  
60         for(int i=0; i<n; ++i){
61             memset(vis, 0, sizeof(vis));
62             dfs(i, 0);
63         }
64         printf("%d\n",maxNum);
65     } 
66     return 0;
67 }
View Code

 

 

朋友的代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 26
 4  
 5 int w[N][N],max,n;  //w[N][N] 存入整个图,w[i][j]=x 代表i到j有x条边
 6  
 7 void dfs(int,int);
 8  
 9 int main()
10 {
11     int m,sum;
12     int a,b,i;
13     while(1)
14     {        
15         scanf("%d%d",&n,&m);
16         if(!m && !n)
17             break;
18  
19         memset(w,0,sizeof(w)); //初始化
20  
21         for(i=0;i<m;i++)
22         {
23             scanf("%d%d",&a,&b);
24             w[a][b]++;  //无向图
25             w[b][a]++;
26         }
27  
28         max=0; 
29         for(i=0;i<n;i++)
30         {
31             sum=0;   //把每个点作为出发点,对每一个点进行搜索
32             dfs(i,sum);
33         }
34         printf("%d\n",max);
35     }
36     return 0;
37 }
38  
39 void dfs(int a,int sum)  // a 为当前探索到哪一个节点,sum为当前探索到的路径长度
40 {
41     int i;
42     if(sum>max)  //更新当前搜索到的最长路径
43         max=sum;
44     for(i=0;i<n;i++)
45     {
46         if(w[a][i])
47         {
48             w[a][i]--;  w[i][a]--; //用去一条边
49             dfs(i,sum+1);  // 进行下一层递归
50             w[a][i]++;  w[i][a]++; //回溯到上一层
51         }
52     }
53     return;
54 }
View Code

 

AI-PPT 一键生成 PPT:用户输入主题关键词,AI-PPT 可快速生成完整 PPT,涵盖标题、正文、段落结构等,还支持对话式生成,用户可在 AI 交互窗口边查看边修改。 文档导入转 PPT:支持导入 Word、Excel、PDF 等多种格式文档,自动解析文档结构,将其转换为结构清晰、排版规范的 PPT,有保持原文和智能优化两种模式。 AI-PPT 对话 实时问答:用户上传 PPT 或 PPTX 文件后,可针对演示内容进行提问,AI 实时提供解答,帮助用户快速理解内容。 多角度内容分析:对 PPT 内容进行多角度分析,提供全面视野,帮助用户更好地把握内容结构和重点。 多语言对话支持:支持多语言对话,打破语言障碍,方便不同语言背景的用户使用。 AI - 绘图 文生图:用户输入文字描述,即可生成符合语义的不同风格图像,如油画、水彩、中国画等,支持中英文双语输入。 图生图:用户上传图片并输入描述,AI - 绘图能够根据参考图和描述生成新的风格化图像,适用于需要特定风格或元素的创作需求。 图像编辑:提供如 AI 超清、AI 扩图、AI 无痕消除等功能,用户可以上传图片进行细节修改和优化,提升图片质量。 AI - 文稿 文案生成:能够根据用户需求生成多种类型的文章,如市场营销文案、技术文档、内部沟通内容等,提升文案质量和创作效率。 文章润色:对已有文章进行改善和优化,包括语言表达、逻辑连贯性、内容流畅度等方面,使文章更符合用户期望和风格。 文章续写:AI 技术理解文本语境,为用户提供新的想法、补充资料或更深层次的见解,帮助用户丰富文档内容。 AI - 医生 智能健康咨询:包括症状自查,用户输入不适症状,AI 结合病史等信息提供疾病可能性分析与初步建议;用药指导,支持查询药品适应症、禁忌症等,并预警潜在冲突;中医辨证,提供体质辨识与调理建议。 医学报告解读:用户上传体检报告
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值