Book of programming problems

Contains advanced programming problems for schools, colleges and problem-hungry inidividuals

Given cross sequence, untangle 3 hoses so that there are no more crosses
Complexity (1-100): 90
pipes (tangled)
fig. 1. A plant with two devices, connected by tangled hoses
pipes (straighten-up)
fig. 2. Hoses straighten up
A plant consists of two devices A and B, connected by three hoses, laying on ground. Each device has three nipples, numbered 1 to 3, as it is shown on fig.1. Each hose connects nipples with the same number. Hoses may lay in an arbitrary order with one exception: the distance between any point on a hose and device A should increase while moving from A to B.

In order to improve the throughput, it was decided to reorder the hoses, so that there would be no crosses (see Fig. 2). To do so, an inspector was instructed to record information about crosses throughout the hoses from device A to device B. For each cross, the inspector has recorded a pair of numbers: the number of hose below, and the number of hose above (no three hoses cross at the same point). So, for example, for fig. 1 the recording would be

2 1 3 1 2 3 3 2 1 3 1 2

Write a program which, given a space-separated sequence of hose numbers the inspector recorded, would detect:
  • are there errors in recording made by the inspector, and
  • is it possible to untangle hoses without disconnecting them from nipples.
The program should print one of the three messages:
  • "Incorrect data", if the recordings are incorrect,
  • "Untanglable", if there is a way to straighten up the hoses,
  • "Not untanglable", if the hoses cannot be straighten up without disconnecting them from the nipples.
Self-tests:

Input dataResult
3 2 2 3 2 1 1 2 2 3 3 2 1 2 2 1Not untanglable
3 2 2 3 2 1 1 2 2 3 3 2 1 2 2 1 2 1 1 2 3 2 2 3 1 2 2 1 2 3 3 2Untanglable
Solution:Show/hide explanation | Solution.hs
There are no posts in this topic.

Post Reply

Book of programming problems is powered by UseBB 1 Forum Software | Contact Admin