hdu 4856

Tunnels

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


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
状态压缩+bfs()。
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#define inf 0x3f3f3f3f
#define N 20
using namespace std;
int vis[N][N],n,dis[N][N],dp[1<<16][16];
char aa[N][N];
struct node
{
    int x1,y1,x2,y2;
}b[N];
struct pp
{
    int x,y;
    int step;
};
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int min(int a,int b)
{
    return a<b?a:b;
}
int distance(int x1,int y1,int x2,int y2)
{
    int i,x,y;
    queue<struct pp>q;
    memset(vis,0,sizeof(vis));
    struct pp cur,next;
    cur.x=x1;
    cur.y=y1;
    cur.step=0;
    vis[cur.x][cur.y]=1;
    if(x1==x2&&y1==y2)//注意这里,有可能两个点是相同的
        return 0;
    q.push(cur);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        for(i=0;i<4;i++)
        {
            x=next.x=cur.x+dir[i][0];
            y=next.y=cur.y+dir[i][1];
            next.step=cur.step+1;
            if(x>=1&&x<=n&&y>=1&&y<=n&&aa[x][y]!='#'&&!vis[x][y])
            {
                if(x==x2&&y==y2)
                    return next.step;
                q.push(next);
                vis[x][y]=1;
            }
        }
    }
    return inf;
}
int main()
{
    int m,i,j,s,k,ans,cu;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        getchar();
        for(i=1;i<=n;i++)
{
            for(j=1;j<=n;j++)
               scanf("%c",&aa[i][j]);
getchar();
}
        for(i=0;i<m;i++)
            scanf("%d%d%d%d",&b[i].x1,&b[i].y1,&b[i].x2,&b[i].y2);
for(i=0;i<m;i++)//距离初始化
            for(j=0;j<m;j++)
            {
if(j!=i)
                   dis[i][j]=distance(b[i].x2,b[i].y2,b[j].x1,b[j].y1);
else
                   dis[i][j]=0;
            }


        s=1<<m;
        //dp[i][j]表示选择j以后,组成了i状态。
        memset(dp,inf,sizeof(dp));
for(i=0;i<m;i++)//初始化只选择了一个的状态
for(j=0;j<m;j++)
dp[1<<i][j]=0;
        for(i=1;i<s;i++)
        {
            for(j=0;j<m;j++)
            {
                if(i&(1<<j))
                {
                    cu=i^(1<<j);//前一个状态
                    for(k=0;k<m;k++)
                    {
                        if((cu&(1<<k))!=0&&k!=j&&dis[k][j]!=inf)//由前一个状态推出后一个状态
                        {
                            dp[i][j]=min(dp[i][j],dp[cu][k]+dis[k][j]);
                        }
                    }
                }
            }
        }
        ans=inf;
        for(i=0;i<m;i++)
            ans=min(ans,dp[s-1][i]);
      if(ans==inf)
          printf("-1\n");
      else
          printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值