zoukankan      html  css  js  c++  java
  • HDU 4856 Tunnels

    Tunnels

    Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 841    Accepted Submission(s): 247

    Problem Description
       Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In his eyes, the city is a grid. He can’t enter a grid with a barrier. In one minute, he can move into an adjacent grid with no barrier. Bob is full of curiosity and he wants to visit all of the secret tunnels beneath the city. To travel in a tunnel, he has to walk to the entrance of the tunnel and go out from the exit after a fabulous visit. He can choose where he starts and he will travel each of the tunnels once and only once. Now he wants to know, how long it will take him to visit all the tunnels (excluding the time when he is in the tunnels).
     
    Input
       The input contains mutiple testcases. Please process till EOF.    For each testcase, the first line contains two integers N (1 ≤ N ≤ 15), the side length of the square map and M (1 ≤ M ≤ 15), the number of tunnels.    The map of the city is given in the next N lines. Each line contains exactly N characters. Barrier is represented by “#” and empty grid is represented by “.”. Then M lines follow. Each line consists of four integers x1, y1, x2, y2, indicating there is a tunnel with entrence in (x1, y1) and exit in (x2, y2). It’s guaranteed that (x1, y1) and (x2, y2) in the map are both empty grid.
     
    Output
       For each case, output a integer indicating the minimal time Bob will use in total to walk between tunnels.    If it is impossible for Bob to visit all the tunnels, output -1.
     
    Sample Input
    5 4
    ....#
    ...#.
    .....
    .....
    .....
    2 3 1 4
    1 2 3 5
    2 3 3 1
    5 4 2 1
     
    Sample Output
    7
     
     
    /*
    这题是第二场组队赛的一条题,是队友blue码的
    一开始想的是二分图,貌似卡边界。。
    然后发觉点不多 直接 n次BFS求 第i个出口 到 第j个入口的距离 ~~ 然后开始状压DP,~~
     
    */
     
     
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <utility>
    #include <queue>
    #define inside(x) ((x)>0 && (x)<=n)
    using namespace std;
    typedef long long ll;
    typedef double DB;
    typedef pair<int,int> pii;
    const int INF = 1e9;
    const DB eps = 1e-6;
    const int N = 20;
    const int dx[]={1,-1,0,0};
    const int dy[]={0,0,1,-1};
    // http://106.185.28.251/vjudge/contest/view.action?cid=53977#overview
    
    char g[N][N];
    struct _edge{
        int sx,sy,ex,ey;
    }e[N];
    int n,m;
    int w[N][N],dis[N][N];
    bool vis[N][N];
    
    int dp[1<<17][N];
    bool dpvis[1<<17][N];
    
    void build_dp()
    {
        int maxn=1<<m;
        for(int i=0;i<maxn;++i)
            for(int j=0;j<m;++j)
                dp[i][j]=INF;
        queue<pii> q;
        memset(dpvis,0,sizeof(dpvis));
        for(int i=0;i<m;++i)
        {
            dp[1<<i][i]=0;
            q.push(make_pair(1<<i,i));
        }
        int s,u,ns;
        while(!q.empty())
        {
            s = q.front().first;
            u = q.front().second;
            q.pop();
            if(dpvis[s][u]) continue;
            dpvis[s][u]=1;
            for(int v=0;v<m;++v)
            {
                if(w[u][v]==-1 || (s&(1<<v)))   continue;
                ns = s|(1<<v);
                if(dp[s][u]+w[u][v] < dp[ns][v])
                {
                    dp[ns][v] = dp[s][u]+w[u][v];
                    q.push(make_pair(ns,v));
                }
            }
        }
    //    for(int i=0;i<maxn;++i)
    //    {
    //        printf("%x: ",i);
    //        for(int j=0;j<m;++j)    printf("%d ",dp[i][j]);
    //        puts("");
    //    }
    }
    
    void bfs(int x,int y)
    {
        memset(vis,0,sizeof(vis));
        dis[x][y]=0;
        vis[x][y]=1;
        queue<pii> q;
        q.push(make_pair(x,y));
        while(!q.empty())
        {
            x=q.front().first;
            y=q.front().second;
            q.pop();
            for(int i=0;i<4;++i)
            {
                int nx=x+dx[i], ny=y+dy[i];
                if(inside(nx) && inside(ny) && !vis[nx][ny] && g[nx][ny]!='#')
                {
                    vis[nx][ny]=1;
                    dis[nx][ny]=dis[x][y]+1;
                    q.push(make_pair(nx,ny));
                }
            }
        }
    }
    
    void run()
    {
        for(int i=1;i<=n;++i)
        {
            scanf("%s",g[i]);
            for(int j=n;j>0;--j)
                g[i][j]=g[i][j-1];
        }
        for(int i=0;i<m;++i)
            scanf("%d%d%d%d",&e[i].sx,&e[i].sy,&e[i].ex,&e[i].ey);
        if(m==1)
        {
            puts("0");
            return;
        }
        for(int i=0;i<m;++i)
        {
            bfs(e[i].ex,e[i].ey);
            for(int j=0;j<m;++j)
            {
                if(!vis[e[j].sx][e[j].sy])  w[i][j]=-1;
                else w[i][j]=dis[e[j].sx][e[j].sy];
            }
            w[i][i]=-1;
        }
    //    for(int i=0;i<m;++i)
    //    {
    //        for(int j=0;j<m;++j)
    //            cout<<w[i][j]<<' ';
    //        cout<<endl;
    //    }
        build_dp();
        int ans=INF;
        int ts=0;
        for(int i=0;i<m;++i)
            ts|=(1<<i);
        for(int i=0;i<m;++i)
            ans=min(ans,dp[ts][i]);
        if(ans==INF)    puts("-1");
        else printf("%d
    ",ans);
    }
    
    int main()
    {
        #ifdef LOCAL
        freopen("in","r",stdin);
        #endif
        while(scanf("%d%d",&n,&m)!=EOF)
            run();
        return 0;
    }
     
     
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    ESP32环境搭建(arduino)
    uPyCraftIDE的介绍以及使用
    ESP32项目汇总
    MicroPython固件烧写
    Server 架构
    base家族在线解密工具
    第1年2月24日 iOS相机旋转问题 为什么UIimage.ciimage值为空
    第1年2月23日 du -sh * | sort -n 统计当前文件夹(目录)大小,并按文件大小排序
    第1年2月14日 mac 运行java
    第1年2月10日 检查iOS app证书过期时间信息
  • 原文地址:https://www.cnblogs.com/hlmark/p/3918443.html
Copyright © 2011-2022 走看看