您的位置:首页 > 其它

Harry And Dig Machine HDU - 5067 (状压dp)

2017-09-28 00:00 435 查看
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*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 \leq n, m \leq 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 \leq a[i][j] \leq 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

大致题意:给你一个n*m的矩阵,然后让你从(0,0)点开始走,每在单位时间内能走一格(上下左右),问你经过所有正值的格子然后回到起点的最少时间。

思路:状压dp,dp[i][j]表示到达i格子时的状态为j时所花最少时间。

代码如下

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
int N,n,m;
struct node
{
int x,y;
};
node dis[11];
int dp[11][(1<<11)];
int  min(int x,int y)
{
if(x==-1) return y;
if(y==-1) return x;
if(x>y) return y;
return
4000
x;
}
int cost(int i,int j)
{
return (abs(dis[i].x-dis[j].x)+abs(dis[i].y-dis[j].y));
}
int main ()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
N=1;
dis[0].x=0;//将起点记为0号点
dis[0].y=0;

for(int i=0;i<n;i++)//记录正值点的坐标
for(int j=0;j<m;j++)
{
int val;
scanf("%d",&val);
if(val&&(i+j))
{
dis
.x=i;
dis
.y=j;
N++;
}
}

if(N==1)//如果除起点外无正值点
{
printf("0\n");
continue;
}

memset(dp,-1,sizeof(dp));
dp[0][0]=0;
int len=(1<<N)-1;
for(int state=0;state<=len;state++)
for(int i=0;i<N;i++)//假设从i号格子出发
{
if(dp[i][state]!=-1)//如果此时i号格子的状态为state可达
{
for(int j=0;j<N;j++)//到j号格子
{
if((1<<j)^state)//如果j号格子还没走过
dp[j][(1<<j)|state]=min(dp[j][(1<<j)|state],dp[i][state]+cost(i,j));
}
}
}
printf("%d\n",dp[0][len]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: