UVA10118Free Candies

一开始看数据范围不大想直接暴力搜索,仔细考虑搜索的状态会有很多重复,搜索量仍然很大。

这就是传说中的记忆化搜索。。。number数组表示每一列取了多少个数,ans每一列取得相应数字个数时的最优解。

第九章前面的代码用了引用,在记忆化搜索里面很方便。这个题还要注意搜索的时候要回溯,搜不下去了要尝试另一种。

 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <bits/stdc++.h>
 4 #include <cstdio>
 5
 6 using namespace std;
 7
 8 typedef long long ll;
 9 const int maxn = 40 + 10;
10 int G[maxn][4];
11 int ans[maxn][maxn][maxn][maxn];
12 bool book[maxn*maxn];
13 int number[4];
14 int n;
15
16 int dfs(int cnt,int a,int b,int c,int d)
17 {
18     number[0] = a, number[1] = b, number[2] = c, number[3] = d;
19     int & num = ans[a][b][c][d];
20     if(num != 0 || cnt >= 5)return num;
21     int sum = 0;
22     for(int i = 0 ; i < 4 ; i ++)
23     {
24         if(number[i] >= n)continue;
25         int v = G[number[i]][i];
26         number[i]++;
27         if(!book[v])
28         {
29             book[v] = true;
30             sum = max(sum,dfs(cnt-1,number[0],number[1],number[2],number[3])+1);
31             book[v] =false;
32             number[i] --;
33         }
34         else
35         {
36             book[v] = false;
37             sum = max(sum,dfs(cnt+1,number[0],number[1],number[2],number[3]));
38             book[v] = true;
39             number[i]--;
40         }
41     }
42     num += sum;
43     return num;
44 }
45 int main(int argc, char const *argv[])
46 {
47     while (cin >> n && n)
48     {
49         memset(ans, 0, sizeof(ans));
50         memset(book,true,sizeof(book));
51         for (int i = 0 ; i < n ; i++)
52             for (int j = 0 ; j < 4 ; j++)
53                 cin >> G[i][j];
54         cout << dfs(0,0,0,0,0) << endl;
55     }
56     return 0;
57 }
时间: 2024-08-06 20:08:47

UVA10118Free Candies的相关文章

poj 3159 Candies

题目链接:http://poj.org/problem?id=3159 Candies Time Limit: 1500MS   Memory Limit: 131072K Total Submissions: 22516   Accepted: 6047 Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought th

LeetCode解题思路:575. Distribute Candies

Given an integer array with even length, where different numbers in this array represent different kinds of candies. Each number means one candy of the corresponding kind. You need to distribute these candies equally in number to brother and sister.

hzau 1207 Candies

1207: Candies Time Limit: 2 Sec  Memory Limit: 1280 MBSubmit: 223  Solved: 31[Submit][Status][Web Board] Description Xiao Ming likes those N candies he collects very much. There are two kinds of candies, A and B. One day, Xiao Ming puts his candies i

poj3159 Candies(差分约束)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Candies Time Limit: 1500MS   Memory Limit: 131072K Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’

poj 2886 Who Gets the Most Candies? (线段树单点更新应用)

poj 2886 Who Gets the Most Candies? Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise order. Each of them has a card with a non-zero integer on it in his/her hand. The game starts from t

poj---(2886)Who Gets the Most Candies?(线段树+数论)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10373   Accepted: 3224 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise

poj 2886 Who Gets the Most Candies?(线段树+约瑟夫环+反素数)

Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 9934   Accepted: 3050 Case Time Limit: 2000MS Description N children are sitting in a circle to play a game. The children are numbered from 1 to N in clockwise o

Zepto Code Rush 2014——Dungeons and Candies

题目链接 题意: k个点,每个点都是一个n * m的char型矩阵.对与每个点,权值为n * m或者找到一个之前的点,取两个矩阵对应位置不同的字符个数乘以w.找到一个序列,使得所有点的权值和最小 分析: 首先,这个图是一个无向图.求权值和最小,每个权值对应的是一条边,且每个点只能有一个权值即一条边,一个k个边,和生成树很像,但是需要证明不能有环形.不妨假设现在有三个点,每个点的最小边成环,这时候是不能找到一个序列使得每个点都取到它的最小边值的,所以,k个点k个边不能有环且边值和最小,就是最小生成

poj3159 Candies

POJ - 3159 Candies 题目大意: 给n个小孩发糖,有m个条件 每个条件给出a,b,c,使得v[b]-v[a]<=c 求v[n]-v[1]的最大值 Sample Input 2 2 1 2 5 2 1 4 Sample Output 5 /* 差分约束裸题. 差分约束的基本模式为,给出若干个形如a-b<=c的不等式,问你x-y的最大值是多少 那么对每个不等式a-b<=c,连一条由b指向a权值为c的有向边,所有的不等式构成一个图 那么答案就是y到x的最短路 */ #inclu