상위 150개 인터뷰중간

N 퀸즈

'N Queens' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

n-queens 퍼즐은 n x n 체스판에 n개의 퀸을 배치하여 두 퀸이 서로 공격하지 않도록 하는 문제입니다.

정수 n이 주어지면 n-queens 퍼즐에 대한 모든 개별 솔루션을 반환합니다. 어떤 순서로든 답변을 반환할 수 있습니다.

각 솔루션에는 n-queens 배치의 고유한 보드 구성이 포함되어 있습니다. 여기서 'Q'와 '.' 둘 다 각각 여왕과 빈 공간을 나타냅니다.

solveNQueens(n: int) -> list 함수를 구현하세요.

제약
  • 1 <= n <= 9

Example 1
Input
4
Output
[[".Q..","...Q","Q...","..Q."],["..Q.","Q...",".Q..","...Q"]]
Explanation

There are exactly 2 distinct solutions to the 4-queens puzzle.

Example 2
Input
1
Output
[["Q"]]
Explanation

A single queen on a 1x1 board is the only solution.

Need a Hint?
세트 또는 힙과 같은 역추적 관련 데이터 구조를 사용하는 것이 좋습니다.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.