解题思路:
DFS
注意事项:
代码改进:
1.dfs内的赋值部分分开写可以缩短运行时间;
2.这里判断王的坐标的方法比较笨,浪费了很多空间。
参考代码:
def dfs(row=0): global r, col_not_occupied, d_1, d_2, cnt cnt += 1 if row == r else 0 for col in range(r): if col_not_occupied[col] and d_1[row - col] and d_2[row + col] and \ map_[row][col]: col_not_occupied[col], d_1[row - col], d_2[row + col] = [False for _ in range(3)] dfs(row + 1) col_not_occupied[col], d_1[row - col], d_2[row + col] = [True for _ in range(3)] r, x, y = map(int, input().split()) cnt = 0 map_ = [[1 for _ in range(r)] for _ in range(r)] col_not_occupied = [True for _ in range(r)] d_1, d_2 = [[True for _ in range(2 * r - 1)] for _ in range(2)] direction = [[0, 0], [1, 0], [-1, 0], [0, 1], [0, -1], [1, 1], [-1, -1], [1, -1], [-1, 1]] for d in direction: if 0 <= x + d[0] - 1 < r and 0 <= y + d[1] - 1 < r: map_[x + d[0] - 1][y + d[1] - 1] = 0 dfs() print(cnt)
0.0分
1 人评分
C语言考试练习题_保留字母 (C语言代码)浏览:578 |
【绝对值排序】 (C语言代码)浏览:481 |
C二级辅导-等差数列 (C语言代码)浏览:569 |
字符串输入输出函数 (Java代码)浏览:1437 |
输出正反三角形 (C语言代码)格式错误!!!浏览:1140 |
printf基础练习2 (有点不明白)浏览:836 |
C语言训练-求1+2!+3!+...+N!的和 (C语言代码)万恶的long long浏览:875 |
【简单计算】 (C语言代码)浏览:622 |
C语言程序设计教程(第三版)课后习题9.8 (C语言代码)浏览:664 |
母牛的故事 (C语言代码)浏览:940 |