hdu 1997 简单的递归

其实还是汉诺塔问题,给你一个状态,问是否是正确状态转移过程中的状态。当盘子数为n时,只需要看最大的盘子在哪根柱子上,三种情况分别递归判断即可~

/*
* hdu1997/win.cpp
* Created on: 2011-10-9
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
typedef priority_queue<int> PQ;

void init(PQ &pq) {
pq.push(-1); //插入一个负数防止队列为空
int temp, tempnum;
scanf("%d", &tempnum);
while (tempnum--) {
scanf("%d", &temp);
pq.push(temp);
}
}

bool judge(PQ &A, PQ &B, PQ &C, int n) {
if (n == 0) {
return true;
}
if (B.top() == n) {
return false;
} else if (A.top() == n) {
A.pop();
return judge(A, C, B, n - 1);
} else {
C.pop();
return judge(B, A, C, n - 1);
}
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int T, N;
scanf("%d", &T);
while (T--) {
PQ A, B, C;
scanf("%d", &N);
init(A);
init(B);
init(C);
printf("%s\n", judge(A, B, C, N) ? "true" : "false");
}
return 0;
}

原文地址:https://www.cnblogs.com/moonbay/p/2203932.html