[BOJ] 9663번 : N-Queen

2024. 8. 17. 17:56Algorithm

1. problem : 

https://www.acmicpc.net/problem/9663

 

 

2. solution 1 :

#include <bits/stdc++.h>
using namespace std;

int N;
int isUsed1[14] = { 0 }; // 같은 열 
int isUsed2[27] = { 0 }; // 우상향 대각선 
int isUsed3[27] = { 0 }; // 우하향 대각선

int ans = 0;

void queenBatch(int k) {
	if (k == N) {
		ans++;
		return;
	}
	for (int col = 0; col < N; col++) {
		if (!isUsed1[col] && !isUsed2[k + col] && !isUsed3[k - col + N - 1]) {
			isUsed1[col] = 1;
			isUsed2[k + col] = 1;
			isUsed3[k - col + N - 1] = 1;
			queenBatch(k + 1); 
			isUsed1[col] = 0;
			isUsed2[k + col] = 0;
			isUsed3[k - col + N - 1] = 0;
		}
	}
}


int main(void) {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> N;

	queenBatch(0);
	cout << ans << '\n';
}

source code 출처  : https://github.com/encrypted-def/basic-algo-lecture/blob/master/0x0C/solutions/9663.cpp

 

basic-algo-lecture/0x0C/solutions/9663.cpp at master · encrypted-def/basic-algo-lecture

바킹독의 실전 알고리즘 강의 자료. Contribute to encrypted-def/basic-algo-lecture development by creating an account on GitHub.

github.com

 

'Algorithm' 카테고리의 다른 글

[BOJ] 15650번 : N과 M (2)  (0) 2024.08.18
[C++] next_permutation  (0) 2024.08.17
[BOJ] 15649번 : N과 M (1)  (0) 2024.08.17
[BOJ] 1182번 : 부분수열의 합  (0) 2024.08.17
[BOJ] 1992번 : 쿼드트리  (0) 2024.08.16