Harry And Dig Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 844 Accepted Submission(s): 344
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?
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≤n,m≤50) .
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤a[i][j]≤100 , and no more than 10 of a[i][j] will be positive integer).
For each test case, there are two integers n and m. (1≤n,m≤50) .
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0≤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
思路:好久没做状压,一直觉得自己没熟练掌握,还是要多练练。
贴一个官方题解:
由于Harry的dig machine是无限大的,而装载石头和卸载石头是不费时间的,所以问题可以转化成:从某一点出发,遍历网格上的一些点,每个点至少访问一次需要的最小时间是多少。这就是经典的旅行商问题,考虑到我们必须要遍历的点只有不到10个,可以用状态压缩解决。
Dp[i][j]表示i状态的点被访问过了,当前停留在点j 需要的最少时间。枚举另一点不在i状态内的点k,从点j节点走向点k,状态转移
Dp[i|(1≪k)][k]=min(Dp[i|(1≪k)][k],Dp[i][j]+Dis(j,k))
其中Dis(j,k)表示点j与点k的最短距离,这个可以通过坐标O(1)计算得到。若有t个点包含石头,则算法复杂度为O(n∗m+(t2)∗(2t))。
代码:
</pre><pre name="code" class="cpp">#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 55
#define INF 0x3f3f3f3f
int dp[(1<<11)+15][N];
int dist[N][N];
struct Node
{
int x,y;
} p[N];
int main()
{
int n,m,t;
int cnt;
while(~scanf("%d %d",&n,&m))
{
memset(dp,INF,sizeof(dp));
cnt=0;
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
scanf("%d",&t);
if(t||(i==0&&j==0))
{
p[cnt].x=i;
p[cnt++].y=j;
}
}
for(int i=0; i<cnt; i++)
{
for(int j=i+1; j<cnt; j++)
dist[i][j]=dist[j][i]=abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);
dist[i][i]=0;
}
dp[0][0]=0;
for(int i=0; i<(1<<cnt); i++)
{
for(int j=0; j<cnt; j++)
{
if(dp[i][j]==INF) continue;
for(int k=0; k<cnt; k++)
{
if(i&(1<<k)) continue;
dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dist[j][k]);
}
}
}
int ans=INF;
for(int i=0;i<cnt;i++)
ans=min(ans,dp[(1<<cnt)-1][i]+dist[i][0]);
printf("%d\n",ans);
}
return 0;
}