hdu 4277 USACO ORZ

27 篇文章 0 订阅
4 篇文章 0 订阅
Problem Description
Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.
I. M. Hei, the lead cow pasture architect, is in charge of creating a triangular pasture surrounded by nice white fence rails. She is supplied with N fence segments and must arrange them into a triangular pasture. Ms. Hei must use all the rails to create three sides of non-zero length. Calculating the number of different kinds of pastures, she can build that enclosed with all fence segments. 
Two pastures look different if at least one side of both pastures has different lengths, and each pasture should not be degeneration.
 

Input
The first line is an integer T(T<=15) indicating the number of test cases.
The first line of each test case contains an integer N. (1 <= N <= 15)
The next line contains N integers li indicating the length of each fence segment. (1 <= li <= 10000)
 

Output
For each test case, output one integer indicating the number of different pastures.
 

Sample Input
  
  
1 3 2 3 4
 

Sample Output
  
  
1
代码:
#include<iostream>
#include<set>
#include<cstdio>

using namespace std;

int e[20],n,t;
set<long long>s;

void dfs(int a,int b,int c,int d)
{
    if (d == n)
    {
        if (a<b||b<c||a<c||a==0||b==0||c==0)//设a>b >c
        {
            return ;
        }
        if(a<b+c)
        {
           long long x =a*1000000000000+b*1000000+c;//将x的不同值存放一种方案
           s.insert(x);
        }
        return;
    }
    dfs(a+e[d],b,c,d+1);
    dfs(a,b+e[d],c,d+1);
    dfs(a,b,c+e[d],d+1);
}

int main()
{
   int i;
   scanf("%d",&t);
   while (t--)
   {
    scanf("%d",&n);
    for (i =  0;i<n;i++)
    {
        scanf("%d",&e[i]);
    }
    s.clear();
    dfs(0,0,0,0);
    cout<<s.size()<<endl;
   }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!根据题目描述,我们需要帮助 Bessie 计算最小成本逃生计划的数量,并且要对结果进行取模操作。为了解决这个问题,我们可以使用动态规划的方法来求解。 首先,我们定义一个二维数组 dp,其中 dp[i][j] 表示在位置 (i, j) 时的最小成本逃生计划数量。我们可以使用动态规划的思想进行状态转移。 根据题目要求,所有的牛必须聚集在同一个单元格中才能逃生,因此我们可以将问题分解为两个子问题: 1. 选择一个单元格 (i, j),使得牛能够聚集在该单元格中。 2. 计算到达单元格 (i, j) 的最小成本逃生计划数量。 对于第一个子问题,假设我们选择了单元格 (i, j) 作为聚集点,那么该单元格一定有一个入口单元格 (x, y),其中 (x, y) 是 (i, j) 的上方或左方单元格。因此,我们可以通过遍历所有可能的入口单元格来计算第二个子问题的结果。 对于第二个子问题,我们可以使用动态规划的方法进行求解。假设我们已经计算了 dp[x][y] 的结果,那么到达单元格 (i, j) 的最小成本逃生计划数量可以通过以下方式计算: 1. 如果 (i, j) 的上方单元格 (x, y) 存在,那么 dp[i][j] += dp[x][y]。 2. 如果 (i, j) 的左方单元格 (x, y) 存在,那么 dp[i][j] += dp[x][y]。 最后,我们需要遍历所有的单元格,找到其中最小成本逃生计划数量的最小值,并将结果对 10^9+7 取模。 下面是对应的 C++ 代码实现: ```cpp #include <iostream> #include <vector> using namespace std; const int MOD = 1e9 + 7; int countEscapePlans(vector<vector<int>>& gates) { int N = gates.size(); int K = gates[0].size(); vector<vector<int>> dp(N, vector<int>(K, 0)); // 初始化边界条件 dp[0][0] = 1; // 动态规划求解 for (int i = 0; i < N; i++) { for (int j = 0; j < K; j++) { if (i > 0) { dp[i][j] += dp[i - 1][j]; dp[i][j] %= MOD; } if (j > 0) { dp[i][j] += dp[i][j - 1]; dp[i][j] %= MOD; } } } return dp[N - 1][K - 1]; } int main() { int N, K; cin >> N >> K; vector<vector<int>> gates(N, vector<int>(K, 0)); for (int i = 0; i < N; i++) { for (int j = 0; j < K; j++) { cin >> gates[i][j]; } } int result = countEscapePlans(gates); cout << result << endl; return 0; } ``` 这段代码首先读取了输入的矩阵大小,然后读取了矩阵中每个单元格的解锁成本。最后,将调用 `countEscapePlans` 函数计算最小成本逃生计划的数量,并输出结果。 希望这可以帮助到你!如果你有任何其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值