908B.md (2740B)
1 Source: https://codeforces.com/problemset/problem/908/B 2 3 B. New Year and Buggy Bot 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Bob programmed a robot to navigate through a 2d maze. 22 23 The maze has some obstacles. Empty cells are denoted by the character '.', where obstacles are denoted by '#'. 24 25 There is a single robot in the maze. Its start position is denoted with the character 'S'. This position has no obstacle in it. There is also a single exit in the maze. Its position is denoted with the character 'E'. This position has no obstacle in it. 26 27 The robot can only move up, left, right, or down. 28 29 When Bob programmed the robot, he wrote down a string of digits consisting of the digits 0 to 3, inclusive. He intended for each digit to correspond to a distinct direction, and the robot would follow the directions in order to reach the exit. Unfortunately, he forgot to actually assign the directions to digits. 30 31 The robot will choose some random mapping of digits to distinct directions. The robot will map distinct digits to distinct directions. The robot will then follow the instructions according to the given string in order and chosen mapping. If an instruction would lead the robot to go off the edge of the maze or hit an obstacle, the robot will crash and break down. If the robot reaches the exit at any point, then the robot will stop following any further instructions. 32 33 Bob is having trouble debugging his robot, so he would like to determine the number of mappings of digits to directions that would lead the robot to the exit. 34 35 Input 36 37 The first line of input will contain two integers *n* and *m* (2 ≤ *n*, *m* ≤ 50), denoting the dimensions of the maze. 38 39 The next *n* lines will contain exactly *m* characters each, denoting the maze. 40 41 Each character of the maze will be '.', '#', 'S', or 'E'. 42 43 There will be exactly one 'S' and exactly one 'E' in the maze. 44 45 The last line will contain a single string *s* (1 ≤ |*s*| ≤ 100) — the instructions given to the robot. Each character of *s* is a digit from 0 to 3. 46 47 Output 48 49 Print a single integer, the number of mappings of digits to directions that will lead the robot to the exit. 50 51 Examples 52 53 Input 54 55 ``` 56 5 6 57 .....# 58 S....# 59 .#.... 60 .#.... 61 ...E.. 62 333300012 63 ``` 64 65 Output 66 67 ``` 68 1 69 ``` 70 71 Input 72 73 ``` 74 6 6 75 ...... 76 ...... 77 ..SE.. 78 ...... 79 ...... 80 ...... 81 01232123212302123021 82 ``` 83 84 Output 85 86 ``` 87 14 88 ``` 89 90 Input 91 92 ``` 93 5 3 94 ... 95 .S. 96 ### 97 .E. 98 ... 99 3 100 ``` 101 102 Output 103 104 ``` 105 0 106 ``` 107 108 Note 109 110 For the first sample, the only valid mapping is , where *D* is down, *L* is left, *U* is up, *R* is right.