UVA 10651

  题目大意是给12个空槽,初始时有以下空槽中有石头,可以把两个连续的石头移动到相邻的一个空槽,并移调中间的石头,求最少剩余几块石头。

  用一个整数表示状态,因公也就不到10000种状态,数组记录即可,状态转移用位操作即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define lowbit(x) x&(-x)
using namespace std;
int dp[10000];
int dfs(int st)
{
	if(dp[st]!=-1) return dp[st];
	int op=7,r1=3,r2=6,tt=st;
	dp[st]=0;
	while(tt) {dp[st]++;tt^=(lowbit(tt));}
	while(op<=3584){
		int temp=st&op;// 获取局部状态
		if(temp==r1||temp==r2) dp[st]=min(dp[st],dfs(st^op));// 若可以消去石头,则尝试更新
		op<<=1;r1<<=1;r2<<=1;
	}
	return dp[st];
}
int main()
{
	int n;
	cin>>n;getchar();
	while(n--){
		memset(dp,-1,sizeof(dp));
		int st=0;
		char t;
		for(int i=0;(t=getchar())!='\n';i++){
			st|=((t=='-'?0:1)<<i);
		}
		dfs(st);
		cout<<dp[st]<<endl;
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值