fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int N;
  5. int board[10][10];
  6. int ans = 0;
  7.  
  8. // 대각선 상태: "/" 대각선, "\" 대각선
  9. bool d1[20], d2[20];
  10.  
  11. // 좌표 배열(색깔 분리: 검은색/흰색 칸)
  12. vector<pair<int,int>> black, white;
  13.  
  14. void dfs(int idx, int cnt, vector<pair<int,int>>& cells) {
  15. if(idx == cells.size()) {
  16. ans = max(ans, cnt);
  17. return;
  18. }
  19. int i = cells[idx].first, j = cells[idx].second;
  20.  
  21. // 놓는 경우
  22. if(!d1[i+j] && !d2[i-j+N-1]) {
  23. d1[i+j] = d2[i-j+N-1] = true;
  24. dfs(idx+1, cnt+1, cells);
  25. d1[i+j] = d2[i-j+N-1] = false;
  26. }
  27. // 안 놓는 경우
  28. dfs(idx+1, cnt, cells);
  29. }
  30.  
  31. int main() {
  32. ios::sync_with_stdio(false);
  33. cin.tie(nullptr);
  34.  
  35. cin >> N;
  36. for(int i=0;i<N;i++) {
  37. for(int j=0;j<N;j++) {
  38. cin >> board[i][j];
  39. if(board[i][j]==1) {
  40. if((i+j)%2==0) black.push_back({i,j});
  41. else white.push_back({i,j});
  42. }
  43. }
  44. }
  45.  
  46. dfs(0,0,black);
  47. int blackAns = ans;
  48. ans = 0;
  49. dfs(0,0,white);
  50. int whiteAns = ans;
  51.  
  52. cout << blackAns + whiteAns << "\n";
  53. }
Success #stdin #stdout 0.01s 5320KB
stdin
5
1 1 0 1 1
0 1 0 0 0
1 0 1 0 1
1 0 0 0 0
1 0 1 1 1
stdout
7