Han Luota (recursive divide and rule)

The meaning of problems: there is a Sanskrit tower, tower has three seats A, B, C, A Nogang has a seat plate, the plate sizes, the lower large, on the small (FIG).

These plates move one from Block A, Block C, Block B, but the intermediate can be borrowed only allowed to move per a dish, and during the movement, the seat plate 3

Sub remain the market in the next, on the small cap.

Description simplified: the A-pillar of the n disks to move the C-pillar, B-pillar which can be borrowed.

Solution: Ignore the middle of the process as a whole to consider.

Status 0: A: n disks stacked in sequence. B: empty. C: empty.

Status 1: A: sequentially stacking a plate. B: n-1 a plate. C: empty.

Status 2: A: Empty. B: n-1 a plate. C: 1 a plate.

C can now be considered as empty, because the plate is larger than C dish other seat.

The status just as the state 0 is moved from Block B C Block.

 

Status 3: A: Empty. B: empty. C: n a plate.

Defined functions Hanoi (n, a, b, c) shows a n disks to move the seat from a seating c.

This operation can be divided into the following three treatment operations:

1, Hanoi (n-1, a, c, b) shows a n-1 a plate holder is moved from a base b.

2, move (a, c) shows a plate 1 c from a seat to move the seat.

3, Hanoi (n-1, b, a, c) of n-1 shows a plate holder moves from b c Block

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include<time.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 20191117
#define PI acos(-1)
using namespace std;
typedef long long ll ;

void solve(int n , char a , char b , char c)
{
    if(n == 0)
        return ;
    else{
        solve(n-1 , a , c , b);
        printf("%d:%c->%c\n" , n , a , c);
        solve(n-1 , b , a , c);
    }
}

int main()
{
    /*#ifdef ONLINE_JUDGE
    #else
        freopen("D:/c++/in.txt", "r", stdin);
        freopen("D:/c++/out.txt", "w", stdout);
    #endif*/
    int n ;
    char a  , b , c ;
    scanf("%d" , &n);
    cin >> a >> b >> c ;
    solve(n , a , b , c);

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/12015978.html