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

14-g0rnn #53

Merged
merged 2 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
.idea/
/.vscode
cmake-build-debug/
CMakeLists.txt
CMakeLists.txt
.clang-tidy
44 changes: 44 additions & 0 deletions g0rnn/backtracking/14-g0rnn.cpp
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};
Copy link
Collaborator

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;
λŠ” κΌ­ ν•„μš”ν•œκ±΄κ°€μš”??

// λ°°μ—΄μ˜ 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]보닀 큼)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ²˜μŒμ— 2차원 λ°°μ—΄λ‘œ μ§„ν–‰ν•˜λ‹€κ°€, '이게 μ•„λ‹Œλ°..? ' μ‹Άμ–΄μ„œ κ· ν˜Έλ‹˜ μ„€λͺ…을 μ’€ λ΄€μŠ΅λ‹ˆλ‹€.
κ· ν˜Έλ‹˜ μ„€λͺ…을 λ³΄λ‹ˆκΉŒ 저도 이 둜직이 μƒκ°λ‚˜μ„œ μ΄λ ‡κ²Œ μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.

λŒ€κ°μ„ μΈ 경우 (rowλŠ” 항상 chess[i]보닀 큼)

이 말은 μ²˜μŒμ— 잘 λͺ°λžλŠ”데 곰곰히 μƒκ°ν•΄λ³΄λ‹ˆκΉŒ κ·Έλ ‡λ„€μš”!
λŒ€κ°μ„ μ€ μš°ν•˜ν–₯ λ˜λŠ” μš°μƒν–₯이기 λ•Œλ¬Έμ—... 맞죠.. ?
(μ €λŠ” μ ˆλŒ€κ°’μœΌλ‘œ λΉ„κ΅ν–ˆμŠ΅λ‹ˆλ‹€... )

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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;
}