N-Credible Mazes
Time limit1sMemory limit128 MB
Given a dimension n and a list of paths between adjacent lattice points, decide if start and end coordinates are connected.
- Level
Medium4 of 10
- Topics
- Graph, DFS, Hash map, Implementation
- Solved
- No attempts yet
Problem
Define an n-tersection as a point in -dimensional space ( a positive integer) whose coordinates are all non-negative integers. For example, is an n-tersection in three-dimensional space.
Two n-tersections are adjacent when they have the same number of dimensions and their coordinates differ by exactly in a single dimension. For example, is adjacent to , , and , but not to , , or .
An n-teresting space is a collection of paths, where each path directly connects two adjacent n-tersections. An n-credible maze is an n-teresting space together with two chosen n-tersections: a start and an end.
For each maze, decide whether you can travel from the start n-tersection to the end n-tersection using only the given paths.
Input
The input contains the descriptions of one or more mazes.
The first line of a description gives , the dimension of the space (, and every coordinate value is less than ).
The next line contains non-negative integers: the first are the coordinates of the start n-tersection (least dimension first) and the next are the coordinates of the end n-tersection.
Then follow zero or more lines, each with non-negative integers describing one path between two adjacent n-tersections (the first integers are one endpoint, the next are the other). This list is ended by a line containing only -1.
Several maze descriptions may appear. The input ends with a line whose dimension value is 0; no data follows that terminating zero.
Output
For each maze, print its position in the input as Maze #k (the first maze is Maze #1, the second Maze #2, and so on). On the same line, print can be travelled if it is possible to travel through the space from the start n-tersection to the end n-tersection, or cannot be travelled if it is not.