#计算直线上的不可用位置 for m inrange(-10,10): #偏移量,随便取值 for k inrange(-10,10): #列 if -i <= m <= 4-i : deducted_position = [i+m,j] deducted_list.append(deducted_position) else:pass if -j <= k <= 4-j: #行 deducted_position = [i,j+k] deducted_list.append(deducted_position) #计算对角线上的不可用位置 ok for k inrange(-10,10):#主对角线 if -i <= k <= 4-i and -j <= k <= 4-j: p = k deducted_position = [i+p,j+p] deducted_list.append(deducted_position) for k inrange(-10,10):#副对角线 if i-4 <= k <= i and -j <= k <= 4-j: p = k deducted_position = [i-p,j+p] deducted_list.append(deducted_position) #这里对生成的列表进行去重 temp = [] for item in deducted_list: ifnot item in temp: temp.append(item)
deducted_list = temp return deducted_list
有了不可以使用的位置,还需要根据它找出可用的位置
1 2 3 4 5
#可用位置计算 defavailable_position(position_pool,position_deduct_list): new_position_pool = [item for item in position_pool if item notin position_deduct_list] position_pool = new_position_pool return position_pool
x_3 = a position_deduct_3 = position_deduct(a) fourth_list = available_position(third_list,position_deduct_3) for b in fourth_list:
x_4 = b position_deduct_4 = position_deduct(b)
fifth_list = available_position(fourth_list,position_deduct_4) for c in fifth_list: x_5 = c result_list.append([x_1,x_2,x_3,x_4,x_5]) #print(result_list) return result_list
defarraylist_convert(t): result = [] for i in t: arr = np.zeros([5,5],int) for j in i: arr[j[0]][j[1]] = 1#按照行标和列标赋值即可
result.append(arr) return result
有了这样一个列表,去重即可。去重的部分是由AI写的,最后对最终所得列表进行计数即可。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
defremove_duplicate_arrays(arrays_list): unique_arrays = set() result = []
for array in arrays_list: array_tuple = tuple(map(tuple, array)) # 将二维数组转换为元组 if array_tuple notin unique_arrays: unique_arrays.add(array_tuple) result.append(array) return result
defcount_num(arr): n = 0 for i in arr: n += 1 return n
#初始位置索引选择 ok deffunc()->list : available_index = [] for i inrange(0,5): for j inrange(0,5): available_index.append([i,j]) return available_index
#可用位置计算 ok defavailable_position(position_pool,position_deduct_list): new_position_pool = [item for item in position_pool if item notin position_deduct_list] position_pool = new_position_pool return position_pool
#生成位置排除列表 ok defposition_deduct(arr)->list: row_index = arr[0] column_index = arr[1] deducted_list = [] i = row_index; j = column_index
#计算直线上的不可用位置 for m inrange(-10,10): #偏移量,随便取值 for k inrange(-10,10): #列 if -i <= m <= 4-i : deducted_position = [i+m,j] deducted_list.append(deducted_position) else:pass if -j <= k <= 4-j: #行 deducted_position = [i,j+k] deducted_list.append(deducted_position) #计算对角线上的不可用位置 ok for k inrange(-10,10):#主对角线 if -i <= k <= 4-i and -j <= k <= 4-j: p = k deducted_position = [i+p,j+p] deducted_list.append(deducted_position) for k inrange(-10,10):#副对角线 if i-4 <= k <= i and -j <= k <= 4-j: p = k deducted_position = [i-p,j+p] deducted_list.append(deducted_position) #去重 temp = [] for item in deducted_list: ifnot item in temp: temp.append(item)
deducted_list = temp return deducted_list
#主程序 defmain(first_list): result_list = [] for i in first_list:
x_1 = 0;x_2 = 0;x_3= 0;x_4 = 0;x_5=0
x_1 = i position_deduct_1 = position_deduct(i) second_list = available_position(first_list,position_deduct_1)
x_3 = a position_deduct_3 = position_deduct(a) fourth_list = available_position(third_list,position_deduct_3) for b in fourth_list:
x_4 = b position_deduct_4 = position_deduct(b)
fifth_list = available_position(fourth_list,position_deduct_4) for c in fifth_list: x_5 = c result_list.append([x_1,x_2,x_3,x_4,x_5]) print(result_list) return result_list
import numpy as np defarraylist_convert(t): result = [] for i in t: arr = np.zeros([5,5],int) for j in i: arr[j[0]][j[1]] = 1
result.append(arr) return result
defremove_duplicate_arrays(arrays_list): unique_arrays = set() result = []
for array in arrays_list: array_tuple = tuple(map(tuple, array)) # 将二维数组转换为元组 if array_tuple notin unique_arrays: unique_arrays.add(array_tuple) result.append(array) return result
defcount_num(arr): n = 0 for i in arr: n += 1 return n