HDU 2167 State Compression DP

Source: Internet
Author: User

And hang on the stone to take the problem is almost more than a diagonal also not the next is the input format

Pre-processing all the states that satisfy a row are listed and the corresponding and calculated

And then according to the DP corresponding ideas to ask

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace Std;


int num[20][20];
int mark[1<<16];
int sum[20][1<<16],have[100000],cont;
int n;
int deal ()
{
int i;
cont=0;
int t=1<<n;
memset (sum,0,sizeof (sum));
printf ("%d\n", t);
for (i=0;i<t;i++)
{
if (i& (i<<1)) continue;
Have[++cont]=i;
if (i==0) continue;
int a=i,b=n;
while (A/2)
{
if (a%2)
{
for (int k=1;k<=n;k++)
{
SUM[K][CONT]+=NUM[K][B];
}
}
a/=2;
b--;
}
for (int k=1;k<=n;k++)
SUM[K][CONT]+=NUM[K][B];
}
return 0;
}
int max (int a,int b)
{
Return a>b?a:b;
}
int main ()
{
Char str[500];
int i,j;
while (gets (str))
{
int Len=strlen (str);
int t=0;
for (i=0,j=0;i<=len;i++)
{
if (str[i]== ' | | | I==len)
{
t++;
num[1][++j]=str[i-1]-' 0 ' + (str[i-2]-' 0 ') *10;
}
}
for (i=2;i<=t;i++)
for (j=1;j<=t;j++)
scanf ("%d", &num[i][j]);
n=t;
Deal ();
int dp[20][1<<n];
Memset (Dp,0,sizeof (DP));

for (i=0;i<=cont;i++)
{
Dp[1][i]+=sum[1][i];
}
for (i=2;i<=n;i++)
{
for (j=1;j<=cont;j++)
{
for (int k=1;k<=cont;k++)
{
if (Have[j]&have[k]) continue;
if (have[j]& (have[k]<<1)) continue;
if (have[j]& (have[k]>>1)) continue;
Dp[i][j]=max (Dp[i][j],dp[i-1][k]+sum[i][j]);
}
}
}
int max=0;
for (i=1;i<=n;i++)
for (j=1;j<=cont;j++)
if (Dp[i][j]>max) max=dp[i][j];
printf ("%d\n", Max);
GetChar ();
GetChar ();
}
return 0;
}

HDU 2167 State Compression 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.