걍 알고리즘 신경 안쓰고 걍 짜고 싶은대로 짜봤는데 왜 시간 초과 나는지 모르겟어.. 형들 알려줘
N Queen > 문제은행 | JUNGOL : 정보올림피아드&알고리즘
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int a;
int ans = 0;//정답
int arr[15][15] = { 0 };// 체스판
int cheak = 0;// 이곳에 체스 놔도 되는지 확인할때 쓰는 변수
int wrong[15][2] = { 0 }; // 체스 놓은거 저장하는곳
void func(int line, int ches)
{
if (line !=ches)
return;
for (int j = 0; j < a; j++)
{
// 검사하는 코드
int cheak = 0;
for (int i = 0; i < ches; i++)
{
if (wrong[i][0] + wrong[i][1] != line + j && wrong[i][0] - j != wrong[i][1] - line && wrong[i][0] != j && wrong[i][1] != line)
cheak++;
}
if (cheak == ches)
{
wrong[ches][1] = line;
wrong[ches][0] = j;
if (ches + 1 == a)
{
ans++;
return;
}
func(line + 1, ches + 1);
}
}
// 만약 한줄에 못넣다면 리턴
return;
}
int main()
{
cin >> a;
func(0, 0);
cout << ans << endl;
return 0;
}
댓글 0