zoukankan      html  css  js  c++  java
  • 习题7-1 uva 208(剪枝)

    题意:按最小字典序输出a到b 的所有路径。

    思路:先处理出个点到目标点b的情况(是否能到达),搜索即可。

    最开始我只判了a能否到b,然后给我的是WA,然后看了半天感觉思路没什么问题,然后把所有点都处理出来,AC

    实在是看不懂- -,好无语。


    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <queue>
    #include <algorithm>
    typedef long long ll;
    using namespace std;
    int tmap[200][200];
    const int inf = 0x3f3f3f3f;
    int ans[200];
    int can[200];
    int n,a,b;
    int tmax;
    int vis[200];
    int all;
    
    void dfs(int cur,int num)
    {
        if(cur == n)
        {
            printf("1");
            for(int i = 1; i < num; i++)
                printf(" %d",ans[i]);
            printf("
    ");
            all++;
            return ;
        }
        for(int i = 1; i <= tmax; i++)
        {
            if(!vis[i] && tmap[cur][i] && can[i])
            {
                vis[i] = 1;
                ans[num] = i;
                dfs(i,num+1);
                vis[i] = 0;
            }
        }
        return ;
    }
    
    void get_can(int cur)      //是否能到达终点
    {
        can[cur] = 1;
        for(int i =1 ; i <= tmax; i++)
            if(tmap[cur][i] && !can[i])
                get_can(i);
    }
    
    int main()
    {
        int cas = 1;
        while(scanf("%d",&n)!=EOF)
        {
            memset(vis,0,sizeof(vis));
            memset(can,0,sizeof(can));
            memset(tmap,0,sizeof(tmap));
            while(scanf("%d %d",&a,&b))
            {
                if(a == 0 && b == 0)
                    break;
                tmap[a][b] = tmap[b][a] = 1;
                if(a > tmax)
                    tmax = a;
                if(b > tmax)
                    tmax = b;
            }
            get_can(n);
            printf("CASE %d:
    ",cas++);
            if(!can[n])
            {
                printf("There are 0 routes from the firestation to streetcorner %d.
    ",n);
                continue;
            }
    
            vis[1] = 1;
            all = 0;
            dfs(1,1);
            printf("There are %d routes from the firestation to streetcorner %d.
    ",all,n);
        }
        return 0;
    }
    

      

  • 相关阅读:
    Python文件相关的操作
    Python运算符
    字符串方法
    Python列表的增删改查和元祖
    压测
    jmeter相关使用
    charles的使用
    接口测试
    编程珠玑之关键字(1)--《c语言深度剖析》整理(转)
    循环单链表操作(转)
  • 原文地址:https://www.cnblogs.com/Przz/p/5409703.html
Copyright © 2011-2022 走看看