ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2016-2017 ACM Central Region of Russia Quarterfinal Programming Contest B. Hanoi tower

2021-05-23 16:35:18  阅读:336  来源: 互联网

标签:Central Contest int Hanoi disks rod ans return tower


原题

Gym 101243B

It has become a good tradition to solve the “Hanoi tower” puzzle at programming contests in Rybinsk. We will review the rules briefly. There are 3 rods marked A, B, C. Initially, N disks of different diameter are placed on rod A: the smallest disk is at the top, the disks below it are
ordered by diameter, in increasing order. Rods B and C are empty yet. The goal is to move all disks from rod A to rod B, using rod C as auxiliary.

Hanoi tower

At each step you can take the uppermost disk from any rod and then put it either on
an empty rod or on a rod where the diameter of the uppermost disk is greater than the
diameter of the disk taken.
Many books on programming give a recursive solution of this task. Here is a sample
procedure in Pascal.

Procedure Hanoi (X, Y, Z: char; N: integer);
Begin
	If N>0 then
		Begin
			Hanoi (X, Z, Y, N-1);
			Writeln(‘Disk ’, N, ‘ from ‘, X, ‘ to ‘, Y);
			Hanoi (Z, Y, X, N-1)
		End
End;

Your task is to write a program that will determine the number of the move, after
which the disks for the first time after the start of the game are distributed equally
between the rods.

Input

The input file contains an integer N – the number of disks.

Output

The output file must contain an integer number – the number of the move, after
which the disks for the first time after the start of the game are distributed equally
between the rods.

Limitations

1 <= N <=300 , N mod 3 = 0

Examples

Input Output
3 6
6 9

题解

Hanoi tower通式为 2^n-1

刚开始没想通它和原来的Hanoi tower移动方式有什么联系,最终无法得出最优的移动方式。最后发现假设abc三个柱,只要先从a移动 i*(2/3)-1 个到b,再移动一个到c,最后将 i/3-1 个从b移动到c即可。

但是交了WA了,于是正经一个一个移动模拟,最后发现只有偶数个时这个式子才正确,奇数个时式子有一点点不一样(不知道为啥)。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>

using namespace std;

/* 用于test2记录移动情况 */
int k[4] = {0};
double ans1 = 0;
int ans2 = 0;

double hanoi(int n)
{
    return round(pow(2, n)-1);
}

int hanoiTest1(int n)    //得到hanoi的通式
{
    if (n < 1) return 0;
    int a = hanoiTest1(n-1);
    a ++;
    a += hanoiTest1(n-1);
    return a;
}

int hanoiTest2m(int a, int b)
{
    if (k[0] == k[1] && k[1] == k[2]) return 0;
    k[a]--; k[b]++;
    ans2++;
    return 1;
}

void hanoiTest2(int a, int b, int c, int n)
{   /*模拟移动 发现奇数时候不太对劲*/
    if (n <= 0) return;
    hanoiTest2(a, c, b, n-1);
    hanoiTest2m(a, b);
    hanoiTest2(c, b, a, n-1);
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    for (int i = 3; i <= 60; i+=6) {
        k[0] = i;k[1] = 0; k[2] = 0;
        ans2 = 0;
        hanoiTest2(0, 1, 2, i);
        /* 最初的想法 先从a移动i*(2/3)-1个到b 再移动一个到c 最后将i/3-1个从b移动到c */
        if (i % 2 == 0) ans1 = hanoi(i/3*2-1) + 1 + hanoi(i/3-1);
        /* 如果奇数需要移动第二步两次(规律如此 实际移动方法并不) */
        else ans1 = hanoi(i/3*2-1) + 1 + hanoi(i/3-1) * 2;
        printf("%d %.0lf %d\n", i, ans1 , ans2);
    }

    return 0;
}
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        try {
            /*
            FileInputStream inFile = new FileInputStream("input.txt");
            PrintStream outStream = new PrintStream(new FileOutputStream("output.txt"));

            System.setIn(inFile);
            System.setOut(outStream);
            */
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();

            BigInteger ans = hanoi(a / 3 * 2 - 1);
            ans = ans.add(BigInteger.ONE);
            ans = ans.add(hanoi(a / 3 - 1));
            if (a % 2 == 1) ans = ans.add(hanoi(a / 3 - 1));

            System.out.println(ans.toString());
            sc.close();
        } catch (Exception e) {
        }
    }

    public static BigInteger hanoi(int n) {
        BigInteger ans = new BigInteger("2");
        ans = ans.pow(n);
        return ans.subtract(BigInteger.ONE);
    }
}

标签:Central,Contest,int,Hanoi,disks,rod,ans,return,tower
来源: https://www.cnblogs.com/weilinfox/p/14801399.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有