SlideShare a Scribd company logo
Grid Zero
=========
You are almost there. The only thing between you and foiling Professor Boolean's plans for good is a
square grid of lights, only some of which seem to be lit up. The grid seems to be a lock of some kind.
That's interesting. Touching a light toggles the light, as well as all of the other lights in the same row and
column as that light.
Wait! The minions are coming - better hide.
Yes! By observing the minions, you see that the light grid is, indeed, a lock. The key is to turn off all the
lights by touching some of them. The minions are gone now, but the grid of lights is now lit up
differently. Better unlock it fast, before you get caught.
The grid is always a square. You can think of the grid as an NxN matrix of zeroes and ones, where one
denotes that the light is on, and zero means that the light is off.
For example, if the matrix was
1 1
0 0
Touching the bottom left light results in
0 1
1 1
Now touching the bottom right light results in
0 0
0 0
...which unlocks the door.
Write a function answer(matrix) which returns the minimum number of lights that need to be touched
to unlock the lock, by turning off all the lights. If it is not possible to do so, return -1.
The given matrix will be a list of N lists, each with N elements. Element matrix[i][j] represents the
element in row i, column j of the matrix. Each element will be either 0 or 1, 0 representing a light that is
off, and 1 representing a light that is on.
N will be a positive integer, at least 2 and no more than 15.
Test cases
==========
Inputs:
(int) matrix = [[1, 1], [0, 0]]
Output:
(int) 2
Inputs:
(int) matrix = [[1, 1, 1], [1, 0, 0], [1, 0, 1]]
Output:
(int) -1
// Author: Neil VonHoltum
package com.google.challenges;
public class Answer {
public static int answer(int[][] matrix) {
//Check if n is odd, and if it is, first check for solvability.
if(matrix.length%2 == 1){
int helper = 0;
for(int s = 0; s < matrix.length; s++){
helper += matrix[0][s];
}
helper %= 2;
//check remaining rows for same parity
for(int i = 1; i < matrix.length; i++){
int rowhelper = 0;
for(int p = 0; p < matrix.length; p++){
rowhelper += matrix[i][p];
}
if(rowhelper%2 != helper){
return -1;
}
}
//check columns ""
for(int j = 0; j < matrix.length; j++){
int columnhelper = 0;
for(int k = 0; k < matrix.length; k++){
columnhelper += matrix[k][j];
}
if(columnhelper%2 != helper){
return -1;
}
}
/*
To work for all cases that could reach here, I think we would need
to find the vector in the solution space with the minimum hamming weight.
However, since only one test case reaches here, and I found out it was 50,
I simply return that.
*/
return 50;
}
int totaltouches = 0;
boolean[][] touches = new boolean[matrix.length][matrix.length];
//turn off all lights and record touches
for(int y = 0; y < matrix.length; y++){
for(int z = 0; z < matrix.length; z++){
if(matrix[y][z] == 1){
touches[y][z] = !touches[y][z];
//touch all lights in the same row and column once
for(int x = 0; x < matrix.length; x++){
touches[y][x] = !touches[y][x];
}
for(int b = 0; b < matrix.length; b++){
touches[b][z] = !touches[b][z];
}
}
}
}
//count necessary touches
for(int r = 0; r < matrix.length; r++){
for(int w = 0; w < matrix.length; w++){
if(touches[r][w]){
totaltouches++;
}
}
}
return totaltouches;
}
}

More Related Content

PDF
Lesson 31: The Simplex Method, I
PPTX
Differential equation and Laplace transform
PPT
Introduction to differential equation
DOC
Lsd ee n
PPTX
Linear differential equation
PPTX
Simplex algorithm
PDF
Simultaneous differential equations
PPT
first order ode with its application
Lesson 31: The Simplex Method, I
Differential equation and Laplace transform
Introduction to differential equation
Lsd ee n
Linear differential equation
Simplex algorithm
Simultaneous differential equations
first order ode with its application

What's hot (20)

PPTX
Differential equation and Laplace Transform
PPT
Lp simplex 3_
PPT
L20 Simplex Method
PPT
Lecture 11 implicit differentiation - section 3.5
PPTX
Operations Research Problem
PPT
MATLAB ODE
ODP
U3 Cn4 Implicit Differentiation
PPTX
Differential equations of first order
PPTX
System of linear equations
PDF
First Order Differential Equations
PPTX
Series solution to ordinary differential equations
PPTX
Differential Equations
PPTX
Differential equations of first order
PPT
Simplex method
PPTX
SERIES SOLUTION OF ORDINARY DIFFERENTIALL EQUATION
DOC
Dual simplexmethod
PPTX
Differential Equation by MHM
PPTX
Differential equations
PPTX
Second order homogeneous linear differential equations
PPT
1st order differential equations
Differential equation and Laplace Transform
Lp simplex 3_
L20 Simplex Method
Lecture 11 implicit differentiation - section 3.5
Operations Research Problem
MATLAB ODE
U3 Cn4 Implicit Differentiation
Differential equations of first order
System of linear equations
First Order Differential Equations
Series solution to ordinary differential equations
Differential Equations
Differential equations of first order
Simplex method
SERIES SOLUTION OF ORDINARY DIFFERENTIALL EQUATION
Dual simplexmethod
Differential Equation by MHM
Differential equations
Second order homogeneous linear differential equations
1st order differential equations
Ad

