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\n", ++cas, res == INF ? -1 : res); 70 } 71 72 return 0; 73 }
DFS POJ 3087 Shuffle'm Up
时间: 2024-12-31 03:58:04