1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include<cstdio> #include<cstdlib> int col[17]; int n,m,ans; bool isPossible(int c){ for(int i=0; i<c; i++){ if(col[i]==col[c]) return false; if(abs(col[i]-col[c])==abs(i-c)) return false; } return true; } void queen(int i){ if(i==n) ans++; else{ for(int j=0;j<n;j++){ col[i] = j; if(isPossible(i)) queen(i+1); } } } int main(void){ scanf("%d", &n); queen(0); printf("%d",ans); return 0; } | cs |
'백준 2 > 백트래킹' 카테고리의 다른 글
| [백준 6603] 로또 (C++/Python) (0) | 2020.12.07 |
|---|---|
| [백준 1987] 알파벳 (0) | 2020.12.07 |
| [백준 15657] N과 M(8) (C++/Python) (0) | 2020.12.07 |
| [백준 15656] N과 M(7) (C++/Python) (0) | 2020.12.07 |
| [백준 15655] N과 M(6) (C++/Python) (0) | 2020.12.07 |