LuckyGrid
2015 TCO 3A · 2015-04-08 · by cgy4ever
Problem Statement
Cat has decided to play a new pen-and-paper game on a grid. For the purpose of this game, a lucky number is any number in which each digit is either a 4 or a 7.
The game is played on a square grid. At the beginning of the game the player is presented with a partially filled grid: some of the cells contain the number 4, some contain the number 7, and all other cells are empty. The player must put either a 4 or a 7 into each of the empty cells. The player wins the game if she is able to fill in the empty cells in such a way that the sum of each row and also the sum of each column is a lucky number.
You are given a
If the player cannot win the game, return -1. If she can win the game, find and return the smallest possible sum of the grid at the end of a won game.
Constraints
- Let n be a number between 1 and 68, inclusive.
- grid will contain exactly n elements.
- Each element of grid will contain exactly n characters.
- Each character of each element of grid will be '4', '7', or '.'.
{"."}
Returns: 4
There are two ways to play this game: Either we put a 4 into the empty cell, or we put a 7 into the empty cell. In both cases we win the game. The answer is 4, because 4 is less than 7.
{"7"}
Returns: 7
{"........",
"........",
"........",
"........",
"........",
"........",
"........",
"........"}
Returns: 352
{".4.",
"7.7",
"4.."}
Returns: -1
The sum of the leftmost column will be either 4+7+4 = 15 or 7+7+4 = 18. Neither of these numbers is lucky.
{"774777..",
"..4.....",
"..7774..",
"..7..7..",
"..7..7..",
"..7..74.",
"..4..4..",
"........"}
Returns: 355
Submissions are judged against all 204 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class LuckyGrid with a public method int findMinimumSum(vector<string> grid) · 204 test cases · 2 s / 256 MB per case