Problem solution · Java

CCC 2001 S3 - Strategic Bombing

CCC 2001 S3 - Strategic Bombing: 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
100 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 2001 S3 - Strategic Bombing, 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

  • 100 lines of Java from the credited upstream file ccc01s3.java.
  • The implementation visibly relies on sequence storage.
  • 8 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 2001 S3 - Strategic Bombing · JavaJava
Use this to learn the idea, then write your own version.
// CCC 2001// Problem J5S3: Stategic bombing // Points A, B, C, D, E, etc are joined by a series of roads.// find a single road that will cut A from B (find all such single roads) // solution method: get the roads//                  fill 2d adjacency matrix will all but 1 road//                  using Warshall's, check if A is connected to B//                  repeat leaving out each road in turn. // File I/O// Input: a series of roads, given as a pair of letters//        (eg DF means D and F are connected).//        "**" denotes end of data// Ouput: list of roads to bomb and a count.  import java.awt.*;import hsa.*; public class J5S3Bombing{    static Console c;     public static void main (String [] args)    {	c = new Console ();	boolean [] [] adj = new boolean [26] [26];	String [] roads = new String [1000];	String s;	int a, b;	int n, count; 	TextInputFile fi = new TextInputFile ("bomb5.in");	TextOutputFile fo = new TextOutputFile ("bomb5.out"); 	// get the roads	s = fi.readString ();	n = 0;	count = 0;	while (!s.equals ("**"))	{	    roads [n++] = s;	    s = fi.readString ();	}	fi.close ();	for (int i = 0 ; i < n ; i++)	{	    // reset adj matrix	    for (int k = 0 ; k < 26 ; k++)		for (int j = 0 ; j < 26 ; j++)		{		    if (k == j)			adj [k] [j] = true;		    else			adj [k] [j] = false;		}	    //load the adj matrix with the ith road missing	    for (int j = 0 ; j < n ; j++)	    {		if (j != i)		{		    a = roads [j].charAt (0) - 'A';		    b = roads [j].charAt (1) - 'A';		    adj [a] [b] = true;		    adj [b] [a] = true;		}	    } 	    // Do transitive closure	    warshalls (adj); 	    if (!adj [0] [1])	    {		fo.println (roads [i]);		c.println (roads [i]);		count++;	    }	}	fo.println ("There are " + count + " disconnecting roads.");	c.println ("There are " + count + " disconnecting roads.");	fo.close ();    }      // Warshall's classic transitive closure algorithm    static public void warshalls (boolean [] [] a)    {	for (int y = 0 ; y < 26 ; y++)	    for (int x = 0 ; x < 26 ; x++)		if (a [x] [y])		    for (int j = 0 ; j < 26 ; j++)			if (a [y] [j])			    a [x] [j] = true;    }}   

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 ↗