A. Walking around Berhattan

Codeforces
IDCF10050A
Time1000ms
Memory64MB
Difficulty
English · Original
Formal · Original
As you probably know, Berhattan is a district of Berland's largest city and it consists of equal square blocks. There are n block lines in the east-west direction and m block lines in the south-north direction. The map shows Berhattan as a rectangle with n rows and m columns, so there are n × m blocks in total. There are n + 1 streets running parallel in the east-west direction (horizontally), and there are m + 1 avenues running parallel in the south-north direction (vertically). Streets and avenues split the district into blocks and separate Berhattan from other districts of Berland. Each block in Berhattan is characterized by its _beauty_ bij. A pedestrian can walk only along streets and avenues. When the pedestrian walks along any of four sides of a block, we say he passes the block. Every time the pedestrian passes a block his _satisfaction_ is increased by bij. If the pedestrian has already passed the block one or more times his satisfaction is increased only by bij / 2 rounded down when he passes the block again. You are given the map of Berhattan with the information about the blocks' beauty and the pedestrian's path along the streets and avenues. The path is given as a string containing letters '_L_', '_R_' and '_M_', where '_L_' means a 90 degree left turn, '_R_' means a 90 degree right turn, and '_M_' means walking one block forward by a street or avenue. Facing the east, the pedestrian starts his path in the north-west corner of Berhattan having zero satisfaction level. His path can cross itself and go along the same streets or avenues several times. Pedestrian's satisfaction is increased every time he moves according to the rules described above. Your task is to calculate the total satisfaction the pedestrian will get after finishing his route. The first line of input contains two integers n and m (1 ≤ n, m ≤ 100), where n is a number of block lines in Berhattan running in the east-west direction, and m is a number of block lines in Berhattan running in the south-north direction. The following n lines contain m digits each. The j-th digit of the i-th line represents bij (0 ≤ bij ≤ 9) — the beauty of the corresponding block. The last line of input contains a path in the format specified above. The path consists of 1 up to 500 characters, inclusively. It is guaranteed that the given path doesn't go outside Berhattan. Print a single integer to the output — the total pedestrian's satisfaction. ## Input The first line of input contains two integers n and m (1 ≤ n, m ≤ 100), where n is a number of block lines in Berhattan running in the east-west direction, and m is a number of block lines in Berhattan running in the south-north direction. The following n lines contain m digits each. The j-th digit of the i-th line represents bij (0 ≤ bij ≤ 9) — the beauty of the corresponding block. The last line of input contains a path in the format specified above. The path consists of 1 up to 500 characters, inclusively. It is guaranteed that the given path doesn't go outside Berhattan. ## Output Print a single integer to the output — the total pedestrian's satisfaction. [samples]
**Definitions** Let $ n, m \in \mathbb{Z}^+ $ denote the number of east-west and south-north block lines, respectively. Let $ B = (b_{i,j}) \in \{0,1,\dots,9\}^{n \times m} $ be the beauty matrix, where $ b_{i,j} $ is the beauty of the block at row $ i $, column $ j $. Let $ P \in \{L, R, M\}^* $ be the path string of length $ \ell $, representing the pedestrian’s movements. The pedestrian starts at position $ (0, 0) $ (north-west corner), facing east. Each block $ (i,j) $ is bounded by: - Horizontal streets: $ i $ (north) and $ i+1 $ (south) - Vertical avenues: $ j $ (west) and $ j+1 $ (east) Let $ c \in \{0,1,\dots,n\} \times \{0,1,\dots,m\} $ be the current position (street, avenue). Let $ d \in \{0,1,2,3\} $ be the current direction: 0=east, 1=south, 2=west, 3=north. Let $ v_{i,j} \in \mathbb{N} $ be the number of times block $ (i,j) $ has been passed (each side traversal counts as one pass). **Constraints** 1. $ 1 \leq n, m \leq 100 $ 2. $ 0 \leq b_{i,j} \leq 9 $ for all $ i \in \{0,\dots,n-1\}, j \in \{0,\dots,m-1\} $ 3. $ 1 \leq |P| \leq 500 $ 4. Path stays within bounds: $ 0 \leq c_{\text{street}} \leq n $, $ 0 \leq c_{\text{avenue}} \leq m $ **Objective** Simulate the path, updating position and direction: - 'M': move one unit in direction $ d $, crossing one block side. - If moving east: from $ (i,j) $ to $ (i,j+1) $ → passes block $ (i,j) $ - If moving south: from $ (i,j) $ to $ (i+1,j) $ → passes block $ (i,j) $ - If moving west: from $ (i,j) $ to $ (i,j-1) $ → passes block $ (i,j-1) $ - If moving north: from $ (i,j) $ to $ (i-1,j) $ → passes block $ (i-1,j) $ - 'L': turn left: $ d \leftarrow (d + 3) \mod 4 $ - 'R': turn right: $ d \leftarrow (d + 1) \mod 4 $ For each block pass: - If $ v_{i,j} = 0 $: add $ b_{i,j} $ to satisfaction - Else: add $ \left\lfloor \frac{b_{i,j}}{2} \right\rfloor $ to satisfaction - Increment $ v_{i,j} \leftarrow v_{i,j} + 1 $ Compute total satisfaction after processing all steps in $ P $. Output: total satisfaction.
API Response (JSON)
{
  "problem": {
    "name": "A. Walking around Berhattan",
    "description": {
      "content": "As you probably know, Berhattan is a district of Berland's largest city and it consists of equal square blocks. There are n block lines in the east-west direction and m block lines in the south-north ",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 65536
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10050A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "As you probably know, Berhattan is a district of Berland's largest city and it consists of equal square blocks. There are n block lines in the east-west direction and m block lines in the south-north ...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n, m \\in \\mathbb{Z}^+ $ denote the number of east-west and south-north block lines, respectively.  \nLet $ B = (b_{i,j}) \\in \\{0,1,\\dots,9\\}^{n \\times m} $ be the beauty matrix,...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments