Back
Close

Hexagonal Maze

Statement

 Goal

You are in a maze, made of hexagonal cells.
The following grid :
4 4
ABCD
EFGH
IJKL
MNOP

has to be understood like this :
  / \   / \   / \   / \
/ \ / \ / \ / \
| | | | |
| A | B | C | D | Line 0
| | | | |
\ / \ / \ / \ / \
\ / \ / \ / \ / \
| | | | |
| E | F | G | H | Line 1
| | | | |
/ \ / \ / \ / \ /
/ \ / \ / \ / \ /
| | | | |
| I | J | K | L | Line 2
| | | | |
\ / \ / \ / \ / \
\ / \ / \ / \ / \
| | | | |
| M | N | O | P | Line 3
| | | | |
\ / \ / \ / \ /
\ / \ / \ / \ /

This means each cell has 6 neighbours : for example, cell F is surrounded by B, C, E, G, J, K.
The grid is periodic, if you go left you appear on the right if there is no wall, and vice versa, similarly with up/down.
So cell B also has 6 neighbours : M, N, A, C, E, F.
Even lines are left-aligned, odd lines are right-aligned.

You are given a grid made by walls and free spaces, you have to draw the shortest path to go from the start to the end.
There may be more than one path, but only one shortest path.
There is always a solution.

The grid contains following symbols :
# : wall
_ : free space
S : start
E : end
You must output the same grid with symbols . on cells which are on the shortest way.
Input
First line: two integers w and h, width and height of the grid.

h following lines: the grid.
Output
h lines : the grid with the answer.
Constraints
4 ≤ w, h ≤ 25
h is always even.
Example
Input
5 6
#####
#S#E#
#_#_#
#_#_#
#___#
#####
Output
#####
#S#E#
#.#.#
#.#.#
#_..#
#####

Tags
MazeBFSPathfinding

Difficulty
Medium

Test cases
Easy Test
Input
5 6 ##### #S#E# #_#_# #_#_# #___# #####
Output
##### #S#E# #.#.# #.#.# #_..# #####

Easy Validator
Input
5 6 ##### #___# #_#_# #_#_# #S#E# #####
Output
##### #.._# #.#.# #.#.# #S#E# #####

Loop Test
Input
8 8 #E_____# ##_###_# ##_____# #_#_#### ##_____# ####_#_# #S_____# ########
Output
#E.____# ##.###_# ##_.___# #_#.#### ##__.__# ####.#_# #S...__# ########

Loop Validator
Input
8 8 #E_____# ####_#_# ##_____# #_####_# ##_____# ##_###_# #S_____# ########
Output
#E...__# ####.#_# ##...__# #.####_# ##.____# ##.###_# #S.____# ########

Through borders Test
Input
6 6 #___## _#__#_ #__##_ ###E#_ __S#_# ###_##
Output
#...## .#__#. #__##. ###E#. ..S#.# ###.##

Through borders Validator
Input
6 6 #_#_## _#_S#_ #__##_ ###_#_ __E#_# ###_##
Output
#_#_## .#.S#. #..##. ###_#. ..E#_# ###_##

Corner ... Test
Input
5 6 E_### #_### ##_## ##_## ###S# ###__
Output
E_### #_### ##_## ##_## ###S# ###..

Corner ... Validator
Input
5 6 S#### _#### #_### #_### ##_E# ##___
Output
S#### _#### #_### #_### ##_E# ##_..

... or not Test
Input
5 6 ####_ #__E# #_### #_### #S### _####
Output
####_ #..E# #.### #.### #S### _####

... or not Validator
Input
5 6 ####_ ###S# ####_ #___# #E### _####
Output
####_ ###S# ####. #...# #E### _####

