HDU 2167 Pebbles 状态压缩DP入门题(3)

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 5485 50 74 94 2892 96 23 71 1023 61 31 30 4664 33 32 95 8978 78 11 55 20 1198 54 81 43 39 9712 15 79 99 58 1013 79 83 65 34 1785 59 61 12 58 9740 63 97 85 66 9033 49 78 79 30 16 34 88 54 39 2680 21 32 71 89 63 39 52 90 14 8949 66 33 19 45 61 31 29 84 98 5836 53 35 33 88 90 19 23 76 23 7677 27 25 42 70 36 35 91 17 79 4333 85 33 59 47 46 63 75 98 96 5575 88 10 57 85 71 34 10 59 84 4529 34 43 46 75 28 47 63 48 16 1962 57 91 85 89 70 80 30 19 38 1461 35 36 20 38 18 89 64 63 88 8345 46 89 53 83 59 48 45 87 98 2115 95 24 35 79 35 55 66 91 95 86 8794 15 84 42 88 83 64 50 22 99 13 3285 12 43 39 41 23 35 97 54 98 18 8584 61 77 96 49 38 75 95 16 71 22 1418 72 97 94 43 18 59 78 33 80 68 5926 94 78 87 78 92 59 83 26 88 91 9134 84 53 98 83 49 60 11 55 17 51 7529 80 14 79 15 18 94 39 69 24 93 4166 64 88 82 21 56 16 41 57 74 51 7949 15 59 21 37 27 78 41 38 82 19 6254 91 47 29 38 67 52 92 81 99 11 2731 62 32 97 42 93 43 79 88 44 54 48
Sample Output
 
 
572
683
2096
2755
 

这个题和之前两个题差不多,就是输入有点费事,加个getchar()

在末尾就不数组越界,否则就会数组越界。

写这个基础题都快写出模板来了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=22;
int num[maxn][maxn],n,tot,prime[1<<16],dp[maxn][1<<16];
char ch[110];
int main(){
	int i,j,k;
	memset(ch,0,sizeof(ch));
	while(gets(ch)){
		int n=0,len=strlen(ch);
		memset(num,0,sizeof(num));
		for(i=0;i<len;i+=3){
			num[0][n++]=(ch[i]-'0')*10+(ch[i+1]-'0');
		}
		for(j=1;j<n;j++){
			gets(ch);
			k=0;
			for(i=0;i<len;i+=3)
				num[j][k++]=(ch[i]-'0')*10+(ch[i+1]-'0');
		}
		int end=1<<n;
		tot=0;
		memset(dp,0,sizeof(dp));
		for(i=0;i<end;i++){
			if((i&(i<<1))==0) prime[tot++]=i;
		}
		for(i=0;i<n;i++){
			for(j=0;j<tot;j++){
				int sum=0;
				for(k=n-1;k>=0;k--){
					if((1<<k)&prime[j])
						sum+=num[i][k];
				}
				dp[i][j]=sum;
				if(i==0) continue;
				for(k=0;k<tot;k++){
					if(prime[j]&prime[k]) continue;
					if(prime[j]&(prime[k]>>1)) continue;
					if(prime[j]&(prime[k]<<1)) continue;
					dp[i][j]=max(dp[i][j],dp[i-1][k]+sum);
				}
			}
		}
		int ans=0;
		for(i=0;i<tot;i++)
			ans=max(ans,dp[n-1][i]);
		printf("%d\n",ans);
		memset(ch,0,sizeof(ch));
		getchar();
	}
	return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值