Dinesman's multiple-dwelling problem: Difference between revisions
Content added Content deleted
(Correct link to problem) |
|||
Line 2,498: | Line 2,498: | ||
Miller=5 |
Miller=5 |
||
Smith=1</pre> |
Smith=1</pre> |
||
=={{header|PureBasic - port of C code}}== |
|||
<lang PureBasic> |
|||
EnableExplicit |
|||
Global verbose = #False |
|||
Macro COND ( a, b ) |
|||
Procedure a ( Array s ( 1 ) ) |
|||
ProcedureReturn Bool( b ) |
|||
EndProcedure |
|||
EndMacro |
|||
Prototype condition ( Array s ( 1 ) ) |
|||
#N_FLOORS = 5 |
|||
#TOP = #N_FLOORS - 1 |
|||
Global Dim solutions ( #N_FLOORS - 1 ) |
|||
Global Dim occupied ( #N_FLOORS - 1 ) |
|||
Enumeration tenants |
|||
#baker |
|||
#cooper |
|||
#fletcher |
|||
#miller |
|||
#smith |
|||
#phantom_of_the_opera |
|||
EndEnumeration |
|||
Global Dim names.s ( 4 ) |
|||
names( 0 ) = "baker" |
|||
names( 1 ) = "cooper" |
|||
names( 2 ) = "fletcher" |
|||
names( 3 ) = "miller" |
|||
names( 4 ) = "smith" |
|||
COND( c0, s( #baker ) <> #TOP ) |
|||
COND( c1, s( #cooper ) <> 0 ) |
|||
COND( c2, s( #fletcher ) <> 0 And s( #fletcher ) <> #TOP ) |
|||
COND( c3, s( #miller ) > s( #cooper ) ) |
|||
COND( c4, Abs( s( #smith ) - s( #fletcher ) ) <> 1 ) |
|||
COND( c5, Abs( s( #cooper ) - s( #fletcher ) ) <> 1 ) |
|||
#N_CONDITIONS = 6 |
|||
Global Dim conds ( #N_CONDITIONS - 1 ) |
|||
conds( 0 ) = @c0() |
|||
conds( 1 ) = @c1() |
|||
conds( 2 ) = @c2() |
|||
conds( 3 ) = @c3() |
|||
conds( 4 ) = @c4() |
|||
conds( 5 ) = @c5() |
|||
Procedure solve ( person.i ) |
|||
Protected i.i, j.i |
|||
If person = #phantom_of_the_opera |
|||
For i = 0 To #N_CONDITIONS - 1 |
|||
Protected proc.condition = conds( i ) |
|||
If proc( solutions( ) ) |
|||
Continue |
|||
EndIf |
|||
If verbose |
|||
For j = 0 To #N_FLOORS - 1 |
|||
PrintN( Str( solutions( j ) ) + " " + names( j ) ) |
|||
Next |
|||
PrintN( "cond" + Str( i ) + " bad\n" ) |
|||
EndIf |
|||
ProcedureReturn 0 |
|||
Next |
|||
PrintN( "Found arrangement:" ) |
|||
For i = 0 To #N_FLOORS - 1 |
|||
PrintN( Str( solutions( i ) ) + " " + names( i ) ) |
|||
Next |
|||
ProcedureReturn 1 |
|||
EndIf |
|||
For i = 0 To #N_FLOORS - 1 |
|||
If occupied( i ) |
|||
Continue |
|||
EndIf |
|||
solutions( person ) = i |
|||
occupied( i ) = #True |
|||
If solve( person + 1 ) |
|||
ProcedureReturn #True |
|||
EndIf |
|||
occupied( i ) = #False |
|||
Next |
|||
ProcedureReturn #False |
|||
EndProcedure |
|||
OpenConsole( ) |
|||
verbose = #False |
|||
If Not solve( 0 ) |
|||
PrintN( "Nobody lives anywhere" ) |
|||
EndIf |
|||
Input( ) |
|||
CloseConsole( ) |
|||
End</lang> |
|||
<pre>Found arrangement: |
|||
2 baker |
|||
1 cooper |
|||
3 fletcher |
|||
4 miller |
|||
0 smith</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |