1. 程式人生 > >UVa 10118 Free Candies (記憶化搜尋+狀態壓縮)

UVa 10118 Free Candies (記憶化搜尋+狀態壓縮)

題目連結:https://cn.vjudge.net/problem/UVA-10118

思路:設dp[pa][pb][pc][pd]四堆糖分別取到這四堆的第pa、pb、pc、pd顆時最多的pair數, 用一個二進位制串記錄籃中糖的狀態(即哪些糖有那些沒有)。狀態轉移一共四個,即分別從這四堆中拿出一顆糖放到籃子裡,轉移到放之後的狀態。詳見程式碼

#include<cstdio>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstring>
#include<sstream>
#include<iostream>
using namespace std;
const int maxn = 42;

int a[maxn], b[maxn], c[maxn], d[maxn]; //four piles
int DP[maxn][maxn][maxn][maxn];
int n;

inline bool Push(int pre, int m, int &st) // if she can take a pair of candies;
{
    bool ok = false;
    if(pre & (1<<m)) pre ^= (1<<m), ok = true;
    else pre |= (1<<m);
    st = pre;
    return ok;
}

int dp(int pa, int pb, int pc, int pd, int st, int num) // st : sugars that are in the basket
{
    int &ans = DP[pa][pb][pc][pd];
    if(ans >= 0) return ans;
    ans = 0;
    if(num < 5) {
       if(pa < n) {
          int st0;
          if(Push(st, a[pa], st0)) ans = max(ans, 1+dp(pa+1, pb, pc, pd, st0, num-1));
          else ans = max(ans, dp(pa+1, pb, pc, pd, st0, num+1));
       }
       if(pb < n) {
          int st0;
          if(Push(st, b[pb], st0)) ans = max(ans, 1+dp(pa, pb+1, pc, pd, st0, num-1));
          else ans = max(ans, dp(pa, pb+1, pc, pd, st0, num+1));
       }
       if(pc < n) {
          int st0;
          if(Push(st, c[pc], st0)) ans = max(ans, 1+dp(pa, pb, pc+1, pd, st0, num-1));
          else ans = max(ans, dp(pa, pb, pc+1, pd, st0, num+1));
       }
       if(pd < n) {
          int st0;
          if(Push(st, d[pd], st0)) ans = max(ans, 1+dp(pa, pb, pc, pd+1, st0, num-1));
          else ans = max(ans, dp(pa, pb, pc, pd+1, st0, num+1));
       }
    }
    return ans;
}

int main()
{
   while(scanf("%d", &n) && n) {
      memset(DP, -1, sizeof DP);
      for(int i = 0; i < n; i++)
         scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
      printf("%d\n", dp(0, 0, 0, 0, 0, 0));
   }
}



相關推薦

UVa 10118 Free Candies 記憶搜尋+狀態壓縮

題目連結:https://cn.vjudge.net/problem/UVA-10118 思路:設dp[pa][pb][pc][pd]四堆糖分別取到這四堆的第pa、pb、pc、pd顆時最多的pair數, 用一個二進位制串記錄籃中糖的狀態(即哪些糖有那些沒有)。狀態轉移一共四

Free Candies記憶搜尋

給出4堆糖,往容量為5籃子裡裝任意一個在堆頂的糖,籃子裡有相同顏色的就拿走,問最多能拿走幾次。 定義四維陣列記憶搜尋。 #include<cstdio> #include<cstri

cf 777c Alyona and Spreadsheet記憶搜尋+遞推

  這題題目說了(1 ≤ n·m ≤ 100 000),那麼n和m就不可能同時取到最大1e5,而且鄰接矩陣也開不了1e5*1e5這麼大,因此不能提前開mp陣列,要在輸入了n和m之後,根據具體的n和m來開mp陣列。   h[i]記錄的是第i行最多能到第幾行,l[j]表示第j列

uva 10118 Free Candies

代碼 ans mem 繼續 con algo str 並且 IE 題意: 有4堆糖,每堆有n個,每次從某一堆的堆頂拿一個放進籃子裏,如果籃子裏有2個顏色相同的糖果,那麽就可以放進袋子裏。 當籃子裏有5個糖果並且沒有相同顏色的糖果時,這個時候就不能再拿了。 問最多可以拿多少對

hdu-1142記憶搜尋+dij

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1142 思路:1、不是求最短路徑,而是求如果兩個點A,B直接相連,且A到終點的距離大於B到終點的距離,求這樣A,B之間的通路的個數。 2、以終點為起點來進行dij 3、記憶化搜尋是,用p[i]陣列記錄符合條件

