Under construction

Still being built. Things may change

Rocky

Time limit1sMemory limit128 MB

Summary
A horse walks straight across a grid and turns by fixed rules when a rock blocks it; report the exit square and the number of stepped squares.
Level

Easy3 of 10

Topics
Simulation, Matrix
Solved
No attempts yet

Problem

Sylvester Stallion is an old horse, and he likes nothing better than wandering the fields around his stable. Sylvester is single minded and walks in a straight line as long as no rock blocks his path. When the square directly ahead of him holds a rock, he does one of three things.

  1. If the square to his right holds no rock, he turns right and keeps walking straight in that direction.
  2. If the square to his right holds a rock and the square to his left does not, he turns left and walks in that direction.
  3. If both squares hold a rock, he turns around and walks back the way he came.

Right and left are taken from the direction Sylvester currently faces. A square outside the field counts as holding no rock.

In a rocky field he may turn several times and leave from an unexpected spot. In the field below, if Sylvester enters at square (1,4)(1, 4), he steps on 12 squares and leaves the field at square (3,5)(3, 5). The top line of the table gives the column numbers, the front of each line gives the row number, and R marks a square with a rock.

123456
5.R....
4...R..
3....R.
2.R...R
1..R.R.

Many of his animal friends worry about him, and his good friend Beau the ram would like to know where these walks end. Given the shape of the field and the square where Sylvester enters, work out which square he leaves from and how many squares he steps on getting there.

Input

The input holds several test cases.

Each test case starts with three integers nn, mm, and rr. nn is the number of columns in the field, mm is the number of rows, rr is the number of rocks, and n,m≤20n, m \le 20. The positions of the rr rocks follow, each written as c r. c is the column number and r is the row number. Rock positions are separated by spaces or newlines and sometimes run across several lines. No square holds more than one rock.

After the rocks comes the square where Sylvester enters, written the same way. Columns are numbered 1 to nn from left to right, and rows are numbered 1 to mm from bottom to top. Sylvester's starting direction is always perpendicular to the side he enters from. His entry square is never a corner square, and it holds no rock.

A line whose three integers are all 0 ends the input. Sylvester never gets trapped in any field given here.

Output

For each test case print one line of the form Case x: c r k. xx is the case number starting at 1, cc and rr are the column and the row of the last square Sylvester stands on before he leaves the field, and kk is the number of squares he steps on during the walk. A square he steps on more than once counts every time.

Examples1

  1. Example 1

    Input
    6 5 7
    2 2 2 5 3 1 4 4
    5 1 5 3 6 2
    1 4
    5 5 0
    1 2
    0 0 0
    
    Expected output
    Case 1: 3 5 12
    Case 2: 5 2 5