Pebbles(hdu 2167)

题意:给定一个N*N的方格,让你在里面取出一些数使其和最大,要求每一个数不能与其相邻的8个数同时取出。

/*
    和hdu1565差不多,只不过在更新的时候考虑斜上方的格子对该格子的影响。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#define N 16
using namespace std;
int dp[N][1<<N],a[N][N],n,m;
char ch[N*4];
void dfs(int s,int ss,int y,int x,int sum){
    if(y>n-1){
        dp[x+1][ss]=max(dp[x+1][ss],dp[x][s]+sum);
        return;
    }
    int flag=0;
    if(s&(1<<y)) flag=1;
    if(y>0&&(s&(1<<y-1))) flag=1;
    if(y<n-1&&(s&(1<<y+1))) flag=1;
    if(!flag) dfs(s,ss|(1<<y),y+2,x,sum+a[x+1][y]);
    dfs(s,ss,y+1,x,sum);
}
void work(){
    for(int i=2;i<=n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&a[i][j]);
    for(int i=0;i<n;i++)
        for(int j=0;j<(1<<n);j++)
            dfs(j,0,0,i,0);
    int ans=0;
    for(int i=0;i<(1<<n);i++)
        ans=max(ans,dp[n][i]);
    printf("%d\n",ans);
}
int main(){
    while(gets(ch)){
        memset(dp,0,sizeof(dp));
        int len=strlen(ch);n=0;
        if(!len) continue;
        for(int i=0;i<len;i+=3)
            a[1][n++]=(ch[i]-'0')*10+ch[i+1]-'0';
        work();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/harden/p/6560517.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lodash是一个JavaScript工具库,提供了很多常用的工具函,可以方便地进行据处理、函式编程等操作。下面是一些常用的Lodash方法及其示例: 1. _.chunk(array, [size]): 把一个组拆分成多个数组,每个数组包含指定量的元素。 ``` _.chunk(['a', 'b', 'c', 'd'], 2); // => [['a', 'b'], ['c', 'd']] _.chunk(['a', 'b', 'c', 'd'], 3); // => [['a', 'b', 'c'], ['d']] ``` 2. _.compact(array): 去掉组中的假值(false、null、0、""、undefined 和 NaN)。 ``` _.compact([0, 1, false, 2, '', 3]); // => [1, 2, 3] ``` 3. _.concat(array, [values]): 连接组与其他值(可以是值或组),生成一个新的组。 ``` var array = [1]; var other = _.concat(array, 2, [3], [[4]]); console.log(other); // => [1, 2, 3, [4]] console.log(array); // => [1] ``` 4. _.difference(array, [values]): 返回在第一个组中但不在其他组中的元素。 ``` _.difference([2, 1], [2, 3]); // => [1] ``` 5. _.drop(array, [n=1]): 去掉组前面的n个元素,返回剩余的组。 ``` _.drop([1, 2, 3]); // => [2, 3] _.drop([1, 2, 3], 2); // => [3] _.drop([1, 2, 3], 5); // => [] _.drop([1, 2, 3], 0); // => [1, 2, 3] ``` 6. _.find(array, [predicate=_.identity], [fromIndex=0]): 遍历组,找到第一个符合条件的元素并返回。 ``` var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.find(users, function(o) { return o.age < 40; }); // => { 'user': 'barney', 'age': 36, 'active': true } _.find(users, { 'age': 1, 'active': true }); // => { 'user': 'pebbles', 'age': 1, 'active': true } _.find(users, ['active', false]); // => { 'user': 'fred', 'age': 40, 'active': false } _.find(users, 'active'); // => { 'user': 'barney', 'age': 36, 'active': true } ``` 7. _.flatten(array): 把一个多维组展开成一维组。 ``` _.flatten([1, [2, [3, [4]], 5]]); // => [1, 2, [3, [4]], 5] _.flattenDeep([1, [2, [3, [4]], 5]]); // => [1, 2, 3, 4, 5] ``` 8. _.groupBy(array, [iteratee=_.identity]): 把一个组按照指定的条件进行分组,返回一个对象,对象的键为分组的条件,值为分组后的组。 ``` var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; _.groupBy(users, 'active'); // => { 'true': [{ 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'pebbles', 'age': 1, 'active': true }], 'false': [{ 'user': 'fred', 'age': 40, 'active': false }] } _.groupBy(users, function(o) { return o.age; }); // => { '1': [{ 'user': 'pebbles', 'age': 1, 'active': true }], '36': [{ 'user': 'barney', 'age': 36, 'active': true }], '40': [{ 'user': 'fred', 'age': 40, 'active': false }] } ``` 9. _.intersection([arrays]): 返回多个数组中都包含的元素。 ``` _.intersection([2, 1], [2, 3]); // => [2] _.intersection([2, 1], [2, 3], [1, 2]); // => [2] ``` 10. _.join(array, [separator=',']): 把组中的所有元素用指定的分隔符连接成一个字符串。 ``` _.join(['a', 'b', 'c'], '~'); // => 'a~b~c' ``` 11. _.reverse(array): 把一个组倒序排列。 ``` _.reverse([1, 2, 3]); // => [3, 2, 1] ``` 12. _.slice(array, [start=0], [end=array.length]): 返回一个从start开始、到end结束(不包含end)的子组。 ``` _.slice([1, 2, 3], 0, 2); // => [1, 2] _.slice([1, 2, 3], 1); // => [2, 3] _.slice([1, 2, 3], -2); // => [2, 3] ``` 以上是一些常用的Lodash方法及其示例,Lodash还有很多其他的方法,可以查看官方文档来了解更多。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值