UVA539- The Settlers of Catan

题意:给n个点,给你点与点之间的关系,求出最大的路线

思路:DFS,用深搜,不断更新最大值。。。。

#include<stdio.h> 
#include<string.h>
#define N 30
 
int n, m, max;
int map[N][N];

void dfs(int x, int cnt) {
	if (cnt > max)		
		max = cnt;
	for(int i = 0; i < n; i++) {
		if (map[x][i]) {	
			map[x][i] = map[i][x] = 0;
			dfs(i, cnt + 1);	
			map[x][i] = map[i][x] = 1;
		}
	}
}

int main() { 
	while (scanf("%d %d", &n, &m) != EOF) {
		int a, b;
		if (n == 0 && m == 0)	
			break;

		max = 0;
		memset(map, 0, sizeof(map));

		for(int i = 0; i < m; i++) {
			scanf("%d %d", &a, &b);	
			map[a][b] = 1;
			map[b][a] = 1;
		}			
		
		for(int i = 0; i < n; i++) {
			dfs(i, 0);
		}	
		printf("%d\n", max);	
	}
	return 0;
}




你可能感兴趣的:(UVA539- The Settlers of Catan)