Harry and Dig Machine
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): Accepted submission (s): 99
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 have to gain knowledge from other certain subjects, such as language, mathematics, 中文版, 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 a 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 is useful, so we just move them to the Top-left cell. Taking it into account which Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore Deci Des to let him does this job and wants it-done as quickly as possible. Harry needs one unit time to move he 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 are 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 Minim Al time Harry needs to finish it?
Inputthey is sever test cases, you should process to the end of file.
For each test case, there is integers n and M.(1≤n,m≤) .
The next n line contains M integer. The j-th number ofItH Line A[i][j] means there is a[i][j] stones on theJTH Cell of theItH Line. ( 0≤a[i][J]≤ , and no more than of a[i][j] would be positive integer).
Outputfor Each test case, just output one line, contains a integer indicate the minimal time that Harry can finish hi S job.
Sample INPUT3 30 0 00 100 00 0 02 21 11 1
Sample Output44
Sourcebestcoder Round #14
Idea: Hamiltonian loop problem, Dp[i][j] indicates that the last node is the shortest distance of J when the state is I
I of the state means that I is a binary, if the J point is inside, the first J bit is 1.
When the game, the left and right, but also drunk ...
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<vector>#include<Set>#include<stack>#include<map>#include<ctime>#include<bitset>#defineLL Long Long#defineMAXN 110#defineINF 0x3f3f3f3fusing namespacestd;intmat[maxn][maxn],dis[ Max][ Max];intMAP1[MAXN][MAXN], dp[(1<<Ten)+Ten][ the] ;BOOLvi[ -][ -];structnode{intx, y;} qe[ -];intMain () {inti,j,k; intTmp,t,x,ans; intN,m,u; while(SCANF ("%d%d", &n,&m)! =EOF) { for(i =1; I <= N; i++) for(j =1; J <= m;j++) scanf ("%d",&Mat[i][j]); X=0 ; for(i =1; I <= N; i++) for(j =1; J <= M; j + +) { if(mat[i][j]>0) {qe[x].x=i; Qe[x].y=J; MAP1[I][J]=x++; } Elsemap1[i][j]=-1; } for(i =1; I <= N; i++) for(j =1; J <= M; j + +)if(mat[i][j]>0) { for(k =0; K < x; k++) {DIS[MAP1[I][J]][MAP1[QE[K].X][QE[K].Y]]= ABS (qe[k].x-i) +abs (qe[k].y-j); }} memset (Dp,inf,sizeof(DP)); for(i =0; I < x;i++) {dp[1<<i][i] = ABS (1-qe[i].x) +abs (1-qe[i].y); } for(i =1; I < (1<<X); i++) for(j =0; J < X; j + +)if(Dp[i][j]! =INF) { for(U =0; u < x; u++)if((i& (1<<u)) = =0) {Dp[i| (1<<u)][u] = min (dp[i| (1<<u)][u],dis[j][u]+Dp[i][j]); } } intans=INF; for(i =0; i < x; i++) {ans= Min (ans,dp[(1<<X)-1][i]+abs (1-qe[i].x) +abs (1-qe[i].y)) ; } if(Ans==inf) ans=0; printf ("%d\n", ans); } return 0 ;}
View Code
Hdu 5067 Harry and Dig machine