DFS를 연습 안 한지 오래되다 보니 DFS를 통한 접근을 생각을 못했다. 그리고 완전탐색을 하면 시간초과가 생길거 같아서 시도조차 못했는데 결국 정답은 완전탐색이었다...
레벨 올리는것보다 다시 차근차근 올라가는걸 목표로 해보겠다.
#include <iostream>
#include <vector>
#include <set>
using namespace std;
vector <vector<int>> board(5, vector<int>(5, 0));
vector <bool> visited(1000000, false);//최대 000000에서 999999까지
vector <pair<int, int>> direction{ {1, 0}, {0, 1}, {-1, 0}, {0, -1} };
int answer = 0;
void input_board()
{
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
cin >> board[i][j];
}
}
return;
}
void DFS(int x, int y, int sum, int count)
{
int i;
int next_x, next_y;
if (count == 5)
{
if (visited[sum] == false)
{
visited[sum] = true;
answer++;
}
return;
}
for (i = 0; i < 4; i++)
{
next_x = x + direction[i].first;
next_y = y + direction[i].second;
if ((next_x >= 0 && next_x < 5) && (next_y >= 0 && next_y < 5))
{
DFS(next_x, next_y, sum * 10 + board[next_x][next_y], count + 1);
}
}
}
void find_answer()
{
int i, j, sum;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
DFS(i, j, board[i][j], 0);
}
}
cout << answer << "\n";
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
input_board();
find_answer();
return 0;
}