HDU 5067 Harry and Dig machine (pressure DP)

Source: Internet
Author: User

I feel like these two days how always meet the pressure ah ....

The number 20 below, the first want to like pressure ah 、、、

But it's time for this crime to forget to consider the absence of stone.

Simple pressure: Indicates that the state is J at the end of section I.

PS: This question is also praised the big Blue Xiang Excavator technology AH. Drunk Ah ...

Harry and Dig Machine Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 468 Accepted Submission (s): 170


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 of < Span class= "Mrow" id= "mathjax-span-13" style= "" > i < Span class= "Mi" id= "mathjax-span-18" style= "" >t h Line A[i][j] means there is a[i][j] stones on the < Span class= "Mrow" id= "mathjax-span-21" style= "" > j < Span class= "Mi" id= "mathjax-span-26" style= "" >t h Cell of the < Span class= "Mrow" id= "mathjax-span-29" style= "" > i < Span class= "Mi" id= "mathjax-span-34" style= "" >t h 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 Input
3 30 0 00 100 00 0 02 21 11 1

Sample Output
44

Sourcebestcoder Round #14
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <map> #include <set> #define EPS 1e-8#define M 1000100#define ll __int64//#define LL Long Lon G#define INF 0x3f3f3f#define PI 3.1415926535898const int maxn = 55;int mp[maxn][maxn];int N, m;int dis[maxn][maxn];using n Amespace std;int dp[12][1<<12];struct node{int x, y;} f[12];int Main () {while (~SCANF ("%d%d", &n, &m)        ) {int ans = 0; for (int i = 1; I <= n; i++) {for (int j = 1; j <= M; j + +) {scanf ("%d", &                AMP;MP[I][J]);                    if (Mp[i][j]) {f[ans].x = i;                F[ans++].y = j;        }}} memset (dis, 0, sizeof (DIS));         for (int i = 0; i < ans; i++)   for (int j = 0; J < ans; j + +) Dis[i][j] = (ABS (f[i].x-f[j].x) +abs (F[I].Y-F[J].Y));        for (int i = 0, i < ans; i++) for (int j = 0; J < (1<<ans); j + +) dp[i][j] = INF;        for (int i = 0; i < ans; i++) dp[i][(1<<i)] = (ABS (f[i].x-1) +abs (f[i].y-1));                for (int j = 0; J < (1<<ans); j + +) {for (int k = 0; k < ans; k++) { if (! (                j& (1<<k))) continue;                    for (int p = 0; p < ans; p++) {if (j& (1<<p)) continue; Dp[p][j| ( 1<<p)] = min (Dp[k][j] + dis[k][p], dp[p][j| (                1&LT;&LT;P)]);        }}} int Min = INF;        for (int i = 0; i < ans; i++) min = min (min, dp[i][(1<<ans) -1]+abs (f[i].x-1) +abs (f[i].y-1));        if (min = = INF) min = 0;    printf ("%d\n", Min); } return 0;}


HDU 5067 Harry and Dig machine (pressure DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.