Created
March 17, 2023 08:05
-
-
Save giraycoskun/21406e67c58a6c1f3b3fb72b9a450f0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 8-Queen naive recursive search solution | |
| # giraycoskun | |
| import numpy as np | |
| STATE_SET = [] | |
| SOLUTION_SET = [] | |
| def check_row(grid, i, k): | |
| for x in range(k,-1,-1): | |
| if grid[i][x] == 1: | |
| return False | |
| return True | |
| def check_diagonal(grid, row, col): | |
| i = row | |
| k = col | |
| while i > -1 and k > -1: | |
| if grid[i][k] == 1: | |
| return False | |
| i -= 1 | |
| k -= 1 | |
| i = row | |
| k = col | |
| while i < 8 and k > -1: | |
| if grid[i][k] == 1: | |
| return False | |
| i += 1 | |
| k -= 1 | |
| return True | |
| def successor(grid, k): | |
| for i in range(8): | |
| if check_diagonal(grid, i, k) and check_row(grid, i, k): | |
| res = grid.copy() | |
| res[i][k] = 1 | |
| STATE_SET.append(res) | |
| if k == 7: | |
| SOLUTION_SET.append(res) | |
| if k<7: | |
| successor(res, k+1) | |
| grid = np.zeros((8,8)) | |
| successor(grid, 0) | |
| #print(SOLUTION_SET) | |
| print(len(STATE_SET)) | |
| print(len(SOLUTION_SET)) | |
| with open("test.txt", 'w') as file: | |
| for item in STATE_SET: | |
| for x in item: | |
| file.write(str(x)) | |
| file.write('\n') | |
| file.write('\n') | |
| file.write('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment