Problem Statement
Your desktop contains several icons, described in the String[] icons. Initially, the 0-th icon has focus, and when you press arrow keys on your keyboard, the focus may switch to other icons. Each element of icons is formatted as "Name NameUp NameDown NameLeft NameRight" (quotes for clarity), where Name is the name of the icon. When that icon has focus and you press the up, down, left, or right arrow key, the focus will move to the icon with name NameUp, NameDown, NameLeft, or NameRight, respectively. For example, consider an icon described by "A B C B A". The icon is named "A". When icon "A" has focus and you press the up arrow key, the focus will move to icon "B". The down arrow key goes to icon "C", and the left arrow key to icon "B". When you press the right arrow key, the focus remains on icon "A".
You perform the following procedure zero or more times: hold down an arrow key, and keep it pressed until the focus stops changing. You want to know which icons could possibly have the focus in the end. Return a int[], the i-th element of which is '1' (one) if the i-th icon can end up with the focus, or '0' (zero) if the it cannot. See examples for clarification.
Definition
- Class:
- Desktop
- Method:
- focusIcons
- Parameters:
- String[]
- Returns:
- int[]
- Method signature:
- int[] focusIcons(String[] icons)
- (be sure your method is public)
Constraints
- icons will contain between 1 and 50 elements, inclusive.
- Each element of icons will be formatted as described in problem statement.
- In each element of icons, Name will contain between 1 and 5 uppercase letters ('A'-'Z').
- Each Name in icons will be distinct.
- In each element of icons, NameUp, NameDown, NameLeft, and NameRight will each be equal to a Name in icons.
- There will be no cycles (if you press any key focused on any icon, the focus will change a finite number of times).
Examples
{"NAME NAME NAME NAME NAME"}Returns: {1 }{"A A A A B", "B B B B CCC", "CCC CCC CCC B CCC"}Returns: {1, 1, 1 }The focus is initially on icon "A", so if you stop without pressing any keys, "A" will end up with the focus. If you hold down the right arrow key, the focus will move to "B", then "C", and remain there, so "C" can end up with the focus. When the focus is on "C", you can hold down the left arrow key, and it will move to "B" and then stop, so "B" can also end up with the focus.{"A A A A EE", "B B EE B B", "C C C EE C", "D EE D D D", "EE B D A C"}Returns: {1, 0, 1, 0, 0 }You can't stop at icon EE, so you can only move from A to C and from C to A.{"B B E B B", "A A A A E", "C C C E C", "D E D D D", "E B D A C"}Returns: {1, 0, 0, 1, 0 }{"E B D A C", "A A A A E", "B B E B B", "C C C E C", "D E D D D"}Returns: {1, 1, 1, 1, 1 }This example has the same icons and focus behavior as the two previous examples. You can get to more icons here because the initially focused icon is different.