Vacation Rentals

Time limit1sMemory limit128 MB

Summary
Given a table of which units are free on each day, schedule a new guest's stay over [a,d) using the fewest unit changes, breaking ties by smallest unit label each night.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy, Implementation, Array
Solved
No attempts yet

Problem

The Fifth Season Resort has a number of condominiums. They are usually occupied by their owners, but while empty they are offered as vacation rentals. The resort has at most 2626 condominiums, so each one is identified by a single uppercase letter.

A guest submits a reservation request with an arrival date and a departure date. Existing reservations are mostly the owners' own, so they cannot be moved to another unit. The guest, however, may be moved from one unit to another between nights. For example, the guest might stay in unit B for the first three nights and then move to unit F for the rest of the stay.

Reservations are counted in nights. A request with arrival date aa and departure date dd means the guest occupies a room on the nights of days a,a+1,…,d−1a, a+1, \dots, d-1 and leaves on day dd. Thus a one-night reservation departs the day after arrival.

Your task is to satisfy the request without changing any existing reservation, using the minimum number of transfers (moves from one unit to another) during the requested period.

Input

The input contains several test cases.

Each test case begins with a line containing two positive integers MM and NN: MM is the number of consecutive days covered by the reservation table, and NN is the number of units. There are at most 100100 days and at least 33 units. Days are numbered 1,2,…,M1, 2, \dots, M, and units are labeled with consecutive uppercase letters starting at AA.

The next MM lines describe the reservation table. The ii-th line corresponds to day ii, and its jj-th character corresponds to the jj-th unit (A,B,C,…A, B, C, \dots). The character X means the unit is already reserved that day, and O means it is available.

The table is followed by one line with two integers: the arrival date and the departure date of the new request. The arrival date is in the range 1…M1 \dots M, and the departure date is greater than the arrival date and at most M+1M+1.

The input ends with a line containing M=N=0M = N = 0, which is not processed.

Output

For each test case, print the case number followed by a colon, then a blank line.

If the request can be satisfied, print a schedule that uses the minimum number of transfers. Each line describes one continuous stay in a single unit and has the form:

<unit>: <start>-<end>

where <unit> is the unit letter, <start> is the day the guest moves into that unit, and <end> is the day the guest moves out. Lines are ordered by increasing start date.

Tie-breaking. Several schedules may reach the minimum number of transfers. Among them, choose the one whose unit labels are lexicographically smallest day by day: prefer the lowest unit label on the first night (so AA is preferred over BB); if there is still a tie, prefer the lowest label on the second night, and so on. This makes the answer unique.

If the request cannot be satisfied, print the single line:

Not available

Separate the output of consecutive test cases with a blank line.

Examples3

  1. Example 1

    Input
    10 7
    XXXXXXX
    XOXXXXO
    XOXXXXO
    XOXXXOX
    OXXOXOX
    XOXOXOX
    OXXOXOX
    OXXXXOX
    XXXXXXX
    XXXXXXX
    2 9
    0 0
    
    Expected output
    Case 1:
    
    B: 2-5
    F: 5-9
    
  2. Example 2

    Input
    3 3
    XXX
    XXX
    XXX
    1 2
    0 0
    
    Expected output
    Case 1:
    
    Not available
    
  3. Example 3

    Input
    3 4
    OOXX
    XXOO
    XXOO
    1 4
    0 0
    
    Expected output
    Case 1:
    
    A: 1-2
    C: 2-4