POJ - 3087 模拟 [kuangbin带你飞]专题一

    模拟洗牌的过程,合并两堆拍的方式:使先取s2,再取s1;分离成两堆的方式:下面C张放到s1,上面C张到s2。当前牌型与第一次相同时,说明不能搜索到答案。

AC代码

#include<cstdio>
#include<cstring>

const int maxn = 100 + 5;
char s1[maxn], s2[maxn], s3[maxn * 2], s4[maxn * 2], vis[maxn * 2];
int c;

void deal(char *a){
	int cur = 0;
	for(int i = 0; i < c; ++i){
		a[cur++] = s2[i];
		a[cur++] = s1[i];
	}
	a[cur] = '\0';
}

void deal(){
	for(int i = 0; i < c; ++i){
		s1[i] = s3[i];
		s2[i] = s3[i + c];
	}
}

int dfs(int cnt){
	deal(s3);
	if(!memcmp(s3, s4, sizeof(s4))) return cnt;
	if(cnt > 1 && !memcmp(s3, vis, sizeof(vis))) return -1;
	deal();
	return dfs(cnt + 1);
}

int main(){
	int T;
	scanf("%d", &T);
	int kase = 1;
	while(T--) {
		scanf("%d", &c);
		scanf("%s%s%s", s1, s2, s4); //s4是目标 
		printf("%d ", kase++);
		deal(vis);
		printf("%d\n", dfs(1));
	}
	return 0;
}

如有不当之处欢迎指出!

转载于:https://www.cnblogs.com/flyawayl/p/8305439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值