2

I'm developing an application in ASP.NET with C# and i'm trying to figure out the best way to implement a logic statement that will stop the system from allowing another reservation to be taken if the trailer for canoes and kayaks is full. The issue is the trailer will hold canoes and kayaks, but there's a lot of different combinations.

There are 5 "rows" on the trailer that count upwards vertically, and 2 "columns" that dissect the 5 rows in the middle. I will draw you a diagram to show you what it looks like, and what boats can go where. "C" will stand for Canoe and "K" will stand for Kayak. The trailer looks like this:

C only|C only  }
______|______  }  BOAT TRAILER
 1C\2K|1C\2K   }
______|______  }     
 1C\2K|1C\2K   } 
______|______  }
 1C\2K|1C\2K   }
______|______  }
C only| C only }
______|______  }

So my question is, what's the best option as far as logic is concerned to not take any more "reservations" when the trailer is full? This application will be a .aspx form that will do an insert command to SQL server taking customer information.

[I posted this on the stack overflow site and someone suggested I post this here because one of you might enjoy this problem!]

Thanks!

user3267755
  • 123
  • 2
  • I can't quite understand the problem. So you have a trailer with $10$ slots for boats in it. What does "1C/2K" mean? You can put either one canoe, or two kayaks? You say you need to determine whether or not the trailer is full - what data do you have? Just how many kayaks and canoes are currently reserved? – Jack M Jun 26 '14 at 18:25
  • @JackM Yes that's correct, levels 2-4 can take 1 canoe or 2 kayaks in the slot, whereas the top and bottom levels can only have 1 canoe each, if applicable. The data I would be working with for this problem would just be whether the customer would be using a canoe (2 persons only) or a kayak (1 person only). And then the trailer constraints that I provided above. – user3267755 Jun 26 '14 at 18:29

2 Answers2

1

It looks like you have ten bays, six of which can take either one canoe or two kayaks and four which can take only one canoe, so count bays.

kayak bays=kayaks/2 (round up)
canoe bays=canoes
bays=kayak bays+canoe bays
if (kayak bays > 6 or bays > 10) fail

When somebody tries to make a reservation, total up the existing reservations with the new one and check for fail.

Ross Millikan
  • 374,822
0

The trouble here is to define what an optimal solution is. I think we can make the following two assumptions:

  1. Since you don't control what reservations you'll get or in what order, we may as well think of the reservations as some predetermined sequence of canoe or kayak reservations which we don't know ahead of time.
  2. Once you accept a reservation and put their boat on the trailer, you never remove it in order to make room for a reservation that you receive later: that is, you never cancel reservations.
  3. When you receive a reservation, if it is at all possible to put their boat on the trailer along with the boats already meant to be on there, you put it on. You do not try to strategize based on what reservations might be made in future: the process is first-come-first-served.

So an optimal logic is one such that, no matter what the sequence of reservations is, obeys rules 2 and 3.

This logic seems like it should fit the bill:

Receive canoe reservation
    If there's a canoe-only slot open
        Use that
    Else
        If there's any other slot open
             Use that
        Else
             No room for canoes
Receive kayak reservation
    If there's a slot with exactly one kayak in it
        Use that
    Else
        If there's any kayak-capable slot open
             Use that
        Else
             No room for kayaks

You might be able to prove that this satisfies the above requirements by induction, but it seems obvious to me that you can't do better than this algorithm.

Jack M
  • 27,819
  • 7
  • 63
  • 129