zoukankan      html  css  js  c++  java
  • POJ_2258 The settlers of Catan (DFS)

      /*这题应该属于水题吧,基本不用什么大的处理,看懂题意就行。直接n次dfs,找出最大的length*/

    //My Code: 16MS

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int N = 30;

    int map[N][N];
    bool vis[N][N];
    int n;
    int ans;

    void dfs(int now, int t){
    int f, i;
    for(f = 1, i = 0; i < n; i++){
    if(vis[now][i] == false && map[now][i]){
    f = 0;
    }
    }
    //cout << f << endl;
    if(f) {ans = ans > t ? ans : t; return;}
    for(i = 0; i < n; i++){
    if(map[now][i] && !vis[now][i]){
    vis[now][i] = vis[i][now] = true;
    dfs(i, t+1);
    vis[now][i] = vis[i][now] = false;
    }
    }
    }

    int main(){
    //freopen("data.in", "r", stdin);

    int m, max, x, y, i;
    while(~scanf("%d%d", &n, &m)){
    if(n == 0 && m == 0) break;
    memset(map, 0, sizeof(map));
    while(m--){
    scanf("%d%d", &x, &y);
    map[x][y] = map[y][x] = 1;
    }
    for(max = -1, i = 0; i < n; i++){
    memset(vis, false, sizeof(vis));
    ans = 0; dfs(i, 0);
    if(max < ans)
    max = ans;
    }
    printf("%d\n", max);
    }
    }



  • 相关阅读:
    JQuery
    CSS
    函数装饰器
    函数
    模块和运算符
    前端编程基础
    MySQL优化指南-大表优化思路
    Linux命令find讲解
    LeetCode每日题解(0324)
    Kmeans算法的经典优化——mini-batch和Kmeans++
  • 原文地址:https://www.cnblogs.com/vongang/p/2220305.html
Copyright © 2011-2022 走看看