Under construction

Still being built. Things may change

Crazy Arduino

Time limit1sMemory limit128 MB

Summary
Simulate Jongsu's moves and the chasing robots on the R by C board, and report the final board or the move where he is caught.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

Jongsu built a game called "Robots" using an Arduino. Jongsu controls a single Arduino and must keep dodging the crazy Arduinos that chase him. A crazy Arduino keeps closing in on Jongsu's Arduino, but its movement is completely predictable.

The game is played on an R×CR \times C board, and one turn consists of the following five steps. The turn is repeated as many times as the length of the given move string.

  1. First, Jongsu moves his Arduino one cell in one of the 8 directions (horizontal, vertical, or diagonal), or leaves it in place.
  2. If Jongsu's Arduino moves onto a cell that holds a crazy Arduino, the game ends and Jongsu loses.
  3. Each crazy Arduino moves one cell in the direction that brings it closest to Jongsu's Arduino. If Jongsu is at (r1,s1)(r_1, s_1) and the crazy Arduino is at (r2,s2)(r_2, s_2), it picks the move that minimizes ∣r1−r2∣+∣s1−s2∣|r_1 - r_2| + |s_1 - s_2| afterward.
  4. If a crazy Arduino moves onto the cell holding Jongsu's Arduino, the game ends and Jongsu loses.
  5. If two or more distinct crazy Arduinos end up on the same cell, a large explosion occurs and every crazy Arduino on that cell is destroyed.

You are given Jongsu's starting position, the positions of the crazy Arduinos, and the direction Jongsu tries to move on each turn. Determine the final state of the board after Jongsu moves as directed. If the game ends partway through, determine on which move it ends.

Input

The first line contains the board size RR and CC. (1≤R,C≤1001 \le R, C \le 100)

Each of the next RR lines contains CC characters describing the board. . is an empty cell, R is a crazy Arduino, and I is Jongsu's Arduino.

The last line contains a string of length at most 100100 giving, in order, the direction Jongsu tries to move on each turn. Directions follow the layout of a numeric keypad. 5 means staying in place, and the other digits mean the directions below. "Up" is the direction in which the row number decreases.

7 = ↖   8 = ↑   9 = ↗
4 = ←   5 = stay   6 = →
1 = ↙   2 = ↓   3 = ↘

Direction keypad

No input makes Jongsu move off the board.

Output

If the game ends partway through, print kraj X. XX is the number of moves Jongsu attempts, counting the turn on which the game ends. That is, if the game ends while processing the KK-th direction, then XX is KK.

If the game does not end before every direction is processed, print the final state of the board in the same format as the input.

Examples3

  1. Example 1

    Input
    4 5
    I....
    .....
    .R.R.
    .....
    6
    
    Expected output
    .I...
    .RR..
    .....
    .....
    
  2. Example 2

    Input
    9 10
    ..........
    .........R
    ..........
    R.........
    R...I.....
    R.........
    ..........
    .........R
    ....R.....
    5558888
    
    Expected output
    ....I.....
    ....R.....
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    ..........
    
  3. Example 3

    Input
    12 8
    ...I....
    ........
    ........
    ........
    ........
    RR......
    ......RR
    R......R
    ........
    ........
    ........
    ...R....
    66445394444162
    
    Expected output
    kraj 11