Problem solution · Java

CCC 2002 S3 - Blindfold

CCC 2002 S3 - Blindfold: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
113 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

Direct simulation

For CCC 2002 S3 - Blindfold, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 113 lines of Java from the credited upstream file ccc02s3.java.
  • The implementation visibly relies on sequence storage.
  • 9 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

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 2002 S3 - Blindfold · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2002// Problem S3J5 Blindfold // given a grid with obstacles and a series of moves (R,L,F)// determine the possible finish points. // clear is '.' and obstacle is 'X' // brute force method. Take every starting posititon and any of the 4 directions// and see where you get. If still okay, mark that spot with a '*' // file input: r and col of grid given// then the grid and finally a # and the moves.// output the grid with the '*' locations. import java.awt.*;import hsa.*; public class S3J5Blindfold{    static Console cc;    static char [] [] grid;    static int r, c, n;    static char [] m;     public static void main (String [] args)    {	cc = new Console (); 	String line; 	TextInputFile fi = new TextInputFile ("blind4.in");	TextOutputFile fo = new TextOutputFile ("blind4.out"); 	// read grid	r = fi.readInt ();	c = fi.readInt ();	grid = new char [r] [c];	for (int i = 0 ; i < r ; i++)	{	    line = fi.readLine ();	    for (int j = 0 ; j < c ; j++)		grid [i] [j] = line.charAt (j);	} 	//read moves	n = fi.readInt ();	m = new char [n];	for (int i = 0 ; i < n ; i++)	    m [i] = (fi.readLine ()).charAt (0); 	// check each square and direction	for (int i = 0 ; i < r ; i++)	    for (int j = 0 ; j < c ; j++)		for (int d = 0 ; d < 360 ; d = d + 90)		    check (i, j, d); 	// print grid	for (int i = 0 ; i < r ; i++)	{	    for (int j = 0 ; j < c ; j++)		fo.print (grid [i] [j]);	    fo.println ();	}	fi.close ();	fo.close ();    }      // this updates the global grid 2D array with a "*" if    // the starting location (i,j) and direction d results in    // a good square.    // quit the process if you go off the square or hit a wall    public static void check (int i, int j, int dir)    {	int pi, pj, k;	pi = i;	pj = j;	k = 0;	while (pi >= 0 && pi < r && pj >= 0 && pj < c &&		(grid [pi] [pj] == '.' || grid [pi] [pj] == '*') &&		k < n)	{	    if (m [k] == 'R')	    {		dir = dir - 90;		if (dir < 0)		    dir = 270;	    }	    else if (m [k] == 'L')		dir = (dir + 90) % 360;	    else if (m [k] == 'F')	    {		if (dir == 0)		    pj = pj + 1;		else if (dir == 180)		    pj = pj - 1;		else if (dir == 90)		    pi = pi - 1;		else		    pi = pi + 1;	    }	    k++;	} 	if (k >= n && pi >= 0 && pi < r && pj >= 0 && pj < c &&		grid [pi] [pj] == '.')	    grid [pi] [pj] = '*';     }}  

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 ↗