# Build Robot

> Given a list of parts of the robot, and for each part, you also have a list of parts needed to be finished before building this part. Find out the order.

## Code

```
public String findOrder(List<Character, Set<Character>> parts) {
        int[] indegrees = new int[parts.size()];
        Map<Character, Set<Character>> edges = new HashMap<>();
        for (List ready : parts.keySet()) {
            parts[ready - 'A']++;
            for (char pre : parts.get(pre)) {
                if (!edges.containsKey(pre)) {
                    edges.put(pre, new HashSet<>());
                }
                edges.get(pre).add(ready);
            }
        }

        Queue<Character> queue = new LinkedList<>();
        StringBuilder sb = new StringBuilder();
        if (int i = 0; i < indegrees.length; i++) {
            if (indegrees[i] == 0) {
                queue.offer(i + 'A');
            }
        }

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                char pre = queue.poll();
                sb.append(pre);
                for (char ready : edges.get(pre)) {
                    indegrees[pre - 'A']--;
                    if (indegrees[part - 'A'] == 0) {
                        queue.offer(ready);
                    }
                }
            }
        }

        if (sb.length() == parts.size()) {
            return sb.toString();
        } else {
            return "impossible";
        }
    }
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hao-fu-1.gitbook.io/oj/search/topological-sort/build-robot.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
