Competitive/Collaborative Programming Class

ICPC Computer Programming Contest Prep

Problem Solving in Computer Science

Spring 2015 -- CSC 2700 Section 01

[Isaac's Home Page ]  [Mailing List ]  [Class Page ]  [Normal ]  

week06/i/FearfulSymmetry.java

/*
 *  Author: Son Pham
 *    Date: March 1, 2013
 * Purpose: Solves this problem using the straightforward approach.
 *          I know there's an easier way, but this approach was alot more fun.
 *          I wanted to see what the patterns looked like.
 * Problem: Deloitte -  Problem 6: Fearful Symmetry
 * Comment: I'm 99% sure this is correct. It ran and worked for the sample input.
 *          The code would be easier to read if I replaced the do while with a
 *          for loop. Oh well. What's done is done.
 */
import java.util.Scanner;

public class FearfulSymmetry {

    private static final int ROWS = 10;
    private static final int COLUMNS = 10;
    private static char[][] pattern = new char[ROWS][COLUMNS];
    
    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        String read = "";
        while(true){
            read = reader.nextLine();
            if(read.equals("@"))
                break;
            else if(read.length() < 1){
                continue;
            }
            int i = 0;
            do{
                if(i > 0)
                    read = reader.nextLine();
                String[] knits = read.split("\\s+");
                int pIndex = 0;
                for(int j = 0; j < knits.length; j++){
                    char stitch = knits[j].charAt(0);
                    int numStitches = Integer.parseInt(knits[j].substring(1));
                    for(int k = 0; k < numStitches; k++){
                        if(stitch == 'k'){
                            pattern[i][pIndex] = '-';
                        }
                        else{
                            pattern[i][pIndex] = 'X';
                        }
                        ++pIndex;
                    }
                }
            }while(++i < 10);
            //printPattern();
            boolean horizontal = checkHorizontal();
            boolean vertical = checkVertical();
            if(horizontal){
                if(vertical){
                    System.out.println("B");
                }
                else
                    System.out.println("H");
            }
            else if(vertical){
                System.out.println("V");
            }
            else
                System.out.println("A");
        }
    }
    
    public static boolean checkHorizontal(){
        for(int i = 0, j = 9; i < 5; ++i,--j){
            for(int k = 0; k < COLUMNS; ++k){
                if(pattern[i][k] != pattern[j][k])
                    return false;
            }
        }
        return true;
    }
    
    public static boolean checkVertical(){
        for(int i = 0, j = 9; i < 5; ++i,--j){
            for(int k = 0; k < ROWS; ++k){
                if(pattern[k][i] != pattern[k][j])
                    return false;
            }
        }
        return true;
    }
    
    public static void printPattern(){
        for(int i = 0; i < ROWS; i++){
            for(int j = 0; j < COLUMNS; j++){
                System.out.print(pattern[i][j]+" ");
            }
            System.out.println();
        }
    }
}

The statements and opinions included in these pages are those of only. Any statements and opinions included in these pages are not those of Louisiana State University or the LSU Board of Supervisors.
© 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015