Problem solution · Java

CCC 2008 S3 - Maze

CCC 2008 S3 - Maze: a Java solution using depth-first search. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Depth-first search
Source
CCCSolutions
Length
97 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Depth-first search

For CCC 2008 S3 - Maze, the implementation follows one branch at a time, making it suitable for components, trees, backtracking, or dependency exploration.

  1. Define the state carried into one recursive or stack frame.
  2. Mark or choose the current state before exploring children.
  3. Combine child results or undo the choice when the branch finishes.

Code notes

  • 97 lines of Java from the credited upstream file ccc08s3.java.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Count unique states for graph traversal; for backtracking, count the branching factor and maximum depth.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2008 S3 - Maze · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2008//// S3: Maze//// This is a recursion 2D array problem.// (a relatively straight forward algorithm without tricks)//// grid is a 2d array of ints holding the number of steps// needed to get to this space. It is initialized to a// large number 99999//// maze is the 2d array of char, holding the maze itself.//// traverse puts the value of x into location r,c//// the main starts by putting a 1 in 0,0.// from there, depending on what is in the maze itself// traverse is called for the squares next to r,c with x+1//// There are a few points. Obviously r,c must be in bounds.// The maze can't be a '*' at r,c (if it is, set grid r,c to -1)// The other thing is x only goes into grid(r,c) if x is less than// what's already there (it is a MINIMUM we're after.)//// traverse will go everywhere it is possible to go,// so back in the main the answer is in bottom right of grid.// If the value there is -1 or the original 99999, -1 is printed.// import java.awt.*;import hsa.*; public class CCC2008S3Maze{    static Console c;    static char[] [] maze;    static int[] [] grid;    static int row, col;     public static void traverse (int r, int c, int x)    {	if (r >= 0 && r < row && c >= 0 && c < col)	    if (grid [r] [c] >= 0 && x < grid [r] [c])		if (maze [r] [c] == '*')		    grid [r] [c] = -1;		else if (maze [r] [c] == '+')		{		    grid [r] [c] = x;		    traverse (r - 1, c, x + 1);		    traverse (r + 1, c, x + 1);		    traverse (r, c - 1, x + 1);		    traverse (r, c + 1, x + 1);		}		else if (maze [r] [c] == '|')		{		    grid [r] [c] = x;		    traverse (r - 1, c, x + 1);		    traverse (r + 1, c, x + 1);		}		else if (maze [r] [c] == '-')		{		    grid [r] [c] = x;		    traverse (r, c - 1, x + 1);		    traverse (r, c + 1, x + 1);		}    }      public static void main (String[] args)    {	int t;	c = new Console ();	TextInputFile f = new TextInputFile ("s3.5.in");	t = f.readInt ();	for (int k = 0 ; k < t ; k++)	{	    row = f.readInt ();	    col = f.readInt ();	    maze = new char [row] [col];	    grid = new int [row] [col];	    for (int i = 0 ; i < row ; i++)		for (int j = 0 ; j < col ; j++)		    maze [i] [j] = f.readChar ();	    for (int i = 0 ; i < row ; i++)		for (int j = 0 ; j < col ; j++)		    grid [i] [j] = 99999;	    traverse (0, 0, 1);	    if (grid [row - 1] [col - 1] > 0 && grid [row - 1] [col - 1] < 99999)		c.println (grid [row - 1] [col - 1]);	    else		c.println (-1);	}    }}   

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