Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BOJ] 9663 #28

Open
HyomK opened this issue Feb 6, 2023 · 0 comments
Open

[BOJ] 9663 #28

HyomK opened this issue Feb 6, 2023 · 0 comments

Comments

@HyomK
Copy link
Owner

HyomK commented Feb 6, 2023

백준 9663 N-Queen
[문제유형] 백트래킹
https://www.acmicpc.net/problem/9663

백트래킹 대표 문제

처음에는 2차원 배열로 퀸의 위치를 나타내었다. -> 시간초과
다른 풀이를 찾아본 결과
한 행에는 하나의 퀸밖에 있을 수 없기 때문에col[행] = 열 방식으로 개선할 수 있었다

  fun checker(cur : Pos) : Boolean{
        //행탐색
        for(i in 0 until cur.row){
            if((col[i] == col[cur.row] || Math.abs(i-cur.row) == Math.abs(col[i]-cur.col))){
                return false
            }
        }
        return true
    }

    fun backTracking( level : Int){
        //대각선과 각 row col에 놓아서는 안 된다
        if(level == N ){
            result ++
            return
        }
        if(level>=N) return
        for(j in 0 until N){
            col[level] = j //level번째 행 j열 에 퀸을 놓는다.
            if(checker(Pos(level,j))) {
                //유망한 노드
                backTracking(level+1)
            }
        }
    }
HyomK added a commit that referenced this issue Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant