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
",ans); } return 0; }

좋은 웹페이지 즐겨찾기