6d-Dynamic Planning

Source: Internet
Author: User

The N monsters stood in a row to let the mage's fireball attack. The most powerful attacking monster caused a damage, which caused B damage to nearby monsters. If the blood volume of the monsters is less than 0, the system will die, find the minimum number of attacks and output the target of each attack (The two-side monster cannot be attacked

 

#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <deque>#include <stack>#include <bitset>#include <algorithm>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <ctime>#include <cstring>using namespace std;#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)#define foreach(c,itr) for(__typeof((c).begin()) itr=(c).begin();itr!=(c).end();itr++)typedef long long ll;typedef long double ld;int n, b, a;int h[20];int dp[11][20][20];int calc(int ind, int H0, int H1) {    int h0 = H0, h1 = H1;    if (dp[ind][H0][H1] != -1)        return dp[ind][H0][H1];    int h2 = h[ind + 1];    int res = 1 << 20;    int cur = 0;    if (ind == n - 2) {        while (h0 > 0 || h1 > 0 || h2 > 0) {            cur++;            h0 -= a;            h1 -= b;            h2 -= a;        }        return dp[ind][H0][H1] = cur;    }    while (h0 > 0) {        cur++;        h0 -= a;        h1 -= b;        h2 -= a;    }    for (int i = 0; i < 20; ++i) {        res = min(res, cur + calc(ind + 1, max(h1, 0), max(h2, 0)));        cur++;        h0 -= a;        h1 -= b;        h2 -= a;    }    return dp[ind][H0][H1] = res;}string sp = "";void build(int ind, int H0, int H1) {    int h0 = H0, h1 = H1;    int h2 = h[ind + 1];    int res = dp[ind][H0][H1];    int cur = 0;    if (ind == n - 2) {        while (h0 > 0 || h1 > 0 || h2 > 0) {            cout << sp << ind+1;            sp = " ";            cur++;            h0 -= a;            h1 -= b;            h2 -= a;        }        return;    }    while (h0 > 0) {        cout << sp << ind+1;        sp = " ";        cur++;        h0 -= a;        h1 -= b;        h2 -= a;    }    for (int i = 0; i < 20; ++i) {        if( res == cur + calc(ind + 1, max(h1, 0), max(h2, 0))) {            build(ind + 1, max(h1, 0), max(h2, 0));            return;        }        cout << sp << ind+1;        sp = " ";        cur++;        h0 -= a;        h1 -= b;        h2 -= a;    }}int main() {    cin >> n >> b >> a;    for (int i = 0; i < n; ++i) {        cin >> h[i];        h[i]++;    }    memset(dp, -1, sizeof dp);    int res = calc(1, h[0], h[1]);    cout << res << endl;    build(1, h[0], h[1]);    cout << endl;    return 0;}

 

6d-Dynamic Planning

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.