HDU 4856 tunnels

Source: Internet
Author: User
Tags exit in
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 traveling in Xi'an. he finds eclipsecret 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 l Ength 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 input5 4 .... #... #................ 2 3 1 41 2 3 52 3 15 4 2 1 sample output7/* this is a question for the second team competition, at the beginning, I thought of a binary chart as a teammate's blue code. It seems like a boundary .. Then we found that the distance from the I-th exit to the J-th entry is calculated by BFS n times ~~ Then start to press 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#overviewchar 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\n",ans);}int main(){    #ifdef LOCAL    freopen("in","r",stdin);    #endif    while(scanf("%d%d",&n,&m)!=EOF)        run();    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.