-
Notifications
You must be signed in to change notification settings - Fork 0
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
14-g0rnn #53
14-g0rnn #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.idea/ | ||
/.vscode | ||
cmake-build-debug/ | ||
CMakeLists.txt | ||
CMakeLists.txt | ||
.clang-tidy |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Created by κΉκ· νΈ on 2025. 1. 7.. | ||
// | ||
#include <iostream> | ||
using namespace std; | ||
|
||
#define EMPTY -1 | ||
|
||
int n; | ||
int chess[15] = {0}; | ||
// λ°°μ΄μ valueμ νΈμ΄ λμ¬μ§ rowλ₯Ό μλ―Έ | ||
// λ°°μ΄μ indexλ νΈμ΄ λμ¬μ§ columnμ μλ―Έ | ||
// ex) chess[3] = 5 -> νΈμ΄ 5ν 3μ΄μ λμ¬μ Έ μλ€λ μλ―Έ | ||
|
||
// colκ³Ό rowλ νΈμ λμ μ΄κ³Ό ν | ||
// colκ³Ό rowλ‘ λ°μ μμΉμ λν΄ μ΄λ―Έ λμ¬μ§ νΈμ νΌν΄ λμ μ μλμ§ | ||
bool canMoveTo(int col, int row) { | ||
for (int i = 0; i < n; i++) { | ||
if (chess[i] == EMPTY) continue; | ||
if (abs(col - i) == row - chess[i]) return false; // λκ°μ μΈ κ²½μ° (rowλ νμ chess[i]λ³΄λ€ νΌ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ²μμ 2μ°¨μ λ°°μ΄λ‘ μ§ννλ€κ°, 'μ΄κ² μλλ°..? ' μΆμ΄μ κ· νΈλ μ€λͺ
μ μ’ λ΄€μ΅λλ€.
μ΄ λ§μ μ²μμ μ λͺ°λλλ° κ³°κ³°ν μκ°ν΄λ³΄λκΉ κ·Έλ λ€μ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. λ§μ΅λλ€! 체μ€ν κΈ°μ€ λ§¨ μμμ λ°μΌλ‘ λ΄λ €κ°κΈ° λλ¬Έμ |
||
if (i == col) return false; // κ°μ μ΄μΈ κ²½μ° | ||
} | ||
return true; | ||
} | ||
|
||
int findQueen(int cnt, int row) { | ||
if (cnt == n) return 1; | ||
|
||
int total = 0; | ||
for (int col = 0; col < n; col++) { | ||
if (!canMoveTo(col, row)) continue; | ||
chess[col] = row; | ||
total += findQueen(cnt + 1, row + 1); // κ°λ₯ν κ²½μ°μ μμ μ΄ν© | ||
chess[col] = EMPTY; | ||
} | ||
return total; | ||
} | ||
|
||
int main() { | ||
cin >> n; | ||
fill(chess, chess + n, EMPTY); | ||
cout << findQueen(0, 0); | ||
return 0; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ λ 2μ°¨μ λ°°μ΄μ λ§λ€μ΄μ ꡬννλ €κ³ νλλ°(κ²°κ΅ κ΅¬νμ λͺ»νμ§λ§)
μ΄μ μΈλ±μ€λ‘ νμ κ°μΌλ‘ λλ λ°©μμ μκ°νμ§ λͺ»νλ€μ.
μ λ λ°λλ‘ νμ μΈλ±μ€λ‘ λκ³ μ΄μ κ°μΌλ‘ λκ³ ν΄λ³΄μμ΅λλ€.
κ°μΈμ μΌλ‘λ κ·Έκ² λ νΈνλ€κ³ μκ°νμ΅λλ€.
κ·Έλ°λ° νκ°μ§ κΆκΈν κ² canMoveToμμ
if (chess[I] == EMPTY) continue;
λ κΌ νμν건κ°μ??