Cakewalk (Codeforces 1505E)
Tags: Greedy, *special

A mouse encountered a nice big cake and decided to take a walk across it, eating the berries on top of the cake on its way. The cake is rectangular, neatly divided into squares; some of the squares have a berry in them, and some don't.The mouse is in a bit of a hurry, though, so once she enters the cake from its northwest corner (the top left cell in the input data), she will only go east (right) or south (down), until she reaches the southeast corner (the bottom right cell). She will eat every berry in the squares she passes through, but not in the other squares.The mouse tries to choose her path so as to maximize the number of berries consumed. However, her haste and hunger might be clouding her judgement, leading her to suboptimal decisions...

Input: The first line of input contains two integers $$$H$$$ and $$$W$$$ ($$$1 \le H, W \le 5$$$), separated by a space, — the height and the width of the cake.The next $$$H$$$ lines contain a string of $$$W$$$ characters each, representing the squares of the cake in that row: '.' represents an empty square, and '*' represents a square with a berry.

Output: Output the number of berries the mouse will eat following her strategy.


Sample Inputs:
4 3
*..
.*.
..*
...

4 4
.*..
*...
...*
..*.

3 4
..**
*...
....

5 5
..*..
.....
**...
**...
**...

Sample Outputs:
3

2

1

1

Solution (Python):
(h, w) = map(int, input().split())
m = list()
ans = 0
for i in range(h):
    s = input()
    m.append(s)
l = 0
r = 0
for i in range(h + w - 1):
    if m[l][r] == '*':
        ans += 1
    if l + 1 < h and m[l + 1][r] == '*':
        l += 1
        continue
    if r >= w - 1:
        l += 1
    else:
        r += 1
print(ans)

Solution (Python):
(h, w) = map(int, input().split())
m = list()
ans = 0
for i in range(h):
    s = input()
    m.append(s)
l = 0
r = 0
for i in range(h + w - 1):
    if m[l][r] == '*':
        ans += 1
    if l + 1 < h and m[l + 1][r] == '*':
        l += 1
        continue
    if r >= w - 1:
        l += 1
    else:
        r += 1
print(ans)

Solution (Python):
(h, w) = map(int, input().split())
m = list()
ans = 0
for i in range(h):
    s = input()
    m.append(s)
l = 0
r = 0
for i in range(h + w - 1):
    if m[l][r] == '*':
        ans += 1
    if l + 1 < h and m[l + 1][r] == '*':
        l += 1
        continue
    if r >= w - 1:
        l += 1
    else:
        r += 1
print(ans)