IPSC Logo

Internet Problem Solving Contest

IPSC 2011

Problem B – BFS killer

Mark just solved a traditional programming task: finding the shortest path in a maze. The map of his maze is a rectangular grid with r rows and c columns (1 r,c 600). Some of the cells contain walls (denoted by #, ASCII code 35), others are empty (denoted by ., ASCII code 46). There are two special empty cells: one starting point (denoted by S) and one treasure (denoted by T).

In each step the player may only move in one of the four cardinal directions. In other words, each two subsequent cells on his path have to share a common side. The treasure is always reachable from the starting point. The player is not allowed to leave the maze or enter a cell with a wall.

Mark’s task was to find the length of shortest path from the starting point to the treasure. He solved this task using breadth-first search (BFS).

His C++ solution is given in the input file. Below you can find the solution in pseudocode. However, Mark made a common bug in his implementation of the FIFO (first in, first out) queue – he is using a circular array of size k. Show him that his queue is too short!

Problem specification

Find a map of a maze such that if Mark’s program is executed on your maze, the queue size will exceed k at some moment. In the easy subproblem, k = 5000. In the hard subproblem, k = 15000. (The queue size is the number of cells it contains.) Your maze must satisfy all the constraints given above.

define process_cell(r,c):  
    if (r,c) is not outside the maze:  
        if cell at (r,c) is not a wall:  
            if (r,c) is unvisited:  
                if treasure is at (r,c), terminate the entire search  
                put (r,c) into Q and mark (r,c) as visited  
 
define BFS(startr,startc):  
    initialize an empty queue Q  
    put (startr,startc) into Q and mark (startr,startc) as visited  
    while Q is not empty:  
        get (r,c) from the queue  
        for each (nr,nc) in (r,c+1), (r-1,c), (r,c-1), (r+1,c):  
            process_cell(nr,nc)

Input specification

The input file for this problem contains a reference C++ implementation of Mark’s solution. The variable size in this implementation must exceed k.

Output specification

On the first line output two integers separated by a single space: number of rows r and number of columns c. Then output r rows with c characters in each – the map of your maze.

Example

A syntactically valid submission with maximum queue size 4.
3 4
....
.S#.
..#T