import re


def semantic_layout2str(layout):
    """Converts a semantic layout matrix to string format that is used by MazeEnv class."""
    output_str = ""
    for row in layout:
        for cell in row:
            output_str += "#" if cell == 0 else "O"
        output_str += "\\"
    output_str = output_str[:-1]    # remove last line break
    output_str = re.sub("O", "G", output_str, count=1)   # add goal at random position
    return output_str