题意:汉诺塔,给定一个初始局面,和一个目标局面,问你最少走多少步。
析:首先考虑最大的盘子,如果最大的盘子已经在相应的柱子上,那么就不用移动了,所以首先先找到要移动的最大盘子k,然后再移动最大的盘子,假设要把它从1移动到2,
那么我们先把1-k-1,移动到3号柱子上,这个局面称为参考局面,那么我们可以这样考虑,把初始状态和目标状态移动成目标局面,然后再移动k即可。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int a[65], b[65];
LL f(int *a, int k, int ff){
if(!k) return k;
if(a[k] == ff) return f(a, k-1, ff);
return f(a, k-1, 6-a[k]-ff) + (1LL<<k-1);
}
int main(){
int kase = 0;
while(scanf("%d", &n) == 1 && n){
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
for(int i = 1; i <= n; ++i) scanf("%d", b+i);
int k = n;
while(k && a[k] == b[k]) --k;
LL ans = 0;
if(k > 0) ans = f(a, k-1, 6-a[k]-b[k]) + f(b, k-1, 6-a[k]-b[k]) + 1;
printf("Case %d: %lld
", ++kase, ans);
}
return 0;
}