POJ 1661 help Jimmy (Memory search)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1661

Also can be a memory search problem, mainly to think about how to set DP, memory search is to avoid recursive process of repeated evaluation, so to get DP must know how to recursion

Since this is a can move around the recursive process is definitely designed around so the one-dimensional DP is from the left or the right, and the number of layers related to the other dimension is the number of layers

You can then get dp[count][flag],flag=1 that indicates how long the count layer will take from the left, and flag=0 indicates how long the count layer will be from the right. And then there's the DFS recursion.

Process

#include <iostream> #include <algorithm> #include <cstring> #include <cstdio>using namespace std;const int M = 2e4 +, MM = 6e4 + 10;int n, x, y, max;int dp[1010][2];struct TnT {int sta, ed, H;} S[1010];bool CMP (TNT A, TNT b) {return a.h > b.h;}    int dfs (int count, int flag) {if (count = = N) {return 0;    } int ans = MM;    if (Dp[count][flag]! =-1) {return dp[count][flag];    } int temp = count;        for (int i = count + 1; I <= n; i++) {temp = i; if (s[count].h-s[i].h <= MAX) {if (flag = = 1) {if (S[count].ed <= s[i].ed && S[co Unt].ed >= S[i].sta) {ans = min (dfs (i, flag) + S[i].ed-s[count].ed, DFS (i, 1-flag) + S[count                    ].ed-s[i].sta);                Break }} if (flag = = 0) {if (S[count].sta <= s[i].ed && s[count].sta >= s[i]. STA) {ans = min (Dfs (i , flag) + S[count].sta-s[i].sta, DFS (i, 1-flag) + S[i].ed-s[count].sta);                Break        }}} else {break; }} if (ans = = MM) {if (temp = = N) {if (S[count].h > MAX) {Dp[count][flag] = ans                ;            return ans;                } else {ans = 0;                Dp[count][flag] = ans;            return ans;        }} Dp[count][flag] = ans;    return ans;        } else {Dp[count][flag] = ans;    return ans;    }}int Main () {int t;    scanf ("%d", &t);        while (t--) {scanf ("%d%d%d%d", &n, &x, &y, &max);        for (int i = 1; I <= n; i++) {scanf ("%d%d%d", &s[i].sta, &s[i].ed, &s[i].h);        } sort (s + 1, s + N + 1, CMP);        S[0].sta = S[0].ed = x, s[0].h = y;        S[n + 1].sta =-M, s[n + 1].ed = m, s[n + 1].h = 0; MEmset (DP,-1, sizeof (DP));        int GG = DFS (0, 0);        GG = min (dfs (0, 1), GG);    printf ("%d\n", GG + y); } return 0;}

Poj 1661 Help Jimmy (memory search)

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.