Poj 1185 artillery distributed pressure DP

Source: Internet
Author: User

Link: http://poj.org/problem? Id = 1185

A map has two kinds of terrain: H and P. A gun can be placed on P. The attack ranges from top to bottom, from left to right, the question is how many guns can be placed on a map at most. Row n <= 100, column M <= 10.

Idea: Because guns cannot be placed in the upper, lower, and lower layers, the number of States in each row is reduced from 2 ^ 10 to 60. The state transition equation is DP [I] [J] [k] = max (DP [I-1] [k] [l] + BB [J]). DP [I] [J] [k] indicates the total number of Turret points placed in the first line of I in the state of J and in the first line of the I-1 in the state of K. Bb [J] indicates the number of Turret in the whole row of status K. After hitting this question, I began to be familiar with the pressure DP.

Code:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<cstdlib>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)#define maxn 10005#define INF 0x7ffffffftypedef long long ll;using namespace std;int judge(int x){    if((x&(x<<1))==0&&((x&(x<<2))==0)&&(((x<<1)&(x<<2))==0))        return 1;    return 0;}int main(){    int row[105]={0};    int aa[105]={0};    int bb[105]={0};    int top=0;    char land[105][15];    int dp[105][60][60]={0};    int r,c;    scanf("%d%d",&r,&c);    for(int i=0;i<(1<<c);i++)    {        if(judge(i))        {            int k=i;            while(k)            {                if(k%2==1)                    bb[top]++;                k/=2;            }            aa[top++]=i;        }    }    for(int i=0;i<r;i++)    {        scanf("%s",land[i]);        for(int j=0;j<c;j++)        {            row[i]*=2;            if(land[i][j]=='P')                row[i]++;        }    }    for(int i=0;i<top;i++)    {        if((aa[i]&row[0])==aa[i])        {            dp[0][i][0]=bb[i];        }    }    for(int i=0;i<top;i++)    {        for(int j=0;j<top;j++)        {            if((aa[i]&aa[j])==0&&(aa[i]&row[1])==aa[i]&&(aa[j]&row[0])==aa[j])                dp[1][i][j]=dp[0][j][0]+bb[i];        }    }    for(int i=2;i<r;i++)    {        for(int j=0;j<top;j++)        {            for(int k=0;k<top;k++)            {                for(int l=0;l<top;l++)                {                    if((aa[j]&aa[k])==0&&(aa[l]&aa[j])==0&&(aa[l]&aa[k])==0&&(aa[j]&row[i])==aa[j]&&(aa[k]&row[i-1])==aa[k]&&(aa[l]&row[i-2])==aa[l])                    dp[i][j][k]=max(dp[i][j][k],dp[i-1][k][l]+bb[j]);                }            }        }    }    int ans=0;    for(int i=0;i<top;i++)    {        for(int j=0;j<top;j++)        {            if(dp[r-1][i][j]>ans)                ans=dp[r-1][i][j];        }    }    printf("%d\n",ans);}


Poj 1185 artillery distributed 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.