5*5的格子,初始状态灯全灭(可以通过init_light来定义任何初始状态),点任何一个格子,上下左右状态反转,目标状态灯全亮
rows = 7 cols = 7 light = [[0 for row in range(rows)] for col in range(cols)] press = [[0 for row in range(rows)] for col in range(cols)] def init_light(): for row in xrange(1,6): for col in xrange(1,6): light[row][col]=1 def printMatrix(light_press): for row in xrange(1,6): print light_press[row][1:6] def AllLightOff(): for col_1 in range(2): for col_2 in range(2): for col_3 in range(2): for col_4 in range(2): for col_5 in range(2): press[1][1]=col_1 press[1][2]=col_2 press[1][3]=col_3 press[1][4]=col_4 press[1][5]=col_5 flag = True for row in xrange(2,6): for col in xrange(1,6): press[row][col]=(light[row-1][col]+press[row-2][col] +press[row-1][col-1]+press[row-1][col]+press[row-1][col+1])%2 for col in xrange(1,6): if (press[5][col-1]+press[5][col]+press[5][col+1]+press[4][col]+light[5][col])%2 != 0 : flag = False break if flag: print "--------The right answer is :----------" printMatrix(press) return True if __name__ == "__main__": init_light() print "the init state is :" print "-------------------" printMatrix(light) AllLightOff()
时间: 2024-11-13 09:38:22