HDU 5067 Harry And Dig Machine

HDU 5067 Harry And Dig Machine

题目链接:HDU 5067 Harry And Dig Machine


Problem Description

As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n <script type="math/tex" id="MathJax-Element-8">*</script>m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?

Input

They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m.(1≤n,m≤50)(1≤n,m≤50).
The next n line, each line contains m integer. The j-th number of i-th line a[ i ][ j ] means there are a[ i ][ j ] stones on the j-th cell of the i-th line.( 0≤a[ i ][ j ]≤1000≤a[ i ] [ j ]≤100 , and no more than 10 of a[ i ][ j ] will be positive integer).

Output

For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.

Sample Input

3 3
0 0 0
0 100 0
0 0 0
2 2
1 1
1 1

Sample Output

4
4

题意:

从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少。

AC代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <locale>
#include <vector>
#include <string>
#include <iomanip>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>

using namespace std;

typedef pair<int,int> P;

int mapp[55][55];
int dis[55][55];
vector<P> v;
int dp[1<<12][12];

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        v.clear();
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&mapp[i][j]);
                if(i==1&&j==1)
                    continue;
                if(mapp[i][j])
                    v.push_back(make_pair(i,j));
            }
        }
        int l=v.size();
        for(int i=0; i<l; i++)
            for(int j=0; j<l; j++)
                dis[i+1][j+1]=abs(v[i].first-v[j].first)+abs(v[i].second-v[j].second);
        for(int i=0; i<l; i++)
            dis[0][i+1]=dis[i+1][0]=v[i].first+v[i].second-2;
        l++;
        if(l==1)
        {
            printf("0\n");
            continue;
        }
        int zong=1<<l;
        memset(dp,0x3f,sizeof dp);
        for(int i=1; i<l; i++)
            dp[1<<i][i]=dis[0][i];
        for(int i=1; i<zong; i++)
            for(int j=0; j<l; j++)
                if((1<<j)&i)
                    for(int k=0; k<l; k++)
                    {
                        if(k==j)
                            continue;
                        if((1<<k)&i)
                            dp[i][j]=min(dp[i][j],dp[i-(1<<j)][k]+dis[k][j]);
                    }
        printf("%d\n",dp[zong-1][0]);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值