N-Credible Mazes

Time limit1sMemory limit128 MB

Summary
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 nn-dimensional space (nn a positive integer) whose coordinates are all non-negative integers. For example, (1,2,3)(1, 2, 3) 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 11 in a single dimension. For example, (1,2,3)(1, 2, 3) is adjacent to (0,2,3)(0, 2, 3), (2,2,3)(2, 2, 3), and (1,2,4)(1, 2, 4), but not to (2,3,3)(2, 3, 3), (3,2,3)(3, 2, 3), or (1,2)(1, 2).

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 nn, the dimension of the space (1≤n≤101 \le n \le 10, and every coordinate value is less than 1010).

The next line contains 2n2n non-negative integers: the first nn are the coordinates of the start n-tersection (least dimension first) and the next nn are the coordinates of the end n-tersection.

Then follow zero or more lines, each with 2n2n non-negative integers describing one path between two adjacent n-tersections (the first nn integers are one endpoint, the next nn 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.

Examples3

  1. Example 1

    Input
    2
    0 0 2 2
    0 0 0 1
    0 1 0 2
    0 2 1 2
    1 2 2 2
    -1
    3
    1 1 1 1 2 3
    1 1 2 1 1 3
    1 1 3 1 2 3
    1 1 1 1 1 0
    1 1 0 1 0 0
    1 0 0 0 0 0
    -1
    0
    
    Expected output
    Maze #1 can be travelled
    Maze #2 cannot be travelled
    
  2. Example 2

    Input
    2
    5 5 5 5
    -1
    0
    
    Expected output
    Maze #1 can be travelled
    
  3. Example 3

    Input
    2
    0 0 1 1
    -1
    0
    
    Expected output
    Maze #1 cannot be travelled