模拟洗牌的过程,合并两堆拍的方式:使先取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; }
如有不当之处欢迎指出!