Imagine there is a 7x7 grid of lights, and only the
middle in the grid is on.
The lights are wired such that when you flip the switch for one light (from on to off or off to on) the others next to it (not diagonally) flip as well.
Using this weird wiring of lights, what is the fewest number of switch changes it takes to turn all the lights off, and which lights should you switch?
Note: Assume all the switches work in the manner explained, and there is one switch for each of the lights.
clearvars,clc
global rowChoice lights switches highrow
rowChoice={};
for k=0:7
c=nchoosek(2:8,k);
for i=1:size(c,1);
rowChoice{end+1}=c(i,:);
end
end
rowChoice;
lights=zeros(9,9); lights(5,5)=1;
switches=zeros(9,9);
highrow=0;
addon(2); % row and column numbers are 1 higher to provide a surrounding buffer
highrow
function addon(row)
global rowChoice lights switches highrow
if row>highrow % highrow was for debugging
highrow=row;
end
for i=1:length(rowChoice)
saveLights=lights; saveSwitches=switches;
w=rowChoice{i};
switches(row,:)=0;
for j=1:length(w)
switches(row,w(j))=1;
for c=w(j)-1:2:w(j)+1
lights(row,c)=xor(lights(row,c),1);
end
for r=row-1:row+1
lights(r,w(j))=xor(lights(r,w(j)),1);
end
end
if row>2
if ~isequal(lights(row-1,2:8),zeros(1,7))
lights=saveLights; switches=saveSwitches;
continue
end
end
if row==8
if ~isequal(lights(row,2:8),zeros(1,7))
lights=saveLights; switches=saveSwitches;
continue
end
disp(switches(2:8,2:8))
disp(' ')
disp(lights(2:8,2:8))
disp(' ')
disp(sum(switches(2:8,2:8),'all'))
disp(' ')
else
addon(row+1)
end
lights=saveLights; switches=saveSwitches;
end
end
finds
0 0 0 1 0 0 0
0 0 1 1 1 0 0
0 1 0 0 0 1 0
1 1 0 1 0 1 1
0 1 0 0 0 1 0
0 0 1 1 1 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
17
showing the 17 flipped switches on top and a confirmation of all the lights off below that.
|
Posted by Charlie
on 2022-10-31 10:31:43 |