# Cannon (Checkerboard DP)

Source: Internet
Author: User

Always think that they write is the state compression, the result is written to know is a chessboard DP

First look at the topic

Yes, chess, or only chess.

For the number of scenarios, I was the first to consider DFS, but the time-out is stable, so decisively give up

And remember that there was a DP question similar to this one before.

So the idea began to shift to DP

After careful consideration, the state of the chessboard is pressed to three dimensions.

DP[I][K][J];

I: The first line of the Chessboard K: a few columns of the previous I row put a gun chess J: There are a few columns in the front I row two gun chess

Because the cannon will be played across the board, so there are up to two guns in a row or row

So

There are 6 elements of the DP equation:

1: No blasting chess, so the equation is dp[i][k][j]+=dp[i-1][k][j];

2: Put a cannon in a row without gun chess, the equation is dp[i][j][k]+=dp[i-1][k-1][j]* (m-k-j+1);

3: Put a cannon in a row with a gun, the equation is dp[i][j][k]+=dp[i-1][k+1][j-1]* (k+1);

4: Put a gun in a row with a gun and a gun in a row without a gun, the equation is dp[i][j][k]+=dp[i-1][k][j-1]* (k) * (m-j-k+1)/2;

5: Put a gun in a row with a gun and a gun in a row with a cannon, the equation is dp[i][k][j]+= (k+2) * (k+1)/2*dp[i-1][k+2][j-2];

6: Put a gun in a row without a gun and put a gun in a row without a gun, the equation is dp[i][k][j]+= (m-j-k+2) * (m-j-k+1)/2*dp[i-1][k-2][j];

Okay, now we can put the code on.

`#include <cstdio>#include<iostream>#defineMoD 999983using namespacestd;intn,m;Long Long intdp[101][101][101],ans=0;intMain () {scanf ("%d%d",&n,&m); dp[0][0][0]=1;  for(intI=1; i<=n;i++)    {         for(intk=0; k<=m;k++)        {             for(intj=0; j<=m-k;j++) {Dp[i][k][j]=dp[i-1][k][j]; if(k) dp[i][k][j]+= (m-k-j+1) *dp[i-1][k-1][j]%MoD; if(J&&k<m) dp[i][k][j]+= (k +1) *dp[i-1][k+1][j-1]%MoD; if(k>1) dp[i][k][j]+= (m-j-k+2) * (m-j-k+1)/2*dp[i-1][k-2][j]%MoD; if(j>1&&k<m-1) dp[i][k][j]+= (k +2) * (k +1)/2*dp[i-1][k+2][j-2]%MoD; if(k&&j) dp[i][k][j]+= (m-j-k+1) * (k) *dp[i-1][k][j-1]%MoD; DP[I][K][J]%=MoD; }        }    }     for(intI=0; i<=m;i++)    {         for(intj=0; j<=m-i;j++) {ans+=Dp[n][i][j]; Ans%=MoD; }} printf ("%lld\n", ans); return 0;}`

Cannon (Checkerboard DP)

Related Keywords:

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.