#include <iostream>
#include <vector>
std::vector<std::pair<int, int>> vec = {};
bool find = false;
int arr[9][9] = {};
bool width[9][1025] = {};
bool length[9][1025] = {};
bool sqare[9][1025] = {};
void DFS(int count)
{
if (vec.size() == count)
{
find = true;
return;
}
int x = vec[count].first;
int y = vec[count].second;
for (int i = 1; i <= 9; ++i)
{
if (!width[x][1 << i] && !length[y][1 << i] && !sqare[(int)(x / 3) * 3 + y / 3][1 << i])
{
arr[x][y] = i;
width[x][1 << i] = true;
length[y][1 << i] = true;
sqare[(int)(x / 3) * 3 + y / 3][1 << i] = true;
DFS(count + 1);
width[x][1 << i] = false;
length[y][1 << i] = false;
sqare[(int)(x / 3) * 3 + y / 3][1 << i] = false;
if (find)
return;
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
std::cout.tie(NULL);
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
std::cin >> arr[i][j];
if (arr[i][j])
{
width[i][1 << arr[i][j]] = true;
length[j][1 << arr[i][j]] = true;
sqare[(int)(i / 3) * 3 + j/ 3][1 << arr[i][j]] = true;
}
else
{
vec.push_back({ i,j });
}
}
}
DFS(0);
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 9; ++j)
{
std::cout << arr[i][j] << " ";
}
std::cout << "\n";
}
return 0;
}
다하고 나니까 굳이 비트연산자를 써야했나 싶긴 하네요...
그냥 떠올라서 했던건데...
다시 돌릴라니 세상 귀찮아가지고...
처음에 가로 세로 네모 안에 있는 걸로 체크하고 0일 때는 따로 체크해놓고 DFS(체크해놓은 vec로만 입력)
입력 후에 이 숫자가 아니라면 다시 입력해줘야 하기 때문에 true 값을 false 값으로 변경해놓는다.
'오늘의 알고리즘' 카테고리의 다른 글
[C++] 백준 6549 히스토그램에서 가장 큰 직사각형 (1) | 2023.04.17 |
---|---|
[C++] 백준 10986 나머지 합 (0) | 2023.02.20 |
[C++] 백준 2981 검문 (0) | 2023.02.01 |
[C++] 백준 16566 카드 게임 (0) | 2022.12.27 |
[C++] 백준 2887 행성 터널 (0) | 2022.12.22 |