Hanoi Topic Summary

Source: Internet
Author: User

Reference the summary of someone else's code

1. Four column Hanoi tower problem and N column Hanoi tower problem
Topic:

#include <cstdio>#include <algorithm>#include <cmath>using namespace STD;Doublef[ -];voidInit () {f[1] =1; f[2] =3; for(inti =3; I <= $; i++) {DoubleMax = F[i-2] *2+3; for(intj =1; J < I; J + +) max = min (max,2* F[i-j] +POW(2, j)-1);    F[i] = Max; }}intMain () {init ();intN while(scanf("%d", &n) = =1) {printf("%d\n", (int) f[n]); }return 0;}

2. Calculate the number of times a plate has been moved

Topic:

#include <cstdio>Long Longans[ $];intMain () {ans[1] =1; for(inti =2; I <= -; i++) Ans[i] = ans[i-1] *2;intTest, x, y;scanf("%d", &test); while(test--) {scanf("%d%d", &x, &y);printf("%i64d\n", Ans[x-y +1]); }return 0;}

3. No rule Hanoi

Topic:

#include <cstdio>intMain () {Long Longans[ to];intTest, N; ans[1] =3; for(inti =2; I <= -; i++) Ans[i] = ans[i-1] *3;scanf("%d", &test); while(test--) {scanf("%d", &n);printf("%i64d\n", Ans[n]); }return 0;}

4. Status Query

Topic

#include <cstdio>#include <cstring>#define MAXNintnum[3][maxn];bool Solve (intNint *one,int *two,int *three) {if(two[0] = = N)return 0;Else if(one[0] = = N)returnSolve (N-1, one +1, three, both);Else if(three[0] = = N)returnSolve (N-1, one, one, three +1);return 1;}intMain () {intTest, N; scanf"%d", &test); while(test--) {intNm; scanf"%d", &n); for(inti =0; I <3; i++) {scanf ("%d", &m); for(intj =0; J <m; J + +) scanf ("%d", &num[i][j]); num[i][m] = -1; }printf('%s\ n ', Solve (n,num[0],num[1],num[2]) ?"true":"false"); }return 0;}

5. Irregular movement
A.
Topic:

#include <cstdio>intMain () {Long Longans[ +];intN ans[1] =2; for(inti =2; I <= *; i++) Ans[i] = ans[i-1] *3+2; while(scanf("%d", &n) = =1) {printf("%i64d\n", Ans[n]); }return 0;}

B.
Topic:

#include <cstdio>#include <algorithm>using namespace STD;#define MAXNintMain () {Long LongF[MAXN], G[MAXN], D[MAXN], E[MAXN]; f[1] =2; for(inti =2; I <= -; i++) F[i] =3* F[i-1] +2; d[1] =1, d[2] =4; for(inti =3; I <= -; i++) D[i] = f[i-1] + f[i-2] +2+ D[i-2]; e[1] =1, e[2] =4; for(inti =3; I <= -; i++) E[i] = e[i-2] +2+ F[i-2] + f[i-1]; g[1] =2, g[2] =4, g[3] =Ten; for(inti =4; I <= -; i++) {G[i] =2* F[i-2] +2* F[i-3] +6+ D[i-3] + e[i-3];    G[i] = min (g[i],f[i]); }intTest, N;scanf("%d", &test); while(test--) {scanf("%d", &n);printf("%lld\n", G[n]); }return 0;}

Hanoi Topic Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.