From 8e37d4fb033e0f044d88d4dce3ad080dc332a72c Mon Sep 17 00:00:00 2001 From: letsjo Date: Wed, 12 Apr 2023 17:18:03 +0900 Subject: [PATCH] [#12] Feat: Add N-Queen --- 9663.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 9663.py diff --git a/9663.py b/9663.py new file mode 100644 index 0000000..ea60de8 --- /dev/null +++ b/9663.py @@ -0,0 +1,30 @@ +# N-Queen +# PyPy3 로 완료 + +N = int(input()) +count = 0 +chessBoard = [0] * N +# (x,y) = (index,value) + + +def checkChessBoard(col): + for i in range(col): + if (chessBoard[col] == chessBoard[i] or abs(col-i) == abs(chessBoard[col]-chessBoard[i])): + return False + return True + + +def nQueen(col): + global count + if (col == N): + count += 1 + return + + for row in range(N): + chessBoard[col] = row + if (checkChessBoard(col)): + nQueen(col+1) + + +nQueen(0) +print(count)