汉诺塔VII
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1191 Accepted Submission(s): 785
Problem Description
n个盘子的汉诺塔问题的最少移动次数是2^n-1,即在移动过程中会产生2^n个系列。由于发生错移产生的系列就增加了,这种错误是放错了柱子,并不会把大盘放到小盘上,即各柱子从下往上的大小仍保持如下关系 :
n=m+p+q
a1>a2>...>am
b1>b2>...>bp
c1>c2>...>cq
ai是A柱上的盘的盘号系列,bi是B柱上的盘的盘号系列, ci是C柱上的盘的盘号系列,最初目标是将A柱上的n个盘子移到C盘. 给出1个系列,判断它是否是在正确的移动中产生的系列.
例1:n=3
3
2
1
是正确的
例2:n=3
3
1
2
是不正确的。
注:对于例2如果目标是将A柱上的n个盘子移到B盘. 则是正确的.
n=m+p+q
a1>a2>...>am
b1>b2>...>bp
c1>c2>...>cq
ai是A柱上的盘的盘号系列,bi是B柱上的盘的盘号系列, ci是C柱上的盘的盘号系列,最初目标是将A柱上的n个盘子移到C盘. 给出1个系列,判断它是否是在正确的移动中产生的系列.
例1:n=3
3
2
1
是正确的
例2:n=3
3
1
2
是不正确的。
注:对于例2如果目标是将A柱上的n个盘子移到B盘. 则是正确的.
Input
包含多组数据,首先输入T,表示有T组数据.每组数据4行,第1行N是盘子的数目N<=64.
后3行如下
m a1 a2 ...am
p b1 b2 ...bp
q c1 c2 ...cq
N=m+p+q,0<=m<=N,0<=p<=N,0<=q<=N,
后3行如下
m a1 a2 ...am
p b1 b2 ...bp
q c1 c2 ...cq
N=m+p+q,0<=m<=N,0<=p<=N,0<=q<=N,
Output
对于每组数据,判断它是否是在正确的移动中产生的系列.正确输出true,否则false
Sample Input
6 3 1 3 1 2 1 1 3 1 3 1 1 1 2 6 3 6 5 4 1 1 2 3 2 6 3 6 5 4 2 3 2 1 1 3 1 3 1 2 1 1 20 2 20 17 2 19 18 16 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Sample Output
true false false false true true
写的很茫然 先留笔
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 #include <iostream> 2 #include <algorithm> 3 #include <queue> 4 #include <cstring> 5 #include <cstdio> 6 #include <vector> 7 #include <string> 8 #include <iterator> 9 #include <cmath> 10 #include <deque> 11 #include <stack> 12 #include <cctype> 13 using namespace std; 14 15 const int N = 70; 16 const int INF = 0xfffffff; 17 18 typedef long long ll; 19 typedef long double ld; 20 21 #define INFL 0x7fffffffffffffffLL 22 #define met(a, b) memset(a, b, sizeof(a)) 23 #define rep(c, a, b) for (int c = a; c < b; c++) 24 #define nre(c, a, b) for (int c = a; c > b; c--) 25 26 //h(n) = ( (4*n-2) / (n+1) ) * h(n-1); 27 bool hanoi (int n, int a[], int c[], int b[]); 28 29 int main () 30 { 31 int t; 32 cin >> t; 33 while (t--) 34 { 35 int a[N], b[N], c[N]; 36 int n, x1, x2, x3; cin >> n; 37 cin >> x1; rep (i, 0, x1) cin >> a[i]; 38 cin >> x2; rep (i, 0, x2) cin >> b[i]; 39 cin >> x3; rep (i, 0, x3) cin >> c[i]; 40 a[x1] = b[x2] = c[x3] = -1; 41 42 if (hanoi (n, a, c, b)) cout << "true" << endl; 43 else cout << "false" << endl; 44 } 45 return 0; 46 } 47 48 bool hanoi (int n, int a[], int c[], int b[]) 49 { 50 if (b[0] == n) return false; 51 else if (a[0] == n) return hanoi(n - 1, a + 1, b, c); 52 else if (c[0] == n) return hanoi(n - 1, b, c + 1, a); 53 return true; 54 }