zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    POJ1112 Team Them Up!
    WebSocket相关介绍
    长轮询 & 短轮询
    图片上传 & 预览
    sessionStorage & localStorage & cookie & session
    进程 & 线程
    https介绍与讲解
    http请求
    TCP/IP的连接与断开
    浏览器缓存
  • 原文地址:https://www.cnblogs.com/moonbay/p/2203932.html
Copyright © 2011-2022 走看看