#include <iostream>
using namespace std;
int scale, input_y, input_x, ct, sol[9];
bool check_x[9], check_y[9], check_d1[15], check_d2[15];
void backtrack(int y)
{
if (y > 8)
{
cout << "\n";
cout << " " << ct << " ";
ct++;
for (int i = 1;i < 9;++i)
{
cout << sol[i] << " ";
}
}
else if (y == input_y)
{
sol[y] = input_x;
backtrack(y + 1);
}
else
{
for (int x = 1;x < 9;++x)
{
int d1 = (x + y - 2), d2 = (x - y + 7);//check diagonal
check_x[input_x] = check_d1[(input_x + input_y - 2)] = check_d2[input_x - input_y + 7] = true;
if (!check_x[x] && !check_d1[d1] && !check_d2[d2])
{
check_x[x] = check_d1[d1] = check_d2[d2] = true;
sol[y] = x;
backtrack(y + 1);
check_x[x] = check_d1[d1] = check_d2[d2] = false;
}
}
}
}
int main()
{
cin >> scale;
while (scale-- && cin >> input_y >> input_x)
{
cout << "SOLN COLUMN" << "\n" << " # 1 2 3 4 5 6 7 8" << "\n";
for (int i = 0;i < 15;i++)
{
check_d1[i] = check_d2[i] = 0;
}//init
for (int i = 0;i < 9;i++)
{
check_x[i] = check_y[i] = sol[i] = 0;
}//init
ct = 1;
backtrack(1);
cout << "\n";
}
}
錯在LINE:9