解题思路:
回溯方法
注意事项:
逐行便利,所以判断是否可以放入数据的时候,可以不需要判断行的部分,而且循环检查的次数也减少。代码简洁
参考代码:
mapA=[[0]*8 for i in range(8)] data=[list(map(int,input().split())) for _ in range(8)] max_num=0 def check(row,column,mapA): x=row-1 y=column-1 for i in range(row+1):#检查列 if mapA[i][column]==1: return False while x>=0 and y>=0:#检查左上角 if mapA[x][y]==1: return False x-=1 y-=1 x=row-1 y=column+1 while x>=0 and y<8:#检查右上角 if mapA[x][y]==1: return False x-=1 y+=1 return True def DFS(row,map): if row==8: Max(mapA) else: for i in range(8): if check(row,i,mapA): mapA[row][i]=1 #放入皇后 DFS(row+1,mapA) #递归 mapA[row][i]=0 #回溯 def Max(map): global max_num sum=0 for i in range(8): for j in range(8): if map[i][j]==1: sum+=data[i][j] if sum>max_num: max_num=sum DFS(0,mapA) print(max_num)
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:569 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:944 |
C语言训练-立方和不等式 (C语言代码)浏览:751 |
A+B for Input-Output Practice (C++代码)浏览:609 |
C语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C语言代码)浏览:691 |
WU-小九九 (C++代码)浏览:1684 |
C语言程序设计教程(第三版)课后习题9.2 (C语言代码)浏览:556 |
Cylinder (C语言描述,蓝桥杯)浏览:1247 |
字符串比较 (C语言代码)浏览:685 |
C二级辅导-等差数列 (C语言代码)浏览:824 |