We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Algorithms
- NP Complete
- Spies, Revised
- Discussions
Spies, Revised
Spies, Revised
Sort by
recency
|
101 Discussions
|
Please Login in order to post a comment
def is_colinear(r1, c1, r2, c2, r3, c3): return (r2 - r1) * (c3 - c1) == (r3 - r1) * (c2 - c1)
def is_valid(row, col, placement): for r2 in range(row): c2 = placement[r2] if col == c2 or abs(row - r2) == abs(col - c2): return False for r1 in range(r2): c1 = placement[r1] if is_colinear(r1, c1, r2, c2, row, col): return False return True
def solve(n, placement=[], used_cols=set()): row = len(placement) if row == n: return placement for col in range(n): if col in used_cols: continue if is_valid(row, col, placement): res = solve(n, placement + [col], used_cols | {col}) if res: return res return None
if name == "main": max_n = 13 for n in range(max_n, 0, -2): result = solve(n)
if result: print(n) print(" ".join(str(c + 1) for c in result)) break9
1 7 4 6 9 2 5 3 8
why this solution is not accepted?
There might be an error in code control, the following output matrix seems true.
"3 1 4 2 5"
Works
How old is this?
In year 2000 as a recent Computer Grad, I resolved the Queens on chess board (from Java Deitel and Deitel): That is:
How many queens can we add into a chess board so that no queen captures the other?! Output is 8, where they are a chess knight distance from each other