N-dimensional travel
InterviewTime limit2sMemory limit512 MB
Given a walk on an N-dimensional integer grid as a list of coordinate indices and signs, decide whether all visited points, including start and end, are distinct.
- Level
Medium5 of 10
- Topics
- Hash map, Implementation, Math, Simulation
- Solved
- No attempts yet
Problem
Subin likes to travel. With nowhere left to go on Earth, Subin sets off into an N-dimensional universe. A point of this universe is written as N coordinates, and the coordinates are indexed from 1 to N.
Subin starts at the origin, the point whose every coordinate is 0, and each move takes two steps.
- Choose the index of the coordinate to move, one of 1 to N.
- Move to the point where that coordinate is 1 larger or 1 smaller. Every other coordinate stays as it was before the move.
Before leaving, Subin wrote down a travel plan. The plan lists, in order, the coordinate index chosen for each move and whether the move increases or decreases that coordinate.
Write a program that follows the plan and prints 1 if every point, including the starting point and the last point, is visited exactly once, and 0 if some point is visited two or more times.
Input
The first line contains the number of dimensions . ()
The second line contains the length of the travel plan. ()
The third line contains the coordinate indices chosen in the plan, in move order, separated by spaces. Each index is between and .
The fourth line contains a string of length . If its -th character is +, the -th move increases the coordinate by 1, and if it is -, the move decreases the coordinate by 1.
Output
Print 1 if every point is visited exactly once, and 0 otherwise.
Hint
In two dimensions, choosing indices 1, 2, 1, 2 with directions +, +, -, - moves along , so is visited twice.
In three dimensions, choosing indices 1, 2, 3, 1, 2 with directions +, +, +, -, - moves along , and no point is visited twice.