Fire Station [Advanced Challenge]

This is where most of us started. Classic Spectrum basic.

Moderator: Programming Moderators

Post Reply
User avatar
Brayn-3
Posts: 14
Joined: Thu Jun 01, 2017 7:15 pm

Fire Station [Advanced Challenge]

Post by Brayn-3 » Sat Jun 24, 2017 6:50 pm

The best location for the new fire station has to be found. The distance to all n houses should be minimized.

The following program places 10 to 20 houses "randomly" depending on the entered seed value (1-65535). Extend the program to find the best location for the fire station and mark it with a small circle (radius 2). Then draw the circumscribed circle, that includes all houses and print it's radius. The calculated locations shown in the video are not the optimal solutions.

The program area between 0 and 99 must not be changed. Start your program at line 100.

Code: Select all

10 INPUT "No.: ";n: IF n<1 OR n>65535 THEN GO TO 1e4
20 RANDOMIZE n: DIM x(20): DIM y(20): INK 2: CLS 
30 LET n=INT (11*RND+10): LET m=30*RND+30: FOR i=1 TO n
40 LET a=2*PI*RND: LET r=m*RND: LET x(i)=r*COS a+128: LET y(i)=r*SIN a+88
50 PLOT x(i),y(i): DRAW 0,1: DRAW 1,-2: DRAW -2,0: DRAW 0,1
60 NEXT i: LET a=0: LET m=0: LET r=0: INK 0:


Have fun! :-)

The solutions for the examples should be near these values:
54321: r = 35.6
3456: r = 48.6
77: r = 42,0

See also: https://www.facebook.com/groups/ZXBasic ... 957501142/

JoeZX
Posts: 574
Joined: Mon May 29, 2017 9:11 pm
Location: Slovakia

Re: Fire Station [Advanced Challenge]

Post by JoeZX » Sat Jun 24, 2017 7:26 pm

https://media.licdn.com/mpr/mpr/shrinkn ... MzEzMg.jpg
- repeatedly getting this result /just joking and where is line 100, I can see only lines 10 to 60/.

User avatar
Brayn-3
Posts: 14
Joined: Thu Jun 01, 2017 7:15 pm

Re: Fire Station [Advanced Challenge]

Post by Brayn-3 » Sat Jun 24, 2017 10:03 pm

There are only lines 10 to 60. Your solution should start at line 100.

User avatar
emook
Posts: 70
Joined: Mon May 29, 2017 6:55 pm

Re: Fire Station [Advanced Challenge]

Post by emook » Sun Jun 25, 2017 11:47 am

Ok does this need the top most y value and subtracting the lowest, then the same for left most and right most x?

That will then give us the difference and a radius to work with.

I've been thinking about it but haven't had time to write any code.
---------
ZXorDIE
http://zxbasic.uk/NextBuild NextBuild
http://zxbasic.uk ZXBD Snapshot uploader
http://zxbasic.uk/db/0 ZXBasic Online Database

User avatar
Brayn-3
Posts: 14
Joined: Thu Jun 01, 2017 7:15 pm

Re: Fire Station [Advanced Challenge]

Post by Brayn-3 » Sun Jun 25, 2017 3:40 pm

That would be a good approach, but in some cases, you do not get the optimal solution. :-)

Post Reply