Solved LeetCode program · 999 of 1,000
#1377 · Frog Position After T Seconds
Original English source
Keep a shared links as an empty list of whole numbers.
For every cell from 0 to 63.
Place 0 into links.
To join from first (a whole number) with second (a whole number). Set item (first times 8 plus second) of links to 1. Set item (second times 8 plus first) of links to 1.
Join from 1 with 2. Join from 1 with 3. Join from 1 with 7. Join from 2 with 4. Join from 2 with 6. Join from 3 with 5. Keep a shared nodes as an empty list of whole numbers. Place 1 into nodes. Keep a shared parents as an empty list of whole numbers. Place 0 into parents. Keep a shared depths as an empty list of whole numbers. Place 0 into depths. Keep a shared chances as an empty list of decimals. Place 1.0 into chances. Keep a shared kids of 0. Keep a shared count of 0. Keep a shared limit of 0. Keep a shared target of 0. Keep a shared result as a decimal. Keep a shared chance as a decimal. Keep a shared share as a decimal.
To countkids from node (a whole number) with parent (a whole number). Set kids to 0.
For every neighbor from 1 to 7.
If item (node times 8 plus neighbor) of links is 1 and neighbor is not parent, add 1 to kids.
To spread from node (a whole number) with code (a whole number). The parent is code divided by 100. The depth is code modulo 100. If depth is at least limit, return. If kids is 0, return. Set share to chance divided by kids as a decimal.
For every neighbor from 1 to 7.
If item (node times 8 plus neighbor) of links is 1 and neighbor is not parent.
Place neighbor into nodes. Place node into parents. Place depth plus 1 into depths. Place share into chances. Add 1 to count.
Set limit to 2. Set target to 4. The head is 0. Set count to 1.
Repeat while head is less than count.
The node is item head of nodes. The parent is item head of parents. The depth is item head of depths. Set chance to item head of chances. Countkids from node with parent. If node is target and depth is limit, add chance to result. If node is target and depth is less than limit and kids is 0, add chance to result. The code is parent times 100 plus depth. Spread from node with code. Add 1 to head.
Show result.
Expected stdout
0.1667