zoukankan      html  css  js  c++  java
  • 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


    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.(1n,m50).
    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.( 0a[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 And Dig Machine

    思路:状态压缩dp,bestcode第二题. 

     1 /*======================================================================
     2  *           Author :   kevin
     3  *            Email :   ubuntu_kevin@126.com
     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
    ",ans);
    83     }
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    ES6-10笔记(class类)
    ES6-10笔记(let&const -- Array)
    小程序的表单提交
    小程序表单回显
    小程序template模板的使用和模板多数据传递
    微信小程序的初始配置
    babel 版本原因运行报错,解决办法
    webpack 和 webpack-cli 安装和使用中出现的问题
    jQuery中操作属性的方法attr与prop的区别
    javaScript 添加和移除class类名的几种方法
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/4060481.html
Copyright © 2011-2022 走看看