From df852f5a9fdc67088178c07aa0bde13ca0f60d6d Mon Sep 17 00:00:00 2001 From: leejw-lu Date: Mon, 31 Jul 2023 13:19:49 +0900 Subject: [PATCH] =?UTF-8?q?#10=209663=EB=B2=88=20N-Queen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../9663_N-Queen.py" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "\353\260\261\355\212\270\353\236\230\355\202\271/9663_N-Queen.py" diff --git "a/\353\260\261\355\212\270\353\236\230\355\202\271/9663_N-Queen.py" "b/\353\260\261\355\212\270\353\236\230\355\202\271/9663_N-Queen.py" new file mode 100644 index 0000000..2c3b76f --- /dev/null +++ "b/\353\260\261\355\212\270\353\236\230\355\202\271/9663_N-Queen.py" @@ -0,0 +1,25 @@ +import sys +read=sys.stdin.readline + +N=int(read()) +count=0 +col = [0] * (N + 1) + +def is_promising(x): + for i in range(x): + if col[x] == col[i] or abs(col[x] - col[i]) == x - i: #열과 대각선 비교 + return False + return True + +def n_queens(x): + global count + if x == N: # (N=4일때) i=0시작~ i=3, n=4: 끝까지 옴. + count += 1 + return + else: + for i in range(N): + col[x] = i + if is_promising(x): + n_queens(x+1) +n_queens(0) +print(count)