File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // N-Queen
2+ // https://www.acmicpc.net/problem/9663
3+ const fs = require ( 'fs' ) ;
4+ const stdin = ( process . platform === 'linux'
5+ ? fs . readFileSync ( '/dev/stdin' ) . toString ( ) . trim ( )
6+ : `8`
7+ ) . split ( '\n' ) ;
8+
9+ const input = ( ( ) => {
10+ let line = 0 ;
11+ return ( ) => stdin [ line ++ ] ;
12+ } ) ( ) ;
13+
14+ function isValid ( col ) {
15+ for ( let i = 0 ; i < col ; i ++ ) {
16+ if (
17+ chessBoard [ i ] === chessBoard [ col ] ||
18+ col - i === Math . abs ( chessBoard [ col ] - chessBoard [ i ] )
19+ ) {
20+ return false ;
21+ }
22+ }
23+ return true ;
24+ }
25+
26+ function dfs ( col ) {
27+ if ( col == N ) {
28+ answer ++ ;
29+ return ;
30+ }
31+
32+ for ( let i = 0 ; i < N ; i ++ ) {
33+ chessBoard [ col ] = i ;
34+ if ( isValid ( col ) ) {
35+ dfs ( col + 1 ) ;
36+ }
37+ }
38+ }
39+
40+ let answer = 0 ;
41+
42+ const N = parseInt ( input ( ) ) ;
43+ let chessBoard = Array ( N ) . fill ( 0 ) ;
44+
45+ dfs ( 0 ) ;
46+ console . log ( answer ) ;
47+
48+ /*
49+
50+ ## How
51+ ๋ฌธ์ ๋ ๋๋ฌธ์ฅ์ผ๋ก ๊ตฌ์ฑ๋์ด์์๊ณ input๋ 1๊ฐ๋ง ๋ฐ๊ฒ๋ ๋์ด์๋ ๋ฌธ์ ์๋ค.
52+
53+ ๋ฐฐ์ด์ index๋ฅผ x๋ผ๊ณ ๋๊ณ x์ ๊ฐ index์ ์ด๊ธฐํ๋๋ ๊ฐ์ y๋ผ๊ณ ๋ ์ ์๋ค. ์ด๋ฐ ํํ๋ก N ํฌ๊ธฐ์ 1์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ ๋ค.
54+
55+ ์ฌ๊ท๋ก ๊ฐ๋ฅํ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ตฌํ ์ ์๋๋ฐ ๋ช
๋ฐฑํ๊ฒ ๋ถ๊ฐ๋ฅํ ์ํฉ์ ๋ํด์๋ ๊ฐ์ง๋ฅผ ์น ์ ์๋ค.
56+
57+ ๊ฐ์ง๋ฅผ ์น ์ ์๋ ์ํฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
58+
59+ 1. ๊ฐ์ ํ ํน์ ์ด์ ํธ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ
60+ 2. ๋๊ฐ์ ์ ํธ์ด ์กด์ฌํ๋ ๊ฒฝ์ฐ
61+
62+ ์ด ๋๊ฐ์ง์ ํด๋นํ์ง ์๋ ๊ฒฝ์ฐ์๋ง true๋ฅผ ๋ฐํํ๋ ํจ์๋ฅผ ๋ง๋ค์ด์ ์ฌ๊ท ํธ์ถ์ ํ๊ฒ๋ ์ฒ๋ฆฌํ์๋ค.
63+
64+ ์ฌ๊ทํจ์์ index๊ฐ N๊น์ง ๊ฐ๋ ๊ฒฝ์ฐ count๋ฅผ ์ฆ๊ฐ์์ผฐ๊ณ ์ต์ข
count๊ฐ ์๋ก ๊ณต๊ฒฉํ ์ ์๊ฒ ๋๋ ๊ฒฝ์ฐ์ ์์ด๋ค.
65+
66+ ## Retrospective
67+
68+ */
You canโt perform that action at this time.
0 commit comments