#include<iostream>
#define N 100
using namespace std;
struct stack{
int stackarray[ N] ;
int sp;
} ;
stack s;
int push( stack * ,int ) ;
int pop( stack * ) ;
void printstack( stack * ) ;
int isempty( stack * ) ;
int isfull( stack * ) ;
void move( int ,char ,char ,char ) ;
int main( ) {
s.sp = - 1 ;
move( 3 ,'A' ,'B' ,'C' ) ;
system ( "pause" ) ;
return 0 ;
}
void move( int n,char source,char temp,char destination) {
L0:
if ( n) {
push( & s,1 ) ; push( & s,n) ; push( & s,source) ; push( & s,temp) ; push( & s,destination) ;
n-- ; swap( temp,destination) ;
goto L0;
L1: cout << "Move disk" << n<< " from " << source<< " to " << destination<< endl;
push( & s,2 ) ; push( & s,n) ; push( & s,source) ; push( & s,temp) ; push( & s,destination) ;
n-- ; swap( source,temp) ;
goto L0;
L2: ;
}
if ( ! isempty( & s) ) {
destination= pop( & s) ;
temp= pop( & s) ;
source= pop( & s) ;
n= pop( & s) ;
switch ( n) {
case 1 : goto L1; break ;
case 2 : goto L2; break ;
}
}
}
int push( stack * p,int x) {
if ( isfull( p) )
return 1 ;
p- > sp++ ;
p- > stackarray[ p- > sp] = x;
}
int pop( stack * p) {
if ( isempty( p) )
return 1 ;
else
return p- > stackarray[ p- > sp-- ] ;
}
int isempty( stack * p) {
if ( p- > sp== - 1 )
return 1 ;
else
return 0 ;
}
int isfull( stack * p) {
if ( p- > sp== N- 1 )
return 1 ;
else
return 0 ;
}
void sw
I2luY2x1ZGU8aW9zdHJlYW0+CgojZGVmaW5lIE4gMTAwCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKCgpzdHJ1Y3Qgc3RhY2t7CgogICAgICAgaW50IHN0YWNrYXJyYXlbTl07CgogICAgICAgaW50IHNwOwoKICAgICAgIAoKICAgICAgIAoKICAgICAgIH07Cgogc3RhY2sgczsKCiAgICAgICAgCgogCgppbnQgcHVzaChzdGFjayAqLGludCk7CgppbnQgcG9wKHN0YWNrICopOwoKdm9pZCBwcmludHN0YWNrKHN0YWNrICopOwoKaW50IGlzZW1wdHkoc3RhY2sgKik7CgppbnQgaXNmdWxsKHN0YWNrICopOyAKCnZvaWQgbW92ZShpbnQsY2hhcixjaGFyLGNoYXIpOyAKCiAgICAgICAKCmludCBtYWluKCl7CgogICAgCgogICAgcy5zcD0tMTsgIAoKICAgIAoKICAgIAoKICAgIAoKICAgIG1vdmUoMywnQScsJ0InLCdDJyk7CgogICAgCgogICAgCgogICAKCiAgICBzeXN0ZW0oInBhdXNlIik7CgogICAgIHJldHVybiAwOwoKICAgICAKCiAgICAgCgogICAgIH0KCgoKdm9pZCBtb3ZlKGludCBuLGNoYXIgc291cmNlLGNoYXIgdGVtcCxjaGFyIGRlc3RpbmF0aW9uKXsKCiAgICAgCgogICAgICAKCiAgICAgIEwwOgoKICAgICAgICAgICAgICBpZihuKXsKCiAgICAgICAgICAgICAgcHVzaCgmcywxKTtwdXNoKCZzLG4pO3B1c2goJnMsc291cmNlKTtwdXNoKCZzLHRlbXApO3B1c2goJnMsZGVzdGluYXRpb24pOwoKICAgICAgICAgICAgICBuLS07c3dhcCh0ZW1wLGRlc3RpbmF0aW9uKTsKCiAgICAgICAgICAgICAgZ290byBMMDsKCiAgICAgTDE6ICAgICAgY291dDw8Ik1vdmUgZGlzayI8PG48PCIgZnJvbSAiPDxzb3VyY2U8PCIgdG8gIjw8ZGVzdGluYXRpb248PGVuZGw7CgogICAgICAgICAgICAgIHB1c2goJnMsMik7cHVzaCgmcyxuKTtwdXNoKCZzLHNvdXJjZSk7cHVzaCgmcyx0ZW1wKTtwdXNoKCZzLGRlc3RpbmF0aW9uKTsKCiAgICAgICAgICAgICAgbi0tOyBzd2FwKHNvdXJjZSx0ZW1wKTsKCiAgICAgICAgICAgICAgZ290byBMMDsKCiAgICAgTDI6ICAgICAgOwoKICAgICAKCiAgICAgfQoKCgogICAgIGlmKCFpc2VtcHR5KCZzKSl7CgogICAgICAgICAgICAgICAgICAgICAgCgogICAgICAgICBkZXN0aW5hdGlvbj1wb3AoJnMpOwoKICAgICAgICAgdGVtcD1wb3AoJnMpOyAgICAgCgogICAgICAgICBzb3VyY2U9cG9wKCZzKTsKCiAgICAgICAgIG49cG9wKCZzKTsKCiAgICAgICAgIHN3aXRjaChuKXsKCiAgICAgICAgICAgIGNhc2UgMTpnb3RvIEwxO2JyZWFrOyAgICAgICAKCiAgICAgICAgICAgIGNhc2UgMjpnb3RvIEwyO2JyZWFrOyAgICAgICAKCiAgICAgICAgICAgICAgICAgICB9ICAgCgogICAgICAgICAgICAgICAgICAgCgogICAgICAgICAgICAgICAgICAgCgogICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoKICAgICAgICAgICAgICAgICAgICAgIAoKICAgICAgICAgICAgICAgICAgICAgIH0KCgoKfQoKICAgICAKCiAgICAgCgppbnQgcHVzaChzdGFjayAqcCxpbnQgeCl7CgogICAgIGlmKGlzZnVsbChwKSkKCiAgICAgcmV0dXJuIDE7CgogICAgIHAtPnNwKys7CgogICAgIHAtPnN0YWNrYXJyYXlbcC0+c3BdPXg7CgogICAgIAoKICAgICB9CgogICAgIAoKaW50IHBvcChzdGFjayAqcCl7CgogICAgIGlmKGlzZW1wdHkocCkpCgogICAgIHJldHVybiAxOwoKICAgICBlbHNlCgogICAgIHJldHVybiBwLT5zdGFja2FycmF5W3AtPnNwLS1dOyAgICAgCgogICAgIAoKICAgICAgCgogICAgIH0gCgogICAgIAoKaW50IGlzZW1wdHkoc3RhY2sgKnApewoKICAgICBpZihwLT5zcD09LTEpCgogICAgIHJldHVybiAxOwoKICAgICBlbHNlCgogICAgIHJldHVybiAwOyAgICAgIAoKICAgICB9CgogICAgIAoKaW50IGlzZnVsbChzdGFjayAqcCl7CgogICAgaWYocC0+c3A9PU4tMSkgICAKCiAgICByZXR1cm4gMTsKCiAgICBlbHNlCgogICAgcmV0dXJuIDA7ICAgCgp9CgoKCgoKdm9pZCBzdw==