class solution(object): def solvenqueens(self,n): self.helper([-1]*n , 0 , n) def helper(self , columnPositions , rowIndex , n): if rowIndex == n: self.printSolution(columnPositions,n) return for column in range(n): columnPositions [rowIndex] = column if self.isValid(columnPositions,rowIndex): self.helper(columnPositions,rowIndex+1,n) def isValid (self, columnPositions,rowIndex): for i in range(rowIndex): if columnPositions[i] == columnPositions[rowIndex]: return False elif abs (columnPositions[i]-columnPositions[rowIndex])== rowIndex-i return False return True def printSolution (self,columnPosition,n): for row in range (n): line =="" for column in range (n): if columnPositions[row] == column: line+="Q" else: line+=". " print(line) print("\n") solution(),solvenqueens(8)
时间: 2023-06-20 11:10:08 浏览: 44
这段代码使用回溯法来解决八皇后问题,即在一个8x8的棋盘上摆放8个皇后,使得每个皇后都不会互相攻击(同一行、同一列、同一对角线上都没有另一个皇后)。
其中,`solvenqueens`函数调用了`helper`函数来搜索所有可能的解,并调用`printSolution`函数来输出解。
`isValid`函数用于判断当前放置的皇后是否合法,即是否和之前的皇后冲突。
最后,调用`solution()`来创建一个`solution`对象,然后调用`solvenqueens`函数来解决八皇后问题。
相关问题
pagehelper.helper-dialect
pagehelper.helper-dialect是指在application.properties文件中配置的pagehelper分页插件的数据库方言。根据提供的引用内容,pagehelper.helper-dialect的值应该是mysql。这意味着pagehelper分页插件将自动检测当前的数据库链接,并选择适合mysql数据库的分页方式。
怎么用python将下面代码中的dw,db改为私有属性import numpy as npimport matplotlib.pyplot as pltfrom HelperClass.DataReader_1_0 import *file_name = "../../data/ch04.npz"class NeuralNet_0_1(object): def __init__(self, eta): self.eta = eta self.w = 0 self.b = 0 def __forward(self, x): z = x * self.w + self.b return z def __backward(self, x,y,z): dz = z - y db = dz dw = x * dz return dw, db def __update(self, dw, db): self.w = self.w - self.eta * dw self.b = self.b - self.eta * db def train(self, dataReader): for i in range(dataReader.num_train): # get x and y value for one sample x,y = dataReader.GetSingleTrainSample(i) # get z from x,y z = self.__forward(x) # calculate gradient of w and b dw, db = self.__backward(x, y, z) # update w,b self.__update(dw, db) # end for def inference(self, x): return self.__forward(x)# end classdef ShowResult(net, dataReader): X,Y = dataReader.GetWholeTrainSamples() # draw sample data plt.plot(X, Y, "b.") # draw predication data PX = np.linspace(0,1,10) PZ = net.inference(PX) plt.plot(PX, PZ, "r") plt.title("Air Conditioner Power") plt.xlabel("Number of Servers(K)") plt.ylabel("Power of Air Conditioner(KW)") plt.show()if __name__ == '__main__': # read data sdr = DataReader_1_0(file_name) sdr.ReadData() # create net eta = 0.1 net = NeuralNet_0_1(eta) net.train(sdr) # result print("w=%f,b=%f" %(net.w, net.b)) # predication result = net.inference(1.346) print("result=", result) ShowResult(net, sdr)
将dw和db改为私有属性可以在它们的变量名前加上两个下划线"__",即将dw改为__dw,将db改为__db。修改后的代码如下:
import numpy as np
import matplotlib.pyplot as plt
from HelperClass.DataReader_1_0 import *
file_name = "../../data/ch04.npz"
class NeuralNet_0_1(object):
def __init__(self, eta):
self.eta = eta
self.w = 0
self.b = 0
self.__dw = 0
self.__db = 0
def __forward(self, x):
z = x * self.w + self.b
return z
def __backward(self, x, y, z):
dz = z - y
self.__db = dz
self.__dw = x * dz
return self.__dw, self.__db
def __update(self):
self.w = self.w - self.eta * self.__dw
self.b = self.b - self.eta * self.__db
def train(self, dataReader):
for i in range(dataReader.num_train):
x, y = dataReader.GetBatchTrainSamples(1)
z = self.__forward(x)
dw, db = self.__backward(x, y, z)
self.__update()