hdu 4856 Tunnels (BFS+状压)

题目链接

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
 

Source
2014西安全国邀请赛 
题意:给你一张n*n的图,其中"."为可走,”#"为不可走,再给你m条通道的入口和出口(代表单向),通道直接穿过不需要时间,点可到相邻的点,话费1个单位时间,问你以任意个通道入口为起点,走完所有通道而且每个通道只走一次,最少需要多少时间。
分析: 这题相当于裸的旅行商问题,只需要bfs预处理每个通道的出口到其他通道的入口的最短距离,然后状压dp搞定。
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define inf 0x3f3f3f
using namespace std;
const int f[4][2]={1,0,-1,0,0,1,0,-1};
char ma[20][20];
int n,m;
struct node
{
    int x1,y1;
    int x2,y2;
}a[20];///保存m个通道的入口和出口坐标
struct Node
{
    int x,y,dis;
};
int b[20][20][20][20];//存的是一个通道的出口到其他通道的入口的最短距离,前两维表示出口坐标,后两维表示入口坐标
int ex,ey;
bool vis[20][20];
int dp[33000][20];
int bfs(int sx,int sy)
{
    memset(vis,false,sizeof(vis));
    queue<Node> q;
    Node st,en;
    st.x=sx,st.y=sy,st.dis=0;
    q.push(st);
    vis[sx][sy]=true;
    while(q.size())
    {
        st=q.front();
        q.pop();
        if(st.x==ex&&st.y==ey) return st.dis;
        for(int i=0;i<4;i++)
        {
            int mx=st.x+f[i][0];
            int my=st.y+f[i][1];
            if(mx<1||mx>n||my<1||my>n||vis[mx][my]||ma[mx][my]=='#') continue;
            vis[mx][my]=true;
            en.x=mx,en.y=my;
            en.dis=st.dis+1;
            q.push(en);
        }
    }
    return inf;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%s",ma[i]+1);
        for(int i=0;i<m;i++)
            scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
        memset(b,inf,sizeof(b));
        for(int i=0;i<m;i++)///预处理出口到其他入口之间的最短距离
        {
            for(int j=0;j<m;j++)
            {
                if(i==j) continue;
                ex=a[j].x1,ey=a[j].y1;
                int d=bfs(a[i].x2,a[i].y2);
                b[a[i].x2][a[i].y2][a[j].x1][a[j].y1]=d;
            }
        }
        int N=1<<m;
        for(int i=0;i<N;i++)
            for(int j=0;j<m;j++)
            dp[i][j]=inf;
        for(int i=0;i<m;i++) dp[(1<<i)][i]=0;
        int ans=inf;
        for(int i=1;i<N;i++)
        {
            int flog=1;
            for(int j=0;j<m;j++)
            {
                if(!(i&(1<<j)))
                {
                    flog=0;
                    continue;
                }
                for(int k=0;k<m;k++)
                {
                    if(!(i&(1<<k))||j==k) continue;
                    dp[i][j]=min(dp[i][j],dp[i^(1<<j)][k]+b[a[k].x2][a[k].y2][a[j].x1][a[j].y1]);
                }
            }
            if(flog)
            {
                for(int j=0;j<m;j++)
                    ans=min(ans,dp[i][j]);
            }
        }
        if(ans==inf) puts("-1");
        else printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(dp,bfs,旅行商问题,状压dp)