hdu 5067 -- Harry And Dig Machine
Harry And Dig Machine
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 659 Accepted Submission(s): 252
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?
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).
思路:状态压缩dp,bestcode第二题.
1 /*====================================================================== 2 * Author : kevin 3 * Email : [email protected] 4 * Filename : HarryAndDigMachine.cpp 5 * Creat time : 2014-10-29 16:35 6 * Description : 7 ========================================================================*/ 8 #include <iostream> 9 #include <algorithm> 10 #include <cstdio> 11 #include <cstring> 12 #include <queue> 13 #include <cmath> 14 #define clr(a,b) memset(a,b,sizeof(a)) 15 #define INF 0x7f7f7f7f 16 #define M 15 17 using namespace std; 18 inline int min_32(int (a),int (b)){return (a)<(b)?(a):(b);} 19 inline int max_32(int (a),int (b)){return (a)>(b)?(a):(b);} 20 inline long long min_64(long long (a),long long (b)){return (a)<(b)?(a):(b);} 21 inline long long max_64(long long (a),long long (b)){return (a)>(b)?(a):(b);} 22 int n,m; 23 int dp[1<<M][M]; 24 int dis[M][M]; 25 struct Node 26 { 27 int x,y; 28 }node[M]; 29 int main(int argc,char *argv[]) 30 { 31 while(scanf("%d%d",&n,&m) != EOF){ 32 int cnt = 1; 33 int a; 34 node[0].x = 0; 35 node[0].y = 0; 36 for(int i = 0; i < n; i++){ 37 for(int j = 0; j < m; j++){ 38 scanf("%d",&a); 39 if(a > 0){ 40 node[cnt].x = i; 41 node[cnt++].y = j; 42 } 43 } 44 } 45 for(int i = 0; i < cnt; i++){ 46 for(int j = 0; j < cnt; j++){ 47 dis[i][j] = (fabs(node[i].x-node[j].x) + fabs(node[i].y-node[j].y)); 48 } 49 } 50 /* 51 for(int k = 0; k < cnt; k++){ 52 for(int i = 0; i < cnt; i++){ 53 for(int j = 0; j < cnt; j++){ 54 dis[i][j] = min_32(dis[i][j],dis[i][k]+dis[k][j]); 55 } 56 } 57 } 58 */ 59 clr(dp,0); 60 for(int state = 0; state < (1<<cnt); state++){ 61 for(int i = 0; i < cnt; i++){ 62 if(state & (1<<(i))){ 63 if(state == (1<<(i))){ 64 dp[state][i] = dis[0][i]; 65 } 66 else{ 67 dp[state][i] = INF; 68 for(int j = 0; j < cnt; j++){ 69 if(i != j && state & (1<<(j))){ 70 int t = (state ^ (1<<(i))); 71 dp[state][i] = min_32(dp[state][i],dp[t][j]+dis[j][i]); 72 } 73 } 74 } 75 } 76 } 77 } 78 int ans = dp[(1<<cnt)-1][0] + dis[0][0]; 79 for(int i = 1; i < cnt; i++){ 80 ans = min_32(ans,dp[(1<<cnt)-1][i]+dis[i][0]); 81 } 82 printf("%d\n",ans); 83 } 84 return 0; 85 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。