From 0eee9fb5479166f7cb56f1382c417a991cffefa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Wed, 4 Sep 2024 03:20:33 +0900 Subject: [PATCH] #45 9663.py --- aiden/class4-2/9663.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 aiden/class4-2/9663.py diff --git a/aiden/class4-2/9663.py b/aiden/class4-2/9663.py new file mode 100644 index 0000000..7a7fe4f --- /dev/null +++ b/aiden/class4-2/9663.py @@ -0,0 +1,25 @@ +def safe(row, col): + for i in range(row): + if col == queens[i] or abs(col - queens[i]) == row - i: + return False + return True + +def near_queens(row): + global count # 전역 변수 count를 사용하겠다는 선언 + # 사용 시 def 함수 외에서 선언된 변수를 내부에서 사용 가능 + if row == n: + count += 1 + return + for col in range(n): + if not col_used[col] and not diag1_used[row + col] and not diag2_used[row - col + n - 1]: + col_used[col] = diag1_used[row + col] = diag2_used[row - col + n - 1] = True + near_queens(row + 1) + col_used[col] = diag1_used[row + col] = diag2_used[row - col + n - 1] = False + +n = int(input()) +col_used = [False] * n +diag1_used = [False] * (2 * n - 1) +diag2_used = [False] * (2 * n - 1) +count = 0 +near_queens(0) +print(count)