Under construction

Still being built. Things may change

Pegman (Small)

Time limit5sMemory limit512 MB

Summary
Change as few arrows as possible so a walker following arrows from any start cell never leaves the grid.
Level

Medium6 of 10

Topics
Brute force, Simulation
Solved
No attempts yet

Problem

While using Google Street View, you may have picked up the Pegman figure and dropped him somewhere. Today a mischievous user is going to place Pegman on some cell of a rectangular grid of unit cells with RR rows and CC columns. Each cell of the grid is either blank or holds an arrow pointing up, right, down, or left.

If Pegman is placed on a blank cell, he stands still forever. If the cell holds an arrow, he starts walking in that direction. While he walks, a blank cell leaves his direction unchanged, and an arrow turns him to the direction of that arrow before he keeps walking.

Pegman might walk around the grid forever, but he might also walk off the edge of the grid. You can prevent that by changing the direction of one or more arrows. Each arrow can only be turned to one of the other three directions; arrows cannot be added or removed.

What is the smallest number of arrows you have to change so that Pegman never walks off the edge, no matter where he is placed?

Input

The first line contains the number of test cases TT. TT test cases follow. Each test case begins with one line holding two space separated integers RR and CC. That line is followed by RR lines of CC characters each, where every character describes one cell.

. no arrow
^ up arrow
> right arrow
v down arrow
< left arrow

Limits

  • 1≤T≤1001 \le T \le 100
  • 1≤R,C≤41 \le R, C \le 4

Output

For each test case, print one line in the form Case #x: y, where xx is the test case number starting from 1 and yy is the minimum number of arrows that must be changed so that Pegman never leaves the grid, wherever he is placed. Print IMPOSSIBLE in place of yy if no amount of changes achieves this.

Hint

In case 1 of the first example, Pegman walks off the top edge wherever he is placed. Turning the upper arrow downward makes him walk back and forth between the two arrows forever.

In case 2, Pegman circles the board clockwise wherever he is placed. No arrow has to change.

In case 3, the mischievous user can place Pegman on the up arrow in the middle, and then he walks off the top edge. Turning that arrow does not help, because it only sends him off a different edge.

In case 4, the only cell available is blank, so Pegman stands still and is in no danger.

Examples2

  1. Example 1

    Input
    4
    2 1
    ^
    ^
    2 2
    >v
    ^<
    3 3
    ...
    .^.
    ...
    1 1
    .
    
    Expected output
    Case #1: 1
    Case #2: 0
    Case #3: IMPOSSIBLE
    Case #4: 0
    
  2. Example 2

    Input
    3
    1 1
    ^
    1 2
    <>
    1 4
    >..<
    
    Expected output
    Case #1: IMPOSSIBLE
    Case #2: 2
    Case #3: 0