codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

115B.md (3068B)


      1 Source: https://codeforces.com/problemset/problem/115/B
      2 
      3 B. Lawnmower
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 stdin
     16 
     17 output
     18 
     19 stdout
     20 
     21 You have a garden consisting entirely of grass and weeds. Your garden is described by an *n* × *m* grid, with rows numbered 1 to *n* from top to bottom, and columns 1 to *m* from left to right. Each cell is identified by a pair (*r*, *c*) which means that the cell is located at row *r* and column *c*. Each cell may contain either grass or weeds. For example, a 4 × 5 garden may look as follows (empty cells denote grass):
     22 
     23  ![](https://espresso.codeforces.com/ac8d9023dd45a8d2bf447ae30b7392ba60273274.png) 
     24 
     25 You have a land-mower with you to mow all the weeds. Initially, you are standing with your lawnmower at the top-left corner of the garden. That is, at cell (1, 1). At any moment of time you are facing a certain direction — either left or right. And initially, you face right.
     26 
     27 In one move you can do either one of these:
     28 
     29 1) Move one cell in the direction that you are facing.
     30 
     31 * if you are facing right: move from cell (*r*, *c*) to cell (*r*, *c* + 1) ![](https://espresso.codeforces.com/1054e100cdeb897ba0302d96757cc48a65b862f2.png)
     32 * if you are facing left: move from cell (*r*, *c*) to cell (*r*, *c* - 1) ![](https://espresso.codeforces.com/d54bd7dff77844e635a768561884e9ba3cc2693e.png)
     33 
     34 2) Move one cell down (that is, from cell (*r*, *c*) to cell (*r* + 1, *c*)), and change your direction to the opposite one.
     35 
     36 * if you were facing right previously, you will face left ![](https://espresso.codeforces.com/44ff276945b3aa720c016e4b8010b1dcce6b305f.png)
     37 * if you were facing left previously, you will face right ![](https://espresso.codeforces.com/0eda77c3f9484f9d2e28ba0e1590d5c0f4af2ead.png)
     38 
     39 You are not allowed to leave the garden. Weeds will be mowed if you and your lawnmower are standing at the cell containing the weeds (your direction doesn't matter). This action isn't counted as a move.
     40 
     41 What is the minimum number of moves required to mow all the weeds?
     42 
     43 Input
     44 
     45 The first line contains two integers *n* and *m* (1 ≤ *n*, *m* ≤ 150) — the number of rows and columns respectively. Then follow *n* lines containing *m* characters each — the content of the grid. "G" means that this cell contains grass. "W" means that this cell contains weeds.
     46 
     47 It is guaranteed that the top-left corner of the grid will contain grass.
     48 
     49 Output
     50 
     51 Print a single number — the minimum number of moves required to mow all the weeds.
     52 
     53 Examples
     54 
     55 Input
     56 
     57 ```
     58 4 5  
     59 GWGGW  
     60 GGWGG  
     61 GWGGG  
     62 WGGGG
     63 ```
     64 
     65 Output
     66 
     67 ```
     68 11
     69 ```
     70 
     71 Input
     72 
     73 ```
     74 3 3  
     75 GWW  
     76 WWW  
     77 WWG
     78 ```
     79 
     80 Output
     81 
     82 ```
     83 7
     84 ```
     85 
     86 Input
     87 
     88 ```
     89 1 1  
     90 G
     91 ```
     92 
     93 Output
     94 
     95 ```
     96 0
     97 ```
     98 
     99 Note
    100 
    101 For the first example, this is the picture of the initial state of the grid:
    102 
    103  ![](https://espresso.codeforces.com/54442a964aa30f8e0d2fd1ddf4b4361c1612ce0e.png) 
    104 
    105 A possible solution is by mowing the weeds as illustrated below:
    106 
    107  ![](https://espresso.codeforces.com/dfb903698561095c2d456b5f9bfb0c0162053e31.png)