hdu 5067 Harry And Dig Machine

http://acm.hdu.edu.cn/showproblem.php?pid=5067

题意:

二维网格图中有若干块石头,从左上角出发,搬走所有石头,再回到左上角的最短距离

旅行商问题

参考上一篇博客

#include<cstdio>
#include<algorithm>
 
using namespace std;

#define N 11

int dp[1<<N][N]; 
int dis[N][N];

int stx[N],sty[N];

int main()
{
     int n,m,tot,x,S;
     while(scanf("%d%d",&n,&m)!=EOF)
     {
          tot=0;
          for(int i=1;i<=n;++i)
              for(int j=1;j<=m;++j)
              {
                  scanf("%d",&x);
                  if(x) stx[++tot]=i,sty[tot]=j;
              }
          for(int i=1;i<=tot;++i)
              for(int j=1;j<=tot;++j)
                  dis[i][j]=abs(stx[i]-stx[j])+abs(sty[i]-sty[j]);
          S=(1<<tot+1)-1;
          for(int i=1;i<=S;++i)
              for(int j=0;j<=tot;++j)
                  dp[i][j]=1e9;
          for(int i=1;i<=tot;++i) dp[0][i]=dis[0][i]=stx[i]-1+sty[i]-1;
         for(int i=1;i<S;++i)
             for(int j=0;j<=tot;++j)
                 if(!(i&1<<j))
                     for(int k=1;k<=tot;++k)
                         if(i&1<<k)
                             dp[i][j]=min(dp[i][j],dis[j][k]+dp[i^1<<k][k]);
         printf("%d\n",dp[S-1][0]);
    }
}

猜你喜欢

转载自www.cnblogs.com/TheRoadToTheGold/p/12384911.html
dig