Python algorithm problem record (2)

Day 2 lights out problem

The rule is that 0 means that the state of the light is on, 1 means that the state of the light is off, and then there is a switch matrix 1 that means pressing 0 means no operation, if the original light is on, the light will be off after pressing it, and vice versa.
It's just that pressing the switch does not only operate the pressed light, but also affects the state of the lights around it. That is, for the middle light, it will cause 5 (up, down, left, and itself) to change the state of the lights. There are only three state changes for the lights on the corners, and four states for the lights on the sides.
Now suppose that there is a matrix of 5*6 lamps that stores the status of the lamps, and then there is a switch matrix that stores the corresponding operations for this batch of lamps. Ask which switches should be pressed for the status of a certain batch of lamps. , will make all the lights go out.

specific ideas

Since the operation of the lights in the middle is different from the four corners and the lights on the sides, we need to expand the 5*6 matrix by the 6*8 matrix, which is the same for all lights, and the switch matrix also changes. It is 6*8.
We need to use a status matrix to store the initial state of the lamps, which requires user input and then need to use another matrix to store the corresponding operations on this batch of lamps;
we can think of it this way, the initial state of a given lamp is for each lamp. Once judged, we only need to operate the switch status of the first row of lights. After the first row of lights is operated, if the first row is to be completely turned off, then the status of the second row of lights switches is determined. After the operation of the second row of lights is completed, the To make the lights in the second row go out, we need to operate the lights in the third row, that is, the switch status of the lights in the third row is determined again, and so on.
That is, as long as the switch state of the first row is given, the switch state of the other rows is determined (because our goal is to turn off each row by operating the switch)

For the last row of lights, after the switch status of the last row of lights is determined, we determine whether the switch status of the remaining rows determined by the switch status of the first row is reasonable by judging whether the lights in the last row are all off. If it is not all off, it means that the first row is selected The switch state is unreasonable and needs to be re-selected.
The selection of the switch state of the first line here is based on the binary rules. I did not write this program well. I will change it later when I have an idea.,,,,

Then go directly to the code:

# -*- coding: utf-8 -*-
"""
Created on Mon Apr 23 16:29:59 2018

@author: xuanxuan
"""

#这一题主要求解熄灯问题
#状态矩阵 0表示灯亮 1表示灯灭;
#按压矩阵 0表示不按压 1表示对应位置的灯按下去
#所以要使得灯是灭的 需要该灯的状态值+周围可以影响到它的press值 加起来是1,3,, 奇数才可以

import numpy as np

def get_status():
    status=np.mat(np.zeros((6,8)))  #初始化该状态矩阵 主要是前面用不到的写0就行

    for i in range(1,6):
        for j in range(1,7):
            status[i,j]=eval(input("please input:"))  #主要是对初始化的矩阵 重新赋值 该status矩阵表示灯的最初始状态

    print(status)

    return status
#get_status()

#负责不断的更新press矩阵的第一行
def update_firstpress(status):
    press=np.mat(np.zeros((6,8)))
    value=[0]*8   #初始化press矩阵的第一行,主要是方便更新press第一行所写的
    while(judge(status,press)==False):
        value[0]+=1
        if value[0]>=2:
            value[0]=0
            value[1]+=1
            if value[1]>=2:
                value[1]=0
                value[2]+=1
                if value[2]>=2:
                    value[2]=0
                    value[3]+=1
                    if value[3]>=2:
                        value[3]=0
                        value[4]+=1
                        if value[4]>=2:
                            value[4]=0
                            value[5]+=1
                            if value[5]>=2:
                                value[5]=0
                                value[6]+=1
                                if value[6]>=2:
                                    value[6]=0
                                    value[7]+=1
        press[1]=np.mat(value)




#根据状态矩阵 和press矩阵的第一行 不断生成后续的press 下一行的perss主要是为了灭掉上一行的灯 
#然后根据最后一行的状态以及最后一行press按压之后的效果能否使得最后一行的灯灭掉 
#来判断该press矩阵是否对现有的灯状态 全部灭掉

#负责根据press矩阵的第press[1]行(当然需要知道press[0]行)来更新press矩阵其余各行
def judge(status,press):

    for i in range(1,5):
        for j in range(1,7):
            if (status[i,j]+press[i-1,j]+press[i,j-1]+press[i,j]+press[i,j+1])%2==1:  
                press[i+1,j]=0  #也就是对于该灯来说 其状态 以及周围灯(不包括下一行的 也就是位于其正下方的灯)对其的影响已经使得该灯灭了   所以下一行的灯 的press 一定是为0 就是不操作了不在按压
            else:
                press[i+1,j]=1
    #以上就对传入的press矩阵不改变0 1 行的情况下  其实就是根据第[1]行不断的更新其余矩阵

    for k in range(1,8):
        if (status[5,j]+press[4,j]+press[5,j-1]+press[5,j]+press[5,j+1])%2==0:
            return False
    print("满足条件的press矩阵为:\n{}".format(press))
    return True

if __name__=="__main__":
    status=get_status()
    update_firstpress(status)    


I really thought about the realization of this question for a long time, mainly the binary manual generation, and finally I didn't think of it,,, I just used a stupid method,,, woo woo,,,

Come on tomorrow too~~~

above.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734236&siteId=291194637