Everything Test
Input
15 12 _##_#####_###_# #__######_##_#_ ##S#_____#_#_#_ ###_####__#_#_# ###_#####_#___# #__#####_#_#_## #_######_#_#_## ________#_#__## _###_####_#_#_# #___#####__#_#_ ######E__###_#_ ___#####_####_#
Output
.##.#####.###.# #_.######.##.#. ##S#_____#.#.#. ###_####_.#.#.# ###_#####.#_..# #__#####.#_#_## #_######.#_#_## ........#_#__## .###_####_#_#_# #___#####__#_#. ######E..###_#. ...#####.####.#

Everything Validator
Input
15 12 ___##_########_ ##_##_#_____##_ #__###E##_##_#_ _###_##_#_##__# _####_##_#_#### #####__________ ___############ ##_###S#___#### ##____#__##_### #_#_#_#_##_#### ####__##_##_#__ ####_###_____#_
Output
...##.########_ ##.##.#_____##_ #..###E##_##_#_ .###_##_#_##__# .####_##_#_#### #####_________. ...############ ##.###S#___#### ##_.__#._##_### #_#.#_#.##_#### ####._##.##_#.. ####.###.....#.

Real hexagon Test
Input
21 20 ##################### #####_#________###### #####_#_#######_##### ####_#_#______#_##### ####_#_#_#####_#_#### ###_#_#_#____#_#_#### ###_#_##__###_#_#_### ##_#_#_##___#_#_#_### ##_#_#_#__##_#_#_#_## #_#_#_#_##_#_#_#_#_## #S#_#_#_#_E__##_#_#_# #_#_#_#_#__#_#_#_#_## ##_#_#_#_###_#_#_#_## ##_#_#_#____#_#_#_### ###_#_#_#######_#_### ###_#_#_______#__#### ####___#######_#_#### ####_#________#_##### #####_#####_###_##### #####___#______######
Output
##################### #####_#________###### #####_#_#######_##### ####_#_#......#_##### ####_#_#.#####.#_#### ###_#_#.#____#.#_#### ###_#_##..###_#.#_### ##_#_#_##...#_#.#_### ##_#_#_#__##.#_#.#_## #_#_#_#_##_#.#_#.#_## #S#_#_#_#_E..##_#.#_# #.#_#_#_#__#_#_#.#_## ##.#_#_#_###_#_#.#_## ##.#_#_#____#_#.#_### ###.#_#_#######.#_### ###.#_#_______#._#### ####...#######_#.#### ####_#.....___#.##### #####_#####.###.##### #####___#__....######

Real hexagon Validator
Input
21 20 ##################### #####_#________###### #####_#_#######_##### ####_#_#______#_##### ####_#_#_#####_#_#### ###_#_#_#____#_#_#### ###_#_##__###_#_#_### ##_#_#_##___#_#_#_### ##_#_#_#__##_#_#_#_## #_#_#_#_##_#_#_#_#_## #S#_#_#_#_E__##_#_#_# #_#_#_#_#__#_#_#_#_## ##_#_#_#_###_#_#_#_## ##_#_#_#______#_#_### ###_#_#_#######_#_### ###_#_#_______#__#### ####___#######_#_#### ####_#________####### #####_#####_###_##### #####___#______######
Output
##################### #####_#........###### #####_#.#######.##### ####_#.#......#.##### ####_#.#.#####.#.#### ###_#.#.#____#.#.#### ###_#.##..###_#.#.### ##_#.#_##...#_#.#.### ##_#.#_#__##.#_#.#.## #_#.#_#_##_#.#_#.#.## #S#.#_#_#_E..##_#.#.# #.#.#_#_#__#_#_#.#.## ##.#.#_#_###_#_#.#.## ##.#.#_#______#.#.### ###.#.#_#######.#.### ###.#.#_______#..#### ####.._#######_#_#### ####_#________####### #####_#####_###_##### #####___#______######

Nothing to change Test
Input
4 4 ##_# #S## #_E# #_##
Output
##_# #S## #_E# #_##

Nothing to change Validator
Input
4 4 #### ___S E### ####
Output
#### ___S E### ####

Solution language

Solution

Stub generator input