HDU 5067 Harry And Dig Machine

来源:互联网 发布:arm java 指令集 编辑:程序博客网 时间:2024/05/16 19:57

Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 378    Accepted Submission(s): 123


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 30 0 00 100 00 0 02 21 11 1
 

Sample Output
44
 


Source

BestCoder Round #14



题意:

给你不超过10个点,求从左上角开始将所有点遍历一遍最终回到起点的最小步数。


分析:

开始以为是贪心,想了会不知如何实现,又想到双向dp,不会,题解看不懂,搜了份dfs,不错


代码如下:

#include<stdio.h>#include<iostream>#include<string.h>#include<cmath>#include<algorithm>#define For(i, a, b) for(int i = a; i < b; i++)using namespace std;const int maxn = 10 + 5;struct node{    int x, y;} N[maxn];int Ivis[maxn];int Imin, t;void dfs(int cnt, int pos, node u){    if(pos > Imin) return ; //剪枝    if(cnt == 0){           //递归出口        pos += (u.x + u.y);        Imin = min(Imin, pos);        return;    }    for(int i = 0; i < t; i++){   //遍历未搜的点        if(!Ivis[i]){           int p = pos + (abs(N[i].x - u.x) + abs(N[i].y - u.y));//不可pos+=,递归回来时pos变了           Ivis[i] = 1;           dfs(cnt-1, p, N[i]); //不可用cnt--!即dfs(cnt--, pos, N[i])错误           Ivis[i] = 0;        }    }    return;}int main(){    int n, m;    //freopen("in.txt", "r", stdin);    while(~scanf("%d%d", &n, &m)){        memset(Ivis, 0, sizeof(Ivis));        int d;        t = 0;        For(i , 0, n) For(j, 0, m) {            scanf("%d", &d);            if(i == 0 && j == 0)                continue;            if(d){                N[t].x = i;                N[t++].y = j;            }        }        Imin = 99999999;        node u;        u.x = 0; u.y = 0;        int pos = 0;        dfs(t, pos, u);        printf("%d\n", Imin);    }    return 0;}



0 0
原创粉丝点击