Answered step by step
Verified Expert Solution
Question
1 Approved Answer
A grid is presented with some squares filled in black and some filled in white, and some are unfilled (gray). Fill in the gray squares
A grid is presented with some squares filled in black and some filled in white, and some are unfilled (gray). Fill in the gray squares as black or white so that every row and every column has the same number of black and white squares, and there are never three or more squares of the same color next to each other in a row or column. We use a 2D array of integers for our grid and represent the two different filled in colors as the two different integer values 1 and 2, and unfilled squares as 0. Please implement the following method in the Unruly class: public static boolean solve(int[][] grid) Your code can assume that the array is full of 0, 1, and 2 values and is a square of even width and height. Your method should leave all the existing 1 and 2 values and try to fill in all the Os with 1 and 2 values so that every row and column has an equal number of 1 and 2 values and there is no run of three or more 1 or 2 values in a row or column. If there is such a solution, you should leave the grid filled in with a solution of all 1 and 2 values according to the rules and return the value true. If there is no solution, solve should return false. (HINT: Your code will probably restore the grid to the original state in that case.) If you wish, you may also use helper methods in your code. Make sure you follow and understand the "Recursive Backtracking General Strategy" described in class. For each recursive call, a "move" should correspond to filling in a square. Make sure that you stop yourself from making a move that would result in a rule violation. If you do this, your code should be fast enough for all the given examples. For each problem, your code will be given a time limit to prevent extremely slow solutions. (For the most complex boards, your code will have a time limit of 5 minutes on a relatively modern computer. For this assignment, you shouldn't need to find code to search for the "best" next move to make; this is more likely to lead to bugs in your code. Keep it simple! You should probably use loops instead of making your code "purely recursive" like RecursionIntro. A grid is presented with some squares filled in black and some filled in white, and some are unfilled (gray). Fill in the gray squares as black or white so that every row and every column has the same number of black and white squares, and there are never three or more squares of the same color next to each other in a row or column. We use a 2D array of integers for our grid and represent the two different filled in colors as the two different integer values 1 and 2, and unfilled squares as 0. Please implement the following method in the Unruly class: public static boolean solve(int[][] grid) Your code can assume that the array is full of 0, 1, and 2 values and is a square of even width and height. Your method should leave all the existing 1 and 2 values and try to fill in all the Os with 1 and 2 values so that every row and column has an equal number of 1 and 2 values and there is no run of three or more 1 or 2 values in a row or column. If there is such a solution, you should leave the grid filled in with a solution of all 1 and 2 values according to the rules and return the value true. If there is no solution, solve should return false. (HINT: Your code will probably restore the grid to the original state in that case.) If you wish, you may also use helper methods in your code. Make sure you follow and understand the "Recursive Backtracking General Strategy" described in class. For each recursive call, a "move" should correspond to filling in a square. Make sure that you stop yourself from making a move that would result in a rule violation. If you do this, your code should be fast enough for all the given examples. For each problem, your code will be given a time limit to prevent extremely slow solutions. (For the most complex boards, your code will have a time limit of 5 minutes on a relatively modern computer. For this assignment, you shouldn't need to find code to search for the "best" next move to make; this is more likely to lead to bugs in your code. Keep it simple! You should probably use loops instead of making your code "purely recursive" like Recursion
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started