zoukankan      html  css  js  c++  java
  • DFS POJ 3087 Shuffle'm Up

    题目传送门

     1 /*
     2     题意:两块扑克牌按照顺序叠起来后,把下半部分给第一块,上半部给第二块,一直持续下去,直到叠成指定的样子
     3     DFS:直接模拟搜索,用map记录该字符串是否被搜过。读懂题目是关键。
     4 */
     5 /************************************************
     6 Author        :Running_Time
     7 Created Time  :2015-8-3 13:57:55
     8 File Name     :POJ_3087.cpp
     9 *************************************************/
    10 
    11 #include <cstdio>
    12 #include <algorithm>
    13 #include <iostream>
    14 #include <sstream>
    15 #include <cstring>
    16 #include <cmath>
    17 #include <string>
    18 #include <vector>
    19 #include <queue>
    20 #include <deque>
    21 #include <stack>
    22 #include <list>
    23 #include <map>
    24 #include <set>
    25 #include <bitset>
    26 #include <cstdlib>
    27 #include <ctime>
    28 using namespace std;
    29 
    30 #define lson l, mid, rt << 1
    31 #define rson mid + 1, r, rt << 1 | 1
    32 typedef long long ll;
    33 const int MAXN = 5e3 + 10;
    34 const int INF = 0x3f3f3f3f;
    35 const int MOD = 1e9 + 7;
    36 map<string, int> cnt;
    37 string str;
    38 bool vis[MAXN][MAXN];
    39 int res, ct;
    40 int n;
    41 
    42 void DFS(string s, string t, int dep)   {
    43     if (!cnt.count (s))  cnt[s] = ++ct;
    44     if (!cnt.count (t)) cnt[t] = ++ct;
    45     if (vis[cnt[s]][cnt[t]])    return ;
    46     vis[cnt[s]][cnt[t]] = true;
    47     string tmp = "";
    48     for (int i=0; i<n; ++i) {
    49         tmp += t[i];    tmp += s[i];
    50     }
    51     if (tmp == str) {
    52         if (res > dep)  res = dep;
    53         return ;
    54     }
    55     s = ""; t = "";
    56     for (int i=0; i<n; ++i) s += tmp[i];
    57     for (int i=n; i<2*n; ++i)   t += tmp[i];
    58     DFS (s, t, dep + 1);
    59 }
    60 
    61 int main(void)    {       //POJ 3087 Shuffle'm Up
    62     int T, cas = 0;  scanf ("%d", &T);
    63     while (T--) {
    64         scanf ("%d", &n);
    65         string s, t;
    66         cin >> s >> t;  cin >> str;
    67         res = INF;  ct = 0; cnt.clear ();   memset (vis, false, sizeof (vis));
    68         DFS (s, t, 1);
    69         printf ("%d %d
    ", ++cas, res == INF ? -1 : res);
    70     }
    71 
    72     return 0;
    73 }
    编译人生,运行世界!
  • 相关阅读:
    链表--判断一个链表是否为回文结构
    矩阵--“之”字形打印矩阵
    二叉树——平衡二叉树,二叉搜索树,完全二叉树
    链表--反转单向和双向链表
    codeforces 490C. Hacking Cypher 解题报告
    codeforces 490B.Queue 解题报告
    BestCoder19 1001.Alexandra and Prime Numbers(hdu 5108) 解题报告
    codeforces 488A. Giga Tower 解题报告
    codeforces 489C.Given Length and Sum of Digits... 解题报告
    codeforces 489B. BerSU Ball 解题报告
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4700340.html
Copyright © 2011-2022 走看看