Misplaced Pages

Five pillars puzzle: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 21:23, 13 October 2006 editFeureau (talk | contribs)Extended confirmed users3,321 edits stub sorting, Typo fixing using AWB← Previous edit Revision as of 22:43, 14 October 2006 edit undoOrimosenzon (talk | contribs)243 edits NotesNext edit →
Line 172: Line 172:


{{game-stub}}{{Uncat}} {{game-stub}}{{Uncat}}
]

Revision as of 22:43, 14 October 2006

The five pillars puzzle is a riddle in which one is to try to remove a string from a structure of five threaded pillars. The following image illustrates this structure:


A software solution

Apparently, there a general solution for this problem using a mutual recursion (Please note the number of each pillar in the image). The implementation is in C++ but it can be easly converted to any programming language.

 
#include <iostream> 
using namespace std; 
void printRange(int n) {
  if (n==1)
    cout << 1 << "\n"; 
  else 
    cout << n << ".." << 1 <<"\n"; 
}
void insert(int n); 
void release_(int c) {
  if(c==1) 
    return;  
  cout << "Thread through (upwards) ring number " << c << " (stick to the right)\n";
  insert(c-1); 
  release_(c-1); 
}
void release(int n) {
  release_(n); 
  cout << "Remove from ";
  printRange(n);
}
void insert_(int c, int n) {
  if(c==n)
    return;  
  cout << "Thread through (upwards) ring number " << c+1 << " (stick to the left)\n";
  release(c);
  insert_(c+1,n);
} 
void insert(int n) {
  cout << "Put over ";
  printRange(n);
  insert_(1,n);
}
int main() {
  int n; 
  while(true) {
    cout << "\nnumber of pillars? (0 = quit)  ";
    cin >> n ;
    if(n<1)
      break;
    cout << "insert "<<n<<":\n-------\n";
    insert(n); 
    cout << "\n\nrelease "<<n<<":\n-------\n";
    release(n); 
  }
}

Execution example

number of pillars? (0 = quit)  3
3
insert 3:
-------
Put over 3..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 3 (stick to the left)
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 2..1
release 3:
-------
Thread through (upwards) ring number 3 (stick to the right)
Put over 2..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 3..1
number of pillars? (0 = quit)  5
5
insert 5:
-------
Put over 5..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 3 (stick to the left)
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 2..1
Thread through (upwards) ring number 4 (stick to the left)
Thread through (upwards) ring number 3 (stick to the right)
Put over 2..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 3..1
Thread through (upwards) ring number 5 (stick to the left)
Thread through (upwards) ring number 4 (stick to the right)
Put over 3..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 3 (stick to the left)
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 2..1
Thread through (upwards) ring number 3 (stick to the right)
Put over 2..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 4..1
release 5:
-------
Thread through (upwards) ring number 5 (stick to the right)
Put over 4..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 3 (stick to the left)
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 2..1
Thread through (upwards) ring number 4 (stick to the left)
Thread through (upwards) ring number 3 (stick to the right)
Put over 2..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 3..1
Thread through (upwards) ring number 4 (stick to the right)
Put over 3..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 3 (stick to the left)
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 2..1
Thread through (upwards) ring number 3 (stick to the right)
Put over 2..1
Thread through (upwards) ring number 2 (stick to the left)
Remove from 1
Thread through (upwards) ring number 2 (stick to the right)
Put over 1
Remove from 5..1
number of pillars? (0 = quit)  0

Notes

  • Actually, the code can solve any intermediate stage by invoking the proper relaese_(...)/insert_(...) function.
Stub icon

This game-related article is a stub. You can help Misplaced Pages by expanding it.

This redirect has not been added to any content categories. Please help out by adding categories to it so that it can be listed with similar redirects, in addition to a stub category.
Categories:
Five pillars puzzle: Difference between revisions Add topic