The TransCompilator

Solved LeetCode program · 845 of 1,000

#1129 · Shortest Path with Alternating Colors

Medium Breadth-First Search chunk-17.md

Original English source

Keep a shared reds as a list of 9 zeros. Set item 1 of reds to 1. Set item 5 of reds to 1. Keep a shared blues as a list of 9 zeros. Keep a shared nodes as an empty list of whole numbers. Keep a shared colors as an empty list of whole numbers. Keep a shared steps as an empty list of whole numbers. Keep a shared seen as a list of 6 zeros. Keep a shared answers holding -1, -1, and -1. Keep a shared count of 0. Keep a shared head of 0. Place 0 into nodes. Place 0 into colors. Place 0 into steps. Place 0 into nodes. Place 1 into colors. Place 0 into steps. Set count to 2. Set item 0 of seen to 1. Set item 1 of seen to 1.

Repeat while head is less than count.
The front is head. The currentnode is item front of nodes. The currentcolor is item front of colors. The currentstep is item front of steps. Add 1 to head. If item currentnode of answers is -1, set item currentnode of answers to currentstep. The nextcolor is 1 minus currentcolor.
For every next from 0 to 2.
The edge is 0. If nextcolor is 0 and item (currentnode times 3 plus next) of reds is 1, set edge to 1. If nextcolor is 1 and item (currentnode times 3 plus next) of blues is 1, set edge to 1. The mark is next times 2 plus nextcolor. If edge is 1 and item mark of seen is 0, set item mark of seen to 1, Place next into nodes, Place nextcolor into colors, Place (currentstep plus 1) into steps, add 1 to count.

For each value in answers, show value.

Open in Playground Browse all 1,000 problems

Expected stdout

0
1
-1