Baekjoon 1799
비숍


QUESTION ❔



CODE ⌨️

#include<iostream>
#define max(a,b) a > b ? a : b
using namespace std;
 
int N;
int ans[2];
int chess[11][11];
int l[20];
int r[20];
 
// 놓을 수 있는 비숍의 최대개수를 구하는 함수
// 흑/백, 두 가지 경우로 나누어 계산
 
void tracking(int row, int col, int count, int color)
{
    if (col >= N) {
        row++;
        if(col%2 == 0) col = 1;
        else col = 0;
    }
    if (row >= N) {
        ans[color] = max(ans[color], count);
        return;
    }
 
    if (chess[row][col] && !l[col - row + N - 1] && !r[row + col])
    {
        l[col - row + N - 1] = r[row + col] = 1;
        tracking(row, col+2, count + 1, color);
        l[col - row + N - 1] = r[row + col] = 0;
    }
    tracking(row, col+2, count, color);
}
 
int main(void)
{
    cin >> N;
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            cin >> chess[i][j];
        }
    }
 
    tracking(0, 0, 0, 0);
    tracking(0, 1, 0, 1);
 
    cout << ans[0] + ans[1];
 
    return 0;
}



RESULT 💛



SIMPLE DISCUSSION ✏️

백트래킹 관련 문제였다. col - row는 오른쪽 아래 대각선의 방향을 가지며, col + row는 왼쪽 아래 대각선의 방향을 가진다. 같은 방향에 놓일 수 없으므로 1로 체크하여 해당 방향에 비숍을 올려 놓지 못하도록 설정한 하나의 idea이며 col - row에 N - 1을 더한 이유는 예를 들어, 4행 0열 (4, 0)인 경우 0 - 4를 하면 음수 index가 발생하는데 index를 0부터 시작하도록 설정하기 위해 임의로 더한 값이다. 백트래킹 중 많은 것을 배우게끔 해준 좋은 문제였다.



SOURCE 💎

Baekjoon_Link 👈 Click here


*****
NOT A TALENT ❎ NOT GIVING UP ✅
CopyRight ⓒ 2022 DCherish All Rights Reserved.