2020 cattle off the base algorithm winter training camp 3 - I Cow Tower of Hanoi [of memory].

Topic Portal


Title Description

Tower of Hanoi is a classic problem, according to legend Temple in ancient India, there is a condition known as Tower of Hanoi (Hanoi) game. The game apparatus is in a copper plate, with three rods (numbered A, B, C), A bottom-up lever, are placed in descending order of n gold plate. Goal of the game: A bar of gold plate to move all rods C, and still maintain the original order folded. Operation rules: a plate can only be moved, and the three bars is always maintained during the movement of the tape in the next, in the small cap, the dish can be placed during operation A, B, C any one bar.

Tower of Hanoi and its derivatives tend to use recursion to solve the problem, but also to learn and understand recursion very good teacher.

Pseudo code which

Function Hanoi(n,a,b,c)
    if n==1 then
        print(a+'->'+c)
    else
        Hanoi(n-1,a,c,b)
        print(a+'->'+c)
        Hanoi(n-1,b,a,c)
    end if
end Function 

Cow quickly understand the meaning of the code and write a program to solve the Tower of Hanoi, he now wanted to study the law of the Tower of Hanoi.
Please statistics following information: A-> B, A-> C , B-> A, B-> C, C-> A, C-> B number, and the total number of steps all moving.


Enter a description:

Only one line, enter a positive integer n ( 1 n 60 ) n (1 \ leq n \ leq 60) represents the Tower of Hanoi layers.


Output Description:

First output line 6
A-> B: XX
A-> C: XX
the B-> A: XX
the B-> C: XX
the C-> A: XX
the C-> B: XX
represent the number of each movement situation of
the last output line
sUM: XX
represents the sum of all movement of the.


Entry

3


Export

A->B:1
A->C:3
B->A:1
B->C:1
C->A:0
C->B:1
SUM:7


Explanation

Moving sequence algorithm in pseudocode as follows:
A-> C
A-> B
the C-> B
A-> C
the B-> A
the B-> C
A-> C
Statistics:
A-> B 1 occurrence
A-> C 3 appears
B-> C 1 occurrence
B-> A 1 occurrence
C-> B 1 occurrence
total 7


answer

  • Very familiar with the topic, you can recursively + of memory

AC-Code

#include<bits/stdc++.h>
using namespace std;
struct node {
	long long data[6];
	node() {
		memset(data, 0, sizeof(data));
	}
	//A->B      0
	//A->C      1
	//B->A      2
	//B->C      3
	//C->A      4
	//C->B      5
};

node operator + (const node& A, const node& B) {
	node C;
	for (int i = 0; i < 6; ++i)
		C.data[i] = A.data[i] + B.data[i];
	return C;
}

void moveto(int x, int y, node& temp) {
	if (x == 0 && y == 1)		++temp.data[0];
	else if (x == 0 && y == 2)	++temp.data[1];
	else if (x == 1 && y == 0)	++temp.data[2];
	else if (x == 1 && y == 2)	++temp.data[3];
	else if (x == 2 && y == 0)	++temp.data[4];
	else if (x == 2 && y == 1)	++temp.data[5];
}

node dp[3][3][3][105];
bool vis[3][3][3][105];

node hanoi(int a, int b, int c, int n) {
	if (vis[a][b][c][n])	return dp[a][b][c][n];
	if (n == 1) {
		moveto(a, c, dp[a][b][c][n]);
		vis[a][b][c][n] = true;
		return dp[a][b][c][n];
	}
	node temp;
	temp += hanoi(a, c, b, n - 1);
	moveto(a, c, temp);
	temp += hanoi(b, a, c, n - 1);
	vis[a][b][c][n] = true;
	return dp[a][b][c][n] = temp;
}

int main() {
	int n;	cin >> n;
	node ans = hanoi(0, 1, 2, n);
	printf("A->B:%lld\n", ans.data[0]);
	printf("A->C:%lld\n", ans.data[1]);
	printf("B->A:%lld\n", ans.data[2]);
	printf("B->C:%lld\n", ans.data[3]);
	printf("C->A:%lld\n", ans.data[4]);
	printf("C->B:%lld\n", ans.data[5]);
	printf("SUM:%lld\n", (1LL << n) - 1);
}
Published 162 original articles · won praise 99 · views 10000 +

Guess you like

Origin blog.csdn.net/Q_1849805767/article/details/104229728