Problem solution · Java

CCC 2002 S4 - Bridge Crossing

CCC 2002 S4 - Bridge Crossing: 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
99 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 S4 - Bridge Crossing, 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

  • 99 lines of Java from the credited upstream file ccc02s4.java.
  • The implementation visibly relies on sequence storage.
  • 7 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 S4 - Bridge Crossing · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2002// Problem S4: Bridge Crossing // This uses a Dynamic Programming approach, due to Vassili Skarine.// the key is a best array: best[i] holds the best time for the first i// people. also group[i] holds the length of the group with i as the// last person. // essentially all the various combinations are tested in the following// order (assuming m = 3 for example)// a// ab// abc//// a b// a bc// a bcd//// ab c// ab cd// ab cde// ... import java.awt.*;import hsa.*; public class S4BridgeCrossingDP{     public static void main (String [] args)    {	TextInputFile fi = new TextInputFile ("bridge4.in");	TextOutputFile c = new TextOutputFile ("bridge4.out");	String [] name;	int [] time;	int m, n;	int totalTime = 0; 	// read info;	m = fi.readInt ();	n = fi.readInt ();	name = new String [n];	time = new int [n];	for (int i = 0 ; i < n ; i++)	{	    name [i] = fi.readLine ();	    time [i] = fi.readInt ();	}	fi.close (); 	// determine the best times	int [] best = new int [n + 1];	int [] group = new int [n + 1];	for (int i = 0 ; i < n + 1 ; i++)	{	    best [i] = 1000000;	    group [i] = -1;	}	best [0] = 0;	group [0] = 0;	for (int i = 0 ; i < n + 1 ; i++)	{	    int cur = 0;	    for (int j = 1 ; j <= m && i + j - 1 < n ; j++)	    {		cur = Math.max (cur, time [i + j - 1]);		if (best [i] + cur < best [i + j])		{		    best [i + j] = best [i] + cur;		    group [i + j] = j;		}	    }	} 	c.println ("Total Time: " + best [n]); 	// to get the groups, work backward thru group creating	// the dividing lines. Then use those lines (in reverse order)	// to print the solution.	int [] lines = new int [n + 1];	int k = n;	int x = 0;	while (group [k] != 0)	{	    lines [x++] = group [k];	    k = k - group [k];	}	int z = 0;	for (int i = x - 1 ; i >= 0 ; i--)	{	    for (int j = 0 ; j < lines [i] ; j++)		c.print (name [z++] + " ");	    c.println ();	} 	c.close ();    }} 

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 ↗