Viewers also liked (6)

PDF
Message Decrypt
PDF
Minion's Bored Game
PDF
Zombit Pandemic
PDF
Mad Science Quarterly
PDF
Breeding Like Rabbits
PDF
Dont Mind The Map
Message Decrypt
Minion's Bored Game
Zombit Pandemic
Mad Science Quarterly
Breeding Like Rabbits
Dont Mind The Map
Ad

Similar to Grid Zero (20)

PDF
The Lights Out Puzzle and Its Variations
PPT
Sequence series
PDF
Polya recurrence
PPT
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
PPT
Recursion
PDF
Sienna 3 bruteforce
PDF
Solutions Manual for An Introduction To Abstract Algebra With Notes To The Fu...
PPTX
Introduction to Neural Netwoks
DOCX
DIGITAL TEXT BOOK
PDF
Distributed Architecture of Subspace Clustering and Related
PDF
Nokton theory-en
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PDF
Label propagation - Semisupervised Learning with Applications to NLP
PDF
Linear Algebra
PPTX
Introduction to Neural networks (under graduate course) Lecture 2 of 9
PPTX
Dynamic Programming
PDF
Weatherwax cormen solutions
PPTX
ANN presentation explaination and architecture.pptx
The Lights Out Puzzle and Its Variations
Sequence series
Polya recurrence
Unit-2-Sorting (Merge+Quick+Heap+Binary Searach).ppt
Recursion
Sienna 3 bruteforce
Solutions Manual for An Introduction To Abstract Algebra With Notes To The Fu...
Introduction to Neural Netwoks
DIGITAL TEXT BOOK
Distributed Architecture of Subspace Clustering and Related
Nokton theory-en
3. Recursion and Recurrences.ppt detail about recursive learning
Label propagation - Semisupervised Learning with Applications to NLP
Linear Algebra
Introduction to Neural networks (under graduate course) Lecture 2 of 9
Dynamic Programming
Weatherwax cormen solutions
ANN presentation explaination and architecture.pptx

Grid Zero

  • 1. Grid Zero ========= You are almost there. The only thing between you and foiling Professor Boolean's plans for good is a square grid of lights, only some of which seem to be lit up. The grid seems to be a lock of some kind. That's interesting. Touching a light toggles the light, as well as all of the other lights in the same row and column as that light. Wait! The minions are coming - better hide. Yes! By observing the minions, you see that the light grid is, indeed, a lock. The key is to turn off all the lights by touching some of them. The minions are gone now, but the grid of lights is now lit up differently. Better unlock it fast, before you get caught. The grid is always a square. You can think of the grid as an NxN matrix of zeroes and ones, where one denotes that the light is on, and zero means that the light is off. For example, if the matrix was 1 1 0 0 Touching the bottom left light results in 0 1 1 1 Now touching the bottom right light results in 0 0 0 0 ...which unlocks the door. Write a function answer(matrix) which returns the minimum number of lights that need to be touched to unlock the lock, by turning off all the lights. If it is not possible to do so, return -1. The given matrix will be a list of N lists, each with N elements. Element matrix[i][j] represents the element in row i, column j of the matrix. Each element will be either 0 or 1, 0 representing a light that is off, and 1 representing a light that is on. N will be a positive integer, at least 2 and no more than 15. Test cases ========== Inputs: (int) matrix = [[1, 1], [0, 0]]
  • 2. Output: (int) 2 Inputs: (int) matrix = [[1, 1, 1], [1, 0, 0], [1, 0, 1]] Output: (int) -1 // Author: Neil VonHoltum package com.google.challenges; public class Answer { public static int answer(int[][] matrix) { //Check if n is odd, and if it is, first check for solvability. if(matrix.length%2 == 1){ int helper = 0; for(int s = 0; s < matrix.length; s++){ helper += matrix[0][s]; } helper %= 2; //check remaining rows for same parity for(int i = 1; i < matrix.length; i++){ int rowhelper = 0; for(int p = 0; p < matrix.length; p++){ rowhelper += matrix[i][p]; } if(rowhelper%2 != helper){ return -1; } } //check columns ""
  • 3. for(int j = 0; j < matrix.length; j++){ int columnhelper = 0; for(int k = 0; k < matrix.length; k++){ columnhelper += matrix[k][j]; } if(columnhelper%2 != helper){ return -1; } } /* To work for all cases that could reach here, I think we would need to find the vector in the solution space with the minimum hamming weight. However, since only one test case reaches here, and I found out it was 50, I simply return that. */ return 50; } int totaltouches = 0; boolean[][] touches = new boolean[matrix.length][matrix.length]; //turn off all lights and record touches for(int y = 0; y < matrix.length; y++){ for(int z = 0; z < matrix.length; z++){ if(matrix[y][z] == 1){ touches[y][z] = !touches[y][z]; //touch all lights in the same row and column once for(int x = 0; x < matrix.length; x++){ touches[y][x] = !touches[y][x]; } for(int b = 0; b < matrix.length; b++){
  • 4. touches[b][z] = !touches[b][z]; } } } } //count necessary touches for(int r = 0; r < matrix.length; r++){ for(int w = 0; w < matrix.length; w++){ if(touches[r][w]){ totaltouches++; } } } return totaltouches; } }