zoukankan      html  css  js  c++  java
  • Pebbles 状态压缩&&dp 状态转移

    Problem Description
    You're given an unlimited number of pebbles to distribute across an N x N game board (N drawn from [3, 15]), where each square on the board contains some positive point value between 10 and 99, inclusive. A 6 x 6 board might look like this:

    The player distributes pebbles across the board so that:

    ?At most one pebble resides in any given square.
    ?No two pebbles are placed on adjacent squares. Two squares are considered adjacent if they are horizontal, vertical, or even diagonal neighbors. There's no board wrap, so 44 and 61 of row three aren't neighbors. Neither are 33 and 75 nor 55 and 92.

    The goal is to maximize the number of points claimed by your placement of pebbles.

    Write a program that reads in a sequence of boards from an input file and prints to stdout the maximum number of points attainable by an optimal pebble placement for each. 

     
    Input
    Each board is expressed as a series of lines, where each line is a space-delimited series of numbers. A blank line marks the end of each board (including the last one)

     
    Output
    then your program would print the maximum number of points one can get by optimally distributing pebbles while respecting the two rules, which would be this (each output should be printed on a single line and followed with a newline):

     
    Sample Input
    71 24 95 56 54
    85 50 74 94 28
    92 96 23 71 10
    23 61 31 30 46
    64 33 32 95 89

    78 78 11 55 20 11
    98 54 81 43 39 97
    12 15 79 99 58 10
    13 79 83 65 34 17
    85 59 61 12 58 97
    40 63 97 85 66 90

    33 49 78 79 30 16 34 88 54 39 26
    80 21 32 71 89 63 39 52 90 14 89
    49 66 33 19 45 61 31 29 84 98 58
    36 53 35 33 88 90 19 23 76 23 76
    77 27 25 42 70 36 35 91 17 79 43
    33 85 33 59 47 46 63 75 98 96 55
    75 88 10 57 85 71 34 10 59 84 45
    29 34 43 46 75 28 47 63 48 16 19
    62 57 91 85 89 70 80 30 19 38 14
    61 35 36 20 38 18 89 64 63 88 83
    45 46 89 53 83 59 48 45 87 98 21

    15 95 24 35 79 35 55 66 91 95 86 87
    94 15 84 42 88 83 64 50 22 99 13 32
    85 12 43 39 41 23 35 97 54 98 18 85
    84 61 77 96 49 38 75 95 16 71 22 14
    18 72 97 94 43 18 59 78 33 80 68 59
    26 94 78 87 78 92 59 83 26 88 91 91
    34 84 53 98 83 49 60 11 55 17 51 75
    29 80 14 79 15 18 94 39 69 24 93 41
    66 64 88 82 21 56 16 41 57 74 51 79
    49 15 59 21 37 27 78 41 38 82 19 62
    54 91 47 29 38 67 52 92 81 99 11 27
    31 62 32 97 42 93 43 79 88 44 54 48

     
    Sample Output
    572
    683
    2096
    2755
    ***************************************************************************************************************************
    状态压缩*&&dp  状态转移
    ***************************************************************************************************************************
      1 /*
      2 状态压缩,dp,
      3 */
      4 #include<iostream>
      5 #include<cstdio>
      6 #include<cstring>
      7 #include<string>
      8 #include<vector>
      9 using namespace std;
     10 int dp[20][1601];
     11 int s[25],state[1601],S,n;
     12 int d[20][20];
     13 vector<int>G[1601];
     14 int legal(int x)//判断状态是否合法
     15 {
     16     int it;
     17     for(it=1;it<15;it++)
     18     {
     19         if((x&s[it])&&(x&s[it-1]))return false;
     20 
     21     }
     22     return true;
     23 }
     24 void get_state()//存储一行时的合法状态
     25 {
     26     int st;
     27     S=0;
     28     for(st=0;st<=15;st++) s[st]=(1<<st);
     29     for(st=0;st<(1<<15);st++)
     30     {
     31         if(legal(st))
     32         {
     33             ++S;
     34             state[S]=st;
     35         }
     36     }
     37 }
     38 int link(int u,int v)//两个状态相邻时是否合适
     39 {
     40     int kt;
     41     if((u&s[0])&&((v&s[1])||v&s[0]))return false;
     42     if((u&s[14])&&((v&s[14])||(v&s[13])))return false;
     43     for(kt=1;kt<14;kt++)
     44     {
     45         if((u&s[kt])&&((v&s[kt])||(v&s[kt-1])||(v&s[kt+1]))) return false;
     46     }
     47     return true;
     48 }
     49 void  fuhe()//找合适的相邻状态
     50 {
     51     int it,jt;
     52     for(it=1;it<=S;it++)
     53      G[it].clear();
     54     for(it=1;it<=S;it++)
     55      for(jt=1;jt<=S;jt++)
     56      {
     57        if(link(state[it],state[jt]))
     58          G[it].push_back(jt);
     59      }
     60 }
     61 int sum(int low,int sta)//对每一种状态计算相应的和
     62 {
     63     int ans=0,it;
     64     if(sta>=s[n])return 0;
     65     for(it=0;it<n;it++)
     66      if(sta&s[it])ans+=d[low][it];
     67     return ans;
     68 }
     69 int main()
     70 {
     71     char str[1001],ch;
     72     get_state();
     73     fuhe();
     74     while(gets(str))
     75     {
     76       int i,j,k;
     77       n=(strlen(str)+1)/3;//判断出行列
     78       for(i=0;i<n;i++)
     79       {
     80          d[0][i]=(str[3*i]-'0')*10+(str[i*3+1]-'0');
     81       }
     82       for(i=1;i<n;i++)
     83        for(j=0;j<n;j++)
     84         scanf("%d",&d[i][j]);
     85       ch=getchar();
     86       ch=getchar();
     87       memset(dp,0,sizeof(dp));
     88       for(i=1;i<=S;i++)
     89       {
     90           if(state[i]>=s[n])break;
     91           dp[0][i]=sum(0,state[i]);//赋初值,以便以下的状态转移
     92       }
     93       for(i=1;i<n;i++)
     94        for(j=1;j<=S;j++)
     95        {
     96          if(state[j]>s[n])break;
     97          int temp=sum(i,state[j]);
     98          for(k=0;k<G[j].size();k++)
     99           if(dp[i][j]<dp[i-1][G[j][k]])//状态转移
    100              dp[i][j]=dp[i-1][G[j][k]];
    101           dp[i][j]+=temp;
    102        }
    103        int maxn=0;
    104        for(i=1;i<=S;i++)//再次遍历
    105         if(dp[n-1][i]>maxn)
    106           maxn=dp[n-1][i];
    107        printf("%d
    ",maxn);
    108 
    109     }
    110      return 0;
    111 }
    View Code
  • 相关阅读:
    关于ListView的注意点
    推荐一波 瀑布流的RecylceView
    RecycleView的简单应用
    Java Junit单元测试
    Java 2 个 List 集合数据求并、补集操作
    Java @Validated 遇到的大坑
    Java中的Validated验证
    使用一条sql语句查询多表的总数
    Java thymeleaf模板获取资源文件的内容
    Java热启动
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3416352.html
Copyright © 2011-2022 走看看