Codeforces Round #197 (Div. 2): C. Xenia and Weights記憶搜尋

  題意: 先輸入一個長度為10的01串,第i個數字為1表示你有重量為i的砝碼無數個,第i個數字為0表示你沒有重量為i的砝碼,你需要按照以下規則在一個一開始平衡的天平上放上m個砝碼 第1個砝碼放在天平左邊,第2個砝碼放在天平的右邊,第3個砝碼放在天平左邊……依次

【ZOJ1107】FatMouse and Cheese記憶搜尋

題目連結 FatMouse and Cheese Time Limit: 10 Seconds      Memory Limit: 32768 KB FatMouse has stored some c

洛谷3257 [JLOI2014]天天酷跑DP記憶搜尋

題目 每次往上跳或往下掉或持平……(具體看題目吧),使路徑上的權值和最大。 題解 記憶化搜尋 設f[i][j][t]表示從出發點到(x,y)這個位置(不含這個位置,即減掉ma[x][y])還可以跳t次的最大權值和。 轉移方程 ,其中w表示這一路上的點權和。 下面說說前輩繞的彎路,

Light OJ 1006 Hex-a-bonacci記憶搜尋優化

Given a code (not optimized), and necessary inputs, you have to find the output of the code for the inputs. The code is as follows: int a, 

HDU 1078 FatMouse and Cheese 記憶搜尋

FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 &l

HDU 1978 How many ways記憶搜尋

   How many ways   Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other

【HDU - 1078】FatMouse and Cheese 記憶搜尋dp

題幹: FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) wh

【牛客 - 301哈爾濱理工大學軟體與微電子學院第八屆程式設計競賽同步賽高年級】小樂樂下象棋記憶搜尋dp,dfs

題幹:   小樂樂一天天就知道玩,這一天又想玩象棋。 我們都知道馬走日。 現在給定一個棋盤,大小是n*m,把棋盤放在第一象限,棋盤的左下角是(0,0),右上角是(n - 1, m - 1); 小樂樂想知道,一個馬從左下角(0, 0)開始,走了k步之後,剛好走到右上角(n - 1,

抵制克蘇恩記憶搜尋

題目描述 小Q同學現在沉迷爐石傳說不能自拔。他發現一張名為克蘇恩的牌很不公平。如果你不玩爐石傳說,不必擔心,小Q同學會告訴你所有相關的細節。爐石傳說是這樣的一個遊戲,每個玩家擁有一個 30 點血量的英雄,並且可以用牌召喚至多 7 個隨從幫助玩家攻擊對手,其中每個隨從也擁有自己的血量和攻擊力。小Q同學有很

3822 期望DP記憶搜尋

這道題的難點就在於狀態的設計,如果你只想了一會就來看這篇題解,我建議你多方面想想狀態的設計之後,若還是沒有思路再來看題解。 主要思路: 第一眼看過去是不是很多人都想如何存棋盤的狀態,但是我們並不需要每一行,每一列是怎麼放得,只需要知道有幾行放了,有幾列放了就可以了。同

UVA 10118-Free Candies

題目連結 題目大意:       有4堆糖果,每一堆有n個糖果,有一個籃子,這個籃子的最大容量為5塊糖,每一次從這四堆中選擇一堆,然後將最上面的糖果拿下來,放到籃子裡面去,如果有相同兩塊糖的就把這;兩塊糖拿出來,如果達到了5塊糖,並且這5塊糖是兩兩不相同的就結束

UAV -10285 Longest Run on a Snowboard 記憶搜尋

題目連結 題目大意:     從矩陣中任意一個點出發,只要它上下左右的任意一個比它的數小,它就能向那個小的數字走一步,問在這個圖中走的最大的步數 題目分析:     如果用暴力搜尋的話可能會很麻煩,很費事,所以很典型的記憶化搜尋 Code:

3652 B-number 數位DP記憶搜尋

B-number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9200    Accepted Submis

1036C Classy Numbers 數位DP記憶搜尋

題意:在n到m區間內,有多少個數滿足每位數字非零的不超過3個(前導零無關)。 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib>

Codeforces Round #459 (Div. 2):D. MADMAX記憶搜尋+博弈論

D. MADMAX time limit per test1 second memory limit per test256 megabytes Problem Description As we all know, Max is the best