import java.util.Scanner; /** * Replace this JavaDoc comment for the purpose of this class */ public class Solution { /** * Gets data from standard input */ private Scanner input = null; /** * Start the execution of the solution * @param args Command line arguments */ public static void main(String args[]) { Solution solution = new Solution(); solution.run(); } /** * Run the solution. This method is called from main() */ public void run() { // Create object to read data from standard input this.input = new Scanner(System.in); int rows = input.nextInt(); int columns = input.nextInt(); // input.next(); Matrix matrix = new Matrix(); matrix.read(input, rows, columns); if(matrix.areRowsValid()) { matrix.processResults(); System.out.printf("%d ", matrix.getBestOption()); matrix.printColumnsWithBestOption(); } // Close the standard input this.input.close(); } } class Matrix { private int matrix[][] = null; private int results[] = null; private int bestOption = -1; private int rows = -1; private int columns = -1; private int MAX_VALUE = Integer.MAX_VALUE; public Matrix() { } public void read(Scanner input, int Arows, int Acolumns) { this.rows = Arows; this.columns = Acolumns; this.matrix = new int[rows][columns]; // System.err.println("1"); // System.err.printf("(%d, %d) (%d, %d) %n", rows, columns, this.matrix.length, this.matrix[0].length); for(int row = 0; row < this.matrix.length; row++) { for(int col = 0; col < this.matrix[row].length; col++) { this.matrix[row][col] = input.nextInt(); // System.err.printf("Succesfuly on (%d, %d) %n", row, col); } } // System.err.println("2"); // dictate(); } public void dictate() { for(int row = 0; row < this.matrix.length; row++) { for(int col = 0; col < this.matrix[row].length; col++) { System.err.print(this.matrix[row][col] + " "); } System.err.println(); } } public boolean areRowsValid() { boolean validRows = true; for(int row = 0; row < this.matrix.length; row++) { int ceroes = 0; for(int col = 0; col < this.matrix[row].length; col++) { if(this.matrix[row][col] == 0) ceroes++; } if(ceroes == 0) { System.out.printf("Invalid row %d%n", row + 1); validRows = false; } } return validRows; } public void processResults() { results = new int[columns]; for(int col = 0; col < this.matrix[0].length; col++) { for(int row = 0; row < this.matrix.length; row++) { if(matrix[row][col] == 1) { int minLeft = calculateLeftMovements(col, row); int minRight = calculateRightMovements(col, row); int minimumMoves = Math.min(minLeft, minRight); results[col] += minimumMoves; if(col == 5) System.err.printf("(%d, %d) returns L%-10d R%-10d %n", row, col, minLeft, minRight); } else { if(col == 5) System.err.printf("(%d, %d) returns L%-10d R%-10d %n", row, col, 0, 0); } } } //dictate(); //printResults(); } public void printResults() { for(int index = 1; index < results.length; index++) System.err.printf("--"); System.err.printf("-%n"); for(int index = 0; index < results.length; index++) System.err.printf("%d ", results[index]); } public int calculateRightMovements(int column, int row) { int total = 1; int present = column + 1; boolean inLoop = true; while( (present < this.matrix[0].length) && inLoop ) { if(this.matrix[row][present] == 1) { present++; total++; } else // We find a vacant space, a 0 { inLoop = false; } } if(inLoop) { return MAX_VALUE; //largest int possible } else { return total; } } public int calculateLeftMovements(int column, int row) { int total = 1; int present = column - 1; boolean inLoop = true; while( (present > -1) && inLoop ) { if(this.matrix[row][present] == 1) { present--; total++; } else { inLoop = false; } } if(inLoop) { return MAX_VALUE; //largest int possible } else { return total; } } public int getBestOption() { /*int bigN = -1; for(int index = 0; index < results.length; index++) { if(results[index] > bigN) bigN = results[index]; }*/ this.bestOption = results[0]; for(int index = 1; index < results.length; index++) { if(results[index] < this.bestOption) this.bestOption = results[index]; } return this.bestOption; } public void printColumnsWithBestOption() { for(int index = 0; index < results.length; index++) { if(this.bestOption == results[index]) System.out.printf("%d ", index + 1); } } }