Algorithm: Hdu 4558 Swordsman Love (DP, West Hill 1, 2nd question)

Source: Internet
Author: User
Tags cas

Ideas:

This is the first DP problem you've ever had when you started a DP game.

In the game I thought of a state transition equation, f[i][j] [k][l][2], I and J are expressed in line I J, K and L represent the energy of man and sword, and the last one-dimensional 0 indicates that the current energy is replenished, 1 indicates to the sword.
Transfer to:
F[I][J][K][L][0] = F[i-1][j][k-mat[i][j]][l][1]+f[i][j-1][k-mat [i][j]][l][1];
F[I][J][K][L][1] = f[i-1][j][k][l-mat[i][j]][0]+f[i][j-1][k][l-mat[i][j]] [0];

But in the implementation, or encountered a variety of problems, always get no sample, so until the end of the game ...
After the end of debugging, and finally debugging out, the result of a turn, 477*477*11*11 complexity or tle ...
Then it is natural to think of dimensionality reduction, the energy of man and sword into the energy difference between man and sword.
But after dimensionality reduction, the state transition becomes unclear.
For example, when the difference is 2, there are [0,2],[1,3],[2,4] ... [8,10], the addition and subtraction operations within different ranges will result in the same results,
For example [0,2],0-2=9, into [9,2] difference is 7
And [2,4], 2-2=0, becomes a [0,2] difference of 2
I don't know how to move on.

Later, a way of thinking, for the difference K, corresponding to the energy of the person and the sword [x,y], means that X plus K will be equal to Y, can be figured out
How do state transitions [x-mat[i][j], y] and [X, Y-mat[i][j]] become the difference in the corresponding state?
Obviously, the former lets the difference increase mat[i][j] because y-x-(Y (x-mat[i][j)) = Mat[i][j], while the latter reduces the difference mat[i][j]

In a word, AC has made me very happy with this problem ^_^

Code:

 #include <iostream> #include <cstdio> #include <cstring> #include <string> #define MP Mak  
E_pair #define SQ (x) ((x) * (x)) typedef long long Int64;  
Const double PI = ACOs (-1.0);  
const int MAXN = 110;  
const int INF = 0X3F3F3F3F;  
     
using namespace Std;  
const int MOD = 1000000007;  
int n, m;  
int f[2][480][22][2];  
int mat[480][480];  
     
Char str[1000];  
     
    int main () {int ncase, Cas=1;  
    scanf ("%d", &ncase);  
        while (ncase--) {scanf ("%d%d%*c", &n, &m);  
            for (int i=1; i<=n; ++i) {gets (str);  
        for (int j=1; j<=m; ++j) mat[i][j] = str[j-1]-' 0 ';  
     
        } memset (f, 0, sizeof (f));  
        int ans = 0;  
     
        BOOL p = 0;  
            for (int i=1; i<=n; ++i) {p =!p;  
            memset (F[p], 0, sizeof (f[p)); for (int j=1; j<=m; ++j) {for (int k=0; k<=10; ++k) {  
                    int x1 = (k+mat[i][j])%11;  
                    int x2 = (k-mat[i][j]+11)%11;  
                    F[p][j][k][0] + = ((f[!p][j][x1][1]+f[p][j-1][x1][1])%mod)%mod;  
                F[P][J][K][1] + = ((F[!p][j][x2][0]+f[p][j-1][x2][0])%mod)%mod;   
            //Add a situation starting at the current point//www.bianceng.cn ++f[p][j][11-mat[i][j]][0];  
                for (int j=1; j<=m; ++j) {ans + = (f[p][j][0][0] + f[p][j][0][1])%mod;  
            Ans%= MOD;  
    } printf ("Case%d:%d\n", cas++, ans);  
return 0; }

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.