/* * Solution Template for Yet Another Lights Problem * * Australian Informatics Olympiad 2023 * * This file is provided to assist with reading and writing of the input * files for the problem. You may modify this file however you wish, or * you may choose not to use this file at all. */ import java.io.*; class Solution { /* R is the number of rows. */ private static int R; /* C is the number of columns. */ private static int C; /* grid contains the initial state of each light. */ private static char grid[][] = new char[205][205]; /* * answer should contain the number of cross-flips in your sequence, or -1 * if it is impossible to turn all the lights on. */ private static int answer; /* * cross_flips should contain your sequence of cross-flips. For example, if * your first cross-flip is (2, 3), then you should set cross_flips[0][0] = * 2 and cross_flips[0][1] = 3. */ private static int cross_flips[][] = new int[40005][2]; /* * Read the next token from the input file. * Tokens are separated by whitespace, i.e., spaces, tabs and newlines. * If end-of-file is reached then an empty string is returned. */ private static String readToken(BufferedReader in) throws IOException { StringBuffer ans = new StringBuffer(); int next; /* Skip any initial whitespace. */ next = in.read(); while (next >= 0 && Character.isWhitespace((char)next)) next = in.read(); /* Read the following token. */ while (next >= 0 && ! Character.isWhitespace((char)next)) { ans.append((char)next); next = in.read(); } return ans.toString(); } public static void main(String[] args) throws IOException { /* Open the input and output files. */ BufferedReader input_file = new BufferedReader(new FileReader( "yalpin.txt")); BufferedWriter output_file = new BufferedWriter(new FileWriter( "yalpout.txt")); /* * Read the values of R, C and the initial state of each light from the * input file. */ R = Integer.parseInt(readToken(input_file)); C = Integer.parseInt(readToken(input_file)); for (int i = 0; i < R; i++) { grid[i] = readToken(input_file).toCharArray(); } /* TODO: This is where you should compute your solution. */ /* * You should store the number of cross-flips in your sequence, or -1 * if it is impossible, into the variable answer. If it is possible, * then your sequence of cross-flips should be stored in the array * cross_flips, using indices 0 to answer-1. */ /* Write the answers to the output file. */ output_file.write(answer + "\n"); if (answer != -1) { for (int i = 0; i < answer; i++) { output_file.write( cross_flips[i][0] + " " + cross_flips[i][1] + "\n"); } } /* Finally, close the input/output files. */ input_file.close(); output_file.close(); } }