hdu 4856 Tunnels

Tunnels

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


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 x 1, y 1, x 2, y 2, indicating there is a tunnel with entrence in (x 1, y 1) and exit in (x 2, y 2). It’s guaranteed that (x 1, y 1) and (x 2, y 2) 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
 



先求出通道之间的最短路,然后dp[i][j]表示以j为起点,状态为i的最优值。

/*
 *=====================
 *File Name:a.cpp
 *Author: qqspeed
 *Date: 2014年 07月 08日 星期二 08:58:32 CST
 *=====================
 */
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

typedef long long ll;
#define rep(i,s,t) for(int i=s;i<t;i++)
#define red(i,s,t) for(int i=s-1;i>=t;i--)
#define ree(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define max(a,b) (a<b?b:a)
#define min(a,b) (a<b?a:b)
#define lowbit(x) (x&(-x))
#define SQR(a) ((a)*(a))

inline int input(){
    int ret=0;bool isN=0;char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-') isN=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        ret=ret*10+c-'0';
        c=getchar();
    }
    return isN?-ret:ret;
}

inline void output(int x){    
    if(x<0){    
        putchar('-');x=-x;    
    }    
    int len=0,data[11];    
    while(x){    
        data[len++]=x%10;x/=10;    
    }    
    if(!len)    data[len++]=0;    
    while(len--)   
        putchar(data[len]+48);    
    putchar('\n');  
}  

const int MAXN=16;
const int dx[]={1,-1,0,0};
const int dy[]={0,0,1,-1};
int dp[1<<MAXN][MAXN];

struct T{
    int xs,ys,xe,ye;
}t[MAXN];

int n,m;
char s[MAXN+2][MAXN+2];
bool id[MAXN+2][MAXN+2];
int dis[MAXN][MAXN];
bool ok[MAXN],vis[MAXN+2][MAXN+2];
vector<int>v[MAXN*MAXN+10];

struct node{
    int x,y,dis;
    bool operator < (const node &a) const{
        return dis>a.dis;
    }
};
priority_queue<node>q;

inline void bfs(int x,int y,int pos){
    while(!q.empty()) q.pop();
    node vn,vw;
    clr(vis,0),clr(ok,0);
    vn.x=x,vn.y=y,vn.dis=0;
    int num=0;
    vis[x][y]=1;
    q.push(vn);
    while(!q.empty()){
        vn=q.top();q.pop();
        if(id[vn.x][vn.y]){
            int S=v[vn.x*n+vn.y].size();
            rep(i,0,S){
                int pp=v[vn.x*n+vn.y][i];
                if(!ok[pp] && pp!=pos){
                    ok[pp]=1;
                    num++;
                    dis[pos][pp]=vn.dis;
                }
            }
        }
        if(num+1==m) return;
        rep(i,0,4){
            int a=vn.x+dx[i],b=vn.y+dy[i];
            if(a>=0 && a<n && b>=0 && b<n && s[a][b]!='#' && !vis[a][b]){
                vis[a][b]=1;
                vw.x=a,vw.y=b,vw.dis=vn.dis+1;
                q.push(vw);
            }
        }
    }    
}

int main(){
    while(~scanf("%d%d",&n,&m)){
        rep(i,0,n) scanf("%s",s[i]);
        clr(id,0);
        rep(i,0,n*n+5) v[i].clear();
        rep(i,0,m){
            scanf("%d%d%d%d",&t[i].xs,&t[i].ys,&t[i].xe,&t[i].ye);
            t[i].xs--,t[i].ys--,t[i].xe--,t[i].ye--;
            id[t[i].xs][t[i].ys]=1;
            v[t[i].xs*n+t[i].ys].push_back(i);
        }
        rep(i,0,m){
            clr(dis[i],-1);
            dis[i][i]=0;
            bfs(t[i].xe,t[i].ye,i);
        }        
        clr(dp,-1);
        rep(i,0,m) dp[1<<i][i]=0;
        int all=(1<<m);
        rep(i,0,all) rep(j,0,m){
            if(i&(1<<j) && dp[i][j]!=-1){
                rep(k,0,m){
                    if(!(i&(1<<k)) && dis[k][j]!=-1){
                        int nxt=(i|(1<<k));
                        int val=dp[i][j]+dis[k][j];
                        if(dp[nxt][k]==-1 || val<dp[nxt][k]){
                            dp[nxt][k]=val;
                        }
                    }
                }
            }
        }
        int ans=-1;
        rep(i,0,m){
            if(dp[all-1][i]==-1) continue;
            if(ans==-1 || ans>dp[all-1][i]){
                ans=dp[all-1][i];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值