Jump to content

Dinesman's multiple-dwelling problem: Difference between revisions

m
→‎{{header|PureBasic}}: more flexible and comparable to other entires
m (→‎{{header|PureBasic}}: more flexible and comparable to other entires)
Line 146:
 
=={{header|PureBasic}}==
<lang PureBasic>ProcedurePrototype testDemandscond(Baker,Array Cooper, Fletcher, Miller, Smitht(1))
 
Protected i
Enumeration #Null
For i=1 To 5 ; verify that one person lives at each floor
#Baker
If i<>Baker And i<>Cooper And i<>Fletcher And i<>Miller And i<>Smith
#Cooper
ProcedureReturn #False
#Fletcher
EndIf
#Miller
#Smith
EndEnumeration
 
Procedure checkTenands(Array tenants(1), Array Condions.cond(1))
Protected i, j
Protected.cond *f
j=ArraySize(Condions())
For i=0 To j
*f=Condions(i) ; load the function pointer to the current condition
If *f(tenants()) = #False
ProcedureReturn #False
EndIf
Next
If Baker=5 : ProcedureReturn #False: EndIf
If Cooper =1 : ProcedureReturn #False: EndIf
If Fletcher=1 Or Fletcher=5: ProcedureReturn #False: EndIf
If Miller<Cooper : ProcedureReturn #False: EndIf
If Int(Abs(Smith-Fletcher))=1: ProcedureReturn #False: EndIf
If Int(Abs(Fletcher-Cooper))=1 : ProcedureReturn #False: EndIf
ProcedureReturn #True
EndProcedure
 
Procedure C1(Array t(1))
If Int(Abs(t(#Fletcher)-t(#Cooper)))<>1
ProcedureReturn #True
EndIf
EndProcedure
 
Procedure C2(Array t(1))
Define a,b,c,d,e
If t(#Baker)<>5
OpenConsole()
ProcedureReturn #True
For a=1 To 5
EndIf
For b=1 To 5
EndProcedure
For c=1 To 5
 
For d=1 To 5
Procedure C3(Array t(1))
For e=1 To 5
If t(#Cooper)<>1
If testDemands(a,b,c,d,e)
ProcedureReturn #True
PrintN("Solution found;")
EndIf
PrintN("Baker="+Str(a)+#CRLF$+"Cooper="+Str(b)+#CRLF$+"Miller="+Str(c))
EndProcedure
PrintN("Fletcher="+Str(d)+#CRLF$+"Smith="+Str(e)+#CRLF$)
 
EndIf
Procedure C4(Array t(1))
If t(#Miller) >= t(#Cooper)
ProcedureReturn #True
EndIf
EndProcedure
 
Procedure C5(Array t(1))
If t(#Fletcher)<>1 And t(#Fletcher)<>5
ProcedureReturn #True
EndIf
EndProcedure
 
Procedure C6(Array t(1))
If Int(Abs(t(#Smith)-t(#Fletcher)))<>1
ProcedureReturn #True
EndIf
EndProcedure
 
Procedure main()
If OpenConsole()
Dim People(4)
Dim Conditions(5)
Protected a, b, c, d, e, i
;
;- Load pointers to all conditions
Conditions(i)=@C1(): i+1
Conditions(i)=@C2(): i+1
Conditions(i)=@C3(): i+1
Conditions(i)=@C4(): i+1
Conditions(i)=@C5(): i+1
Conditions(i)=@C6()
;
; generate and the all legal combinations
For a=1 To 5
For b=1 To 5
If a=b: Continue: EndIf
For c=1 To 5
If a=c Or b=c: Continue: EndIf
For d=1 To 5
If d=a Or d=b Or d=c : Continue: EndIf
For e=1 To 5
If e=a Or e=b Or e=c Or e=d: Continue: EndIf
People(#Baker)=a
People(#Cooper)=b
People(#Fletcher)=c
People(#Miller)=d
People(#Smith)=e
If checkTenands(People(), Conditions())
PrintN("Solution found;")
PrintN("Baker="+Str(a)+#CRLF$+"Cooper="+Str(b)+#CRLF$+"Miller="+Str(c))
PrintN("Fletcher="+Str(d)+#CRLF$+"Smith="+Str(e)+#CRLF$)
EndIf
Next
Next
Next
Next
Next
Print("Press ENTER to exit"): Input()
Next
EndIf
Next
EndProcedure
Print("Press ENTER to exit"): Input()</lang>
 
main()</lang>
<pre>Solution found;
Baker=3
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.