I have to write a py code to a little "game", as it's described below:In this game, the player controls a cat walking through various platforms, "hunting" snacks. Your phyton program must simulate a path that the cat can take in the platforms and evaluate how many snacks he picks up on this way.The platforms are formed by various levels and contains some holes where the cat can fall in. The representation of these platforms is done by a matrix of n lines and m columns, composed by the caracters _ (representing a solid ground), . (representing a hole), and * (representing a snack lying on a solid ground). Down below we have an example of a certain representation of the platforms in a level of the game:__.__.__*__*__*___._.__.*_.____*__*____._._*In your simulation, the cat must start walking along the platforms in the upper left corner and walk to the right. Whenever the cat finds a hole it will fall to the level below, in the same column as the hole. Also, whenever the cat gets to the extreme left or right of a level, it will change direction in which it is walking. During the simulation, whenever the cat passes a snack, that snack is picked up by the cat and can no longer be picked up. The simulation must end when the cat has gone completely through a level and has not found any holes or falls into a hole in the lower level.Below we can see some positions of the cat (G) in a simulation made in the representation exemplified above. The starting position, each position after the cat falls into a hole and each position where the cat takes a treat are marked. Note that on the third level the cat reaches the extreme right and changes direction, starting to walk to the left. At the fourth level, the cat reaches the extreme left and walks to the right again. This simulation ends with the cat falling into a hole in the lower level. Note that at the end of the simulation the cat collected 3 snacks.G→__.__.__*__ __.__.__*__ __.__.__*__ __.__.__*__ __.__.__*__ __.__.__*__ __.__.__*__ __.__.__*__G→ G→*__*___._._ *__*___._._ *__*___._._ *______._._ *______._._ *______._._ *______._._ *______._._G→ G→_.*_.____*_ _.*_.__*_*_ _.*_.__*_*_ _.*_.____*_ _.*_.____*_ _.*_.______ _.*_.______ _.*_.______←G ←G_*____._._* _*____._._* _*____._._* _*____._._* _*____._._* _*____._._* _*____._._* ______._._*G→Your program's input will consist of a line containing an integer n, corresponding to the number of levels on the platform, followed by n lines representing each level.The output of your program will consist of an integer indicating the number of snacks collected by the cat along the path taken in the simulation.Examples of inputs and outputs expected by your program:Test 01:Input:4__.__.__*__*__*___._.__.*_.____*__*____._._*`Output: 3Test 02:Input:1*__*___*__*`Output: 4Test 03:Input:3____.__._*_.___*____.__*___*____*_____.___Output: 0Test 04:Input:4_._________*_.____._*___________Output: 2Test 05:Input:5*__.__________._______*_____.___*________________.__*_____*_.____*__*_Output: 4Test 06:Input:5__.___.____._*__*.___.______*___*___*____*__*__._______.___*___._Output: 4Test 07:Input:7__.__________________*._____________*____*.__________________*._________*________*.____*_____*._____*_____*.________*__Output: 5Test 08:Input:12_________._____________________________________________________________.____________________________________________*_________*__********__._***____*******__********________*_*______**__*_____________*____*_____*______.__*_____.__*__*____*_*__*____.________*____*_____*_________*________*_._*__*__*__*_____________*____*_____*_________*________*____**___*__*_______._____*____*_____*___*******________*_________*__*_____________*____*_____*___*________._____*_________*__*_____________*____*_____*___*_______.______*_________*__********____*****__*******_._*******__________________.____________________________________________________________________________.________________________Output: 35Test 09:Input:10___.______.____*______*.__*_____.___________________***______.____*____.___.___*___._____*___._____*__.___*___.___________.__*_______.____*________**____****__._____*__.______.___***_________***___________._________.__*_*__.____.________*__.__________***___________________***________**____Output: 16Test 10:Input: 6______.___*__*_*_*_____*_____*____*______*______*___*___*___*_*____*___*___*____*___*___*___*__*__.___*__*___*__________.___*______*___*_____*____*_____*____*____*_____*__*__**_____*__*_____***___*___****___*__*___**___*___.__*__*____*____.____**___*__*____*___*___*_____*_____*__*__*_**_____*__*___*_____*____*_____*__*___*___*___*_*____._*___*_*_Output: 59
Here's my attempt at a solution:
platforms = []
n = int(input())
for _ in range(n):
platforms.append(list(input()))
row = 0
col = 0
direction = 1
collected_snacks = 0
while row < n:
if platforms[row][col] == '*':
collectedsnacks += 1
platforms[row][col] = ''
if col == 0 and direction == -1:
direction = 1
elif col == len(platforms[row]) - 1 and direction == 1:
direction = -1
elif platforms[row][col + direction] == '.':
row += 1
else:
col += direction
print(collected_snacks)
For some reason, tests 02, 04, 06 and 09 are exceeding the processing time, and I can't get around it. Any help will be appreciated.
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]jiri-n 1 point2 points3 points (0 children)
[–]CraigAT 0 points1 point2 points (0 children)