NC15831. That’s One Hanoi-ed Teacher
描述
输入描述
Input consists of three lines, each line representing one peg of a Tower of Hanoi configuration. Each of these lines starts with a non-negative integer m indicating the number of disks on that peg, followed by m integers indicating the disks, starting with the disk on the bottom of the peg. The first line refers to the start peg and the last line refers to the destination peg. Disks are numbered consecutively starting at 1 with each number indicating the disk’s radius. All disk numbers used form a consecutive sequence. The maximum number of disks in any test case is 50.
输出描述
Display No if the given configuration is not in the optimal solution sequence; otherwise display the minimum number of remaining moves required to get to the final configuration.
示例1
输入:
1 3 2 2 1 0
输出:
4
C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 620K, 提交时间: 2020-03-03 14:29:14
#include <iostream> #include <cstdio> #include <algorithm> #include <string> #include <cstring> #include <cstdlib> #include <cmath> #include <stack> #include <queue> #include <set> #include <map> #include <vector> #include <ctime> #include <cctype> #include <bitset> #include <utility> #include <sstream> #include <complex> #include <iomanip> #define inf 0x3f3f3f3f typedef long long ll; using namespace std; ll p[60],jg; int n,k,x,a[60],ks=1,js=3; void init() { p[0]=1; for(int i=1; i<=50; i++) p[i]=p[i-1]*2; } int main() { init(); for(int i=1; i<=3; i++) { cin>>k; n+=k; for(int j=1; j<=k; j++) { cin>>x; a[x]=i; } } for(int i=n; i; i--) { if(a[i]==ks) js=6-ks-js; else if(a[i]==js) { jg+=p[i-1]; ks=6-ks-js; } else { cout<<"No"<<endl; return 0; } } cout<<p[n]-1-jg<<endl; return 0; }
C++ 解法, 执行用时: 4ms, 内存消耗: 484K, 提交时间: 2021-12-26 00:43:04
#include<bits/stdc++.h> #define ll long long using namespace std; ll ans; bool solve(int num,vector<int>&A,vector<int>&B,vector<int>&C) { if(!num) return true; if(A.size()&&A[0]==num) { ans+=(1LL<<((ll)num-1)); A.erase(A.begin()); return solve(num-1,A,C,B); } if(C.size()&&C[0]==num) { C.erase(C.begin()); return solve(num-1,B,A,C); } return false; } vector<int>A,B,C; int main() { int La,Lb,Lc,i,j,x; scanf("%d",&La);for(i=1;i<=La;i++) scanf("%d",&x),A.push_back(x); scanf("%d",&Lb);for(i=1;i<=Lb;i++) scanf("%d",&x),B.push_back(x); scanf("%d",&Lc);for(i=1;i<=Lc;i++) scanf("%d",&x),C.push_back(x); if(!solve(La+Lb+Lc,A,B,C)) puts("No"); else printf("%lld\n",ans); return 0; }