题目链接:Shuffle'm Up

除了英文题有点恶心。发现模拟 + bfs 就可以过的时候,就是水了。

一个bug 就是filp函数得到string s12失败了。恩。据大腿告知,string 并不指定字符串的初始化长度是0,也就是说我每个元素的赋值是没有意义的。或者。string 是引用型数据类型,所以名字是一个指针吧。然后str.resize()函数愉快的解决了这个问题。

还有就是对题意。字符串从左到右是从下到上。我是懵了一下的。

题意思路见代码吧。

 1 /*
 2   题目很长。也没有看。百度到题意。给出两个长度相同的牌。洗牌方式见图得到s12。然后以最下面的相应长度为s1,上面的为s2,然后,重新洗牌。
 3   问s12 是否有可能得到s12 和目标牌一样。步数是多少。
 4   开始是没有思路。然后模拟。然后。bfs。把第一个s12当做初始状态,然后开始进队列。用map标记是否出现过
 5   样例:
 6   下 上
 7   AHAH
 8   HAHA
 9   HHAAAAHH
10 
11   AHAH
12   HAHA
13   HAAHHAAH
14 
15   HAAH
16   HAAH
17   HHAAAAHH
18  */
19 
20 #include <string.h>
21 #include <iostream>
22 #include <queue>
23 #include <map>
24 #include <string>
25 #define maxn 1000000
26 using namespace std;
27 
28 queue<string>que;
29 map<string, int>mp;
30 string s1, s2, s12;
31 string temp, now;
32 
33 int ans;
34 int len;
35 
36 void filp(string s1, string s2) {
37     string tempans;
38     //cout << "======\n";
39     for (int i=0, j=0; i<len; i++, j+=2) {
40         temp[j] = s2[i];
41     }
42     //cout << "------\n";
43     for (int i=0, j=1; i<len; i++, j+=2) {
44         temp[j] = s1[i];
45     }
46     temp[2*len] = '\0';
47 }
48 
49 void dfs() {
50     while(!que.empty()) {
51         now = que.front();
52         que.pop();
53        // cout << now << "====" << endl;
54         if (now == s12) {
55             ans = mp[now];
56             return;
57         }
58         for (int i=0; i<len; ++i) {
59             s1[i] = now[i];
60         }
61         for (int i=0; i<len; ++i) {
62             s2[i] = now[i+len];
63         }
64         filp(s1, s2);
65         if (!mp[temp]) {
66             que.push(temp);
67             mp[temp] = mp[now] + 1;
68         }
69     }
70     return;
71 }
72 
73 
74 int main() {
75      int t;
76      cin >> t;
77      int cnt = 0;
78      while(t--) {
79        mp.clear();
80        ans = maxn;
81        cin >> len >> s1 >> s2 >> s12;
82        temp.resize(2*len);
83        filp(s1, s2);
84 
85        mp[temp]++;
86        while(!que.empty()) {
87         que.pop();
88        }
89        que.push(temp);
90        dfs();
91        if (ans == maxn) {
92         cout << ++cnt << " " << -1 << endl;
93        }
94        else cout << ++cnt << " " << ans << endl;
95        dfs();
96      }
97      return 0;
98 }
View Code

 

posted on 2016-01-27 15:34  小小八  阅读(225)  评论(0编辑  收藏  举报