import java.io.*; import java.util.*; /** * Use 2D prefix sums, Breadth-First-Search, and Binary Search. * * 1. Build a 2D prefix sum array to store the number of obstructions in the * rectangle from (0,0) to (i,j) for every i, j. This will be used together * with the inclusion-exclusion principle to determine the number of * obstructions in any rectangle from (r1,c1) to (r2,c2). * 2. Binary search on the robot size. * 3. For each robot size, determine if all unobstructed cells can be reached: * a. Iterate over all possible starting locations and check if the robot * fits there. If no valid starting location exists, the robot is too big. * b. Perform a breadth first search from the starting location, by shifting * the robot up/down by one row or column and tracking the top-left corners * of all reachable positions. * c. Build a new 2D prefix sum array of the visited top-left corners. This * will be used to determine if some position was reached that would cover * a particular cell. * d. Iterate over all unobstructed grid cells and check that at least one * position was reached that would cover the grid cell. * * @author Finn Lidbetter */ public class CleaningRobot_Finn { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] s = br.readLine().split(" "); int nRows = Integer.parseInt(s[0]); int nCols = Integer.parseInt(s[1]); int x = Integer.parseInt(s[2]); boolean[][] grid = new boolean[nRows][nCols]; for (int i=0; i rq = new LinkedList<>(); LinkedList cq = new LinkedList<>(); boolean startedBFS = false; for (int sr=0; sr 0) { int cr = rq.poll(); int cc = cq.poll(); for (int i=0; i<4; i++) { int nr = cr+dr[i]; int nc = cc+dc[i]; if (nr>=0 && nc>=0 && nr 0; } }