122颜色方块(9)
编号:(4个编号12的格子是空格,相当于是4个角落的数字都为0的小方块)
代码:
#include<iostream>
#include<iomanip>
using namespace std;
int same(int i, int j, int k, int l, int x)
{
if (i == x || j == x || k == x || l == x)return 1;
else return 0;
}
bool ok(int list[][4], int a, int b, int c, int d)
{
if ((list[a][3] + list[b][2] + list[c][1] + list[d][0]) == 10)return true;
return false;
}
bool main2(int list[][4], int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9)
{
for (int i10 = 0; i10 < 12; i10++)
{
if (same(i1, i2, i3, i4, i10) || same(i5, i6, i7, i8, i10) || i9 == i10)continue;
if (!ok(list, i5, i6, i9, i10))continue;
for (int i11 = 0; i11 < 12; i11++)for (int i12 = 0; i12 < 12; i12++)
{
if (same(i1, i2, i3, i4, i11) || same(i5, i6, i7, i8, i11) || i9 == i11 || i10 == i11)continue;
if (!ok(list, i7, i8, 12, i11) || i9 == i12 || i10 == i12 || i11 == i12)continue;
if (same(i1, i2, i3, i4, i12) || same(i5, i6, i7, i8, i12))continue;
if (!ok(list, i8, i9, i11, i12) || !ok(list, i9, i10, i12, 12))continue;
cout << " " << setw(4) << i1 + 1 << setw(4) << i2 + 1 << endl;
cout << setw(4) << i3 + 1 << setw(4) << i4 + 1 << setw(4) << i5 + 1 << setw(4) << i6 + 1 << endl;
cout << setw(4) << i7 + 1 << setw(4) << i8 + 1 << setw(4) << i9 + 1 << setw(4) << i10 + 1 << endl;
cout << " " << setw(4) << i11 + 1 << setw(4) << i12 + 1 << endl << endl;
cout << "这是其中一种解法,数字1的位置表示第一个输入的格子应该排的位置,以此类推";
system("pause>nul");
return true;
}
}
return false;
}
int main()
{
int list[13][4];
cout << "输入每个格子的4个数字" << endl;
for (int i = 0; i < 12; i++)for (int j = 0; j < 4; j++)cin >> list[i][j];
list[12][0] = list[12][1] = list[12][2] = list[12][3] = 0;
for (int i1 = 0; i1 < 12; i1++)for (int i2 = 0; i2 < 12; i2++)for (int i3 = 0; i3 < 12; i3++)
{
if (i1 == i2 || i1 == i3 || i2 == i3)continue;
for (int i4 = 0; i4 < 12; i4++)for (int i5 = 0; i5 < 12; i5++)
{
if (i1 == i4 || i2 == i4 || i3 == i4 || !ok(list, 12, i1, i3, i4))continue;
if (same(i1, i2, i3, i4, i5) || !ok(list, i1, i2, i4, i5))continue;
for (int i6 = 0; i6 < 12; i6++)for (int i7 = 0; i7 < 12; i7++)
{
if (same(i1, i2, i3, i4, i6) || i5 == i6)continue;
if (!ok(list, i2, 12, i5, i6))continue;
if (same(i1, i2, i3, i4, i7) || i5 == i7 || i6 == i7)continue;
for (int i8 = 0; i8 < 12; i8++)
{
if (same(i1, i2, i3, i4, i8) || i5 == i8 || i6 == i8 || i7 == i8)continue;
if (!ok(list, i3, i4, i7, i8))continue;
for (int i9 = 0; i9 < 12; i9++)
{
if (same(i1, i2, i3, i4, i9) || same(i5, i6, i7, i8, i9))continue;
if (!ok(list, i4, i5, i8, i9))continue;
if (main2(list, i1, i2, i3, i4, i5, i6, i7, i8, i9))return 0;
}
}
}
}
}
return 0;
}
输入:
5 3 6 0
2 5 1 2
1 4 1 3
8 4 6 3
1 3 1 5
4 6 3 5
3 4 4 4
4 6 4 2
3 1 2 6
6 3 3 4
3 0 5 5
0 2 6 2
输出:
4 10
1 3 12 6
8 11 5 2
9 7
这是其中一种解法,数字1的位置表示第一个输入的格子应该排的位置,以此类推
132(10)
一样的