Locks in a grid problem
This is a simulation to an interesting problem about a lock. Which has n key slots. These slots are either vertical or horizontal. To open the lock these slots have to be in a particular configuration.
If we put the key in one slot and turn its orientation all the other slots in that particular row and column change their configuration. Is it possible to open this lock for a particular configuration of 16 slots ? If yes then how ?
"""The purpose of this is to simulate the problem of locks
where when you turn on the key in the grid all the other
keys in the same row and column also turn. I represent this
by 0 and 1
One can experiment and learn that if the grid is even by even
then we can change any particular bit by"""
n = 4
a=[]
sum = 0
# It prints the whole Grid of Locks
def printMatrix(val):
for i in range(n):
print(val[i])
# It flips the entries from 0 to 1 and 1 to 0
def flip(val):
if val == 0:
return 1
return 0
# It changes the entries in a given row and column
def ChangeRowChangeColumn(a,i,j):
a[i][j]=flip(a[i][j])
for k in range(n):
a[i][k]=flip(a[i][k])
for k in range(n):
a[k][j]=flip(a[k][j])
for i in range(1,n+1):
b=[]
for j in range(1,n+1):
b.append(1)
c = b[:]
sum= sum+n
a.append(c)
del b[:]
printMatrix(a)
flag=1
while(flag == 1):
row=int(input("Enter what row you want to change ?"))
col=int(input("Enter what column you want to change?"))
ChangeRowChangeColumn(a,row,col)
printMatrix(a)
flag = int(input("Enter 0 or 1 to continue"))
Labels: lock problem, Python
0 Comments:
Post a Comment
<< Home