eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків

Maze

Consider the following maze made of equilateral triangles: \includegraphics{https://static.e-olymp.com/content/46/468a1bdfb80f38349e61eff06394c59e4c7e2496.jpg} Each vertex is described by two coordinates x and y as in the picture. Some of the edges have a white or a black circle on them. There are two major rules that control movement within the maze: \begin{itemize} \item it is only allowed to pass edges with circles on them. \item while walking through the maze it is obligatory to alternate white and black circles; i.e. it is only allowed to pass an edge with white circle if the last circle passed was black and vice versa. It is allowed to pass an edge with either black or white circle on it during the first move. \end{itemize} Write a program to find the length of the shortest path from the entrance point to the exit in the maze. The length of the path is defined as the number of edges (or circles) passed. You may assume that such path always exists. \InputFile The first line contains two integers \textbf{W} and \textbf{H} which are the width and the height of the maze respectively (\textbf{1} ≤ \textbf{W}, \textbf{H} ≤ \textbf{500}). The second line consists of four integer values: \textbf{X_1 Y_1 X_2 Y_2} (\textbf{0} ≤ \textbf{X_1}, \textbf{X_2} ≤ \textbf{W}; \textbf{0} ≤ \textbf{Y_1}, \textbf{Y_2} ≤ \textbf{H} ). (\textbf{X_1}, \textbf{Y_1}) are the coordinates of the entry point in the maze and (\textbf{X_2}, \textbf{Y_2}) are the exit coordinates. The next \textbf{2H+1} lines provide the description of the edges: odd lines (\textbf{3}^rd, \textbf{5}^th, etc) describe horizontal edges, and even lines (\textbf{4}^th, \textbf{6}^th, etc) describe non-horizontal ones. Each line consists of a string of characters \textbf{n}, \textbf{w} and \textbf{b}, where \textbf{n} means, that there is no circle on the edge, and \textbf{w} or \textbf{b} means that there is white or black circle on the edge respectively. There are no spaces between these characters. Naturally, odd lines consist of exactly \textbf{W} characters, and even lines consist of exactly \textbf{2W+1} characters. \OutputFile Your program should output a single integer (the length of the shortest path from entrance point to the exit in the maze) in the first (and the only) line of the outfile. \textbf{Comment} 1: A simple maze. One possible shortest path is this: (\textbf{0}, \textbf{0}) → (\textbf{1}, \textbf{0}) → (\textbf{0}, \textbf{1}) → (\textbf{1}, \textbf{1}) → (\textbf{1}, \textbf{0}) → (\textbf{2}, \textbf{0}) → (\textbf{2}, \textbf{1}) Here is the illustration of the maze and the shortest path: \includegraphics{https://static.e-olymp.com/content/22/22117bf40279ac404282f4ccc108ac8a39d91b3e.jpg} 2: This is the description of the maze given in the picture on the first page. The shortest path is this: (\textbf{0}, \textbf{2}) → (\textbf{1}, \textbf{2}) → (\textbf{1}, \textbf{1}) → (\textbf{2}, \textbf{1}) → (\textbf{2}, \textbf{0}) → (\textbf{3}, \textbf{0}) → (\textbf{3}, \textbf{1}) → (\textbf{3}, \textbf{2}) → (\textbf{4}, \textbf{1}) → (\textbf{3}, \textbf{1}) → (\textbf{3}, \textbf{0}) → (\textbf{2}, \textbf{0}) → → (\textbf{2}, \textbf{1}) → (\textbf{1}, \textbf{1}) →(\textbf{1}, \textbf{2}) → (\textbf{1}, \textbf{3}) → (\textbf{2}, \textbf{3}) → (\textbf{2}, \textbf{4}) → (\textbf{3}, \textbf{4}) → (\textbf{3}, \textbf{3}) → (\textbf{4}, \textbf{3}) → (\textbf{4}, \textbf{2}) → (\textbf{5}, \textbf{2}) (Length: \textbf{22})
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
2 1
0 0 2 1
bb
nwwnw
bn


Вихідні дані #1
6
Джерело BOI-2005