位置可以任选,但是格子是不能旋转的。
代码:
#include<iostream>
#include<iomanip>
using namespace std;
char list[12][4];
bool notBeLine(int i, int j)
{
return list[i][2] != list[j][0] || list[i][3] != list[j][1];
}
bool notBeRow(int i, int j)
{
return list[i][1] != list[j][0] || list[i][3] != list[j][2];
}
bool same(int i, int j, int k, int l, int x)
{
return i == x || j == x || k == x || l == x;
}
bool main2(int i, int j, int k, int l);
bool main3(int i, int j, int k, int l, int ri, int rj, int rk, int rl);
int main()
{
cout << "输入每个格子的4个点的颜色,用任何字符代表颜色都可以" << endl;
for (int i = 0; i < 12; i++)for (int j = 0; j < 4; j++)cin >> list[i][j];
for (int i = 0; i < 12; i++)
{
for (int j = 0; j < 12; j++)
{
if (i == j || (notBeLine(i, j)))continue;
for (int k = 0; k < 12; k++)
{
if (i == k || j == k || (notBeLine(j, k)))continue;
for (int l = 0; l < 12; l++)
{
if (i == l || j == l || k == l)continue;
if (notBeLine(k, l))continue;
if (main2(i, j, k, l))return 0;
}
}
}
}
}
bool main2(int i, int j, int k, int l)
{
for (int ri = 0; ri < 12; ri++)
{
if (notBeRow(i, ri) || same(i, j, k, l, ri))continue;
for (int rj = 0; rj < 12; rj++)
{
if (notBeRow(j, rj) || same(i, j, k, l, rj))continue;
if (ri == rj || (notBeLine(ri, rj)))continue;
for (int rk = 0; rk < 12; rk++)
{
if (notBeRow(k, rk) || same(i, j, k, l, rk))continue;
if (ri == rk || rj == rk || (notBeLine(rj, rk)))continue;
for (int rl = 0; rl < 12; rl++)
{
if (notBeRow(l, rl) || same(i, j, k, l, rl))continue;
if (ri == rl || rj == rl || rk == rl || notBeLine(rk, rl))continue;
if (main3(i, j, k, l, ri, rj, rk, rl))return true;
}
}
}
}
return false;
}
bool main3(int i, int j, int k, int l, int ri, int rj, int rk, int rl)
{
for (int j1 = 0; j1 < 12; j1++)
{
if (same(i, j, k, l, j1) || same(ri, rj, rk, rl, j1) || notBeRow(j1, j))continue;
for (int j2 = 0; j2 < 12; j2++)
{
if (same(i, j, k, l, j2) || same(ri, rj, rk, rl, j2) || notBeRow(rj, j2))continue;
for (int k1 = 0; k1 < 12; k1++)
{
if (same(i, j, k, l, k1) || same(ri, rj, rk, rl, k1) || notBeRow(k1, k))continue;
if (j1 == j2 || j2 == k1 || k1 == j1)continue;
for (int k2 = 0; k2 < 12; k2++)
{
if (same(i, j, k, l, k2) || same(ri, rj, rk, rl, k2) || notBeRow(rk, k2))continue;
if (j1 == k2 || j2 == k2 || k1 == k2)continue;
if (notBeLine(j1, k1) || notBeLine(j2, k2))continue;
cout << " " << setw(4) << i + 1 << setw(4) << ri + 1 << endl;
cout << setw(4) << j1 + 1 << setw(4) << j + 1 << setw(4) << rj + 1 << setw(4) << j2 + 1 << endl;
cout << setw(4) << k1 + 1 << setw(4) << k + 1 << setw(4) << rk + 1 << setw(4) << k2 + 1 << endl;
cout << " " << setw(4) << l + 1 << setw(4) << rl + 1 << endl << endl;
cout << "这是其中一种解法,数字1的位置表示第一个输入的格子应该排的位置,以此类推";
system("pause>nul");
return true;
}
}
}
}
return false;
}