解题思路:
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语言程序设计教程(第三版)课后习题8.8 (C++代码)浏览:583 |
【绝对值排序】 (C语言代码)浏览:500 |
不容易系列 (C语言代码)浏览:703 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:741 |
C语言训练-排序问题<2> (C++代码)(sort函数)浏览:1728 |
C二级辅导-计负均正 (C语言代码)浏览:607 |
时间转换 (Java代码)浏览:618 |
C语言程序设计教程(第三版)课后习题11.1 (C语言代码)浏览:725 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:634 |
剪刀石头布 (C语言代码)浏览:1792 |