-
https://www.acmicpc.net/problem/9663
12345678910111213141516171819202122232425262728293031323334353637383940#include<iostream>using namespace std;int n,ans;bool r[32];bool c[32];bool row[16];bool col[16];void Queen(int y, int cnt) {if (cnt == n) {ans++;return;}for (int i = 0; i < n; i++) {if (row[i] == false && col[y] == false && r[i + y] == false && c[i - y + (n-1)] == false) {row[i] = true;col[y] = true;r[i + y] = true;c[i - y + (n-1)] = true;Queen(y + 1, cnt + 1);row[i] = false;col[y] = false;r[i + y] = false;c[i - y + (n - 1)] = false;}}}int main() {ios_base::sync_with_stdio(0);cin.tie(0);cin >> n;Queen(0, 0);cout << ans << '\n';return 0;}http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter백트래킹 복습으로 다시해본 N-Queen 문제
row col, 그리고 대각선을 기준으로 검증해줘서 재귀를돌린 모형으로했다.
기존의것보다 조금더 빠름
'백준알고리즘' 카테고리의 다른 글
벽 부수고 이동하기 (0) 2019.10.12 과외맨 (0) 2019.10.10 진우의 달 여행(Large) (0) 2019.10.09 거울 설치 (0) 2019.10.09 미네랄 (0) 2019.10.09