145颜色方块(11)
【摘要】
智力游戏
规则和122颜色方块(9)132(10)一样,不过这里变成了16个格子。
代码也是直接将那个代码拓展得到的。
代码:
#include<iostream>
#include<iomanip>
using namespace std;
int same(int i, int...
规则和122颜色方块(9)132(10)一样,不过这里变成了16个格子。
代码也是直接将那个代码拓展得到的。
代码:
#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); bool main3(int list[][4], int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9, int i10, int i11, int i12); int main() { int list[17][4]; cout << "输入每个格子的4个数字" << endl; for (int i = 0; i < 16; i++)for (int j = 0; j < 4; j++)cin >> list[i][j]; list[16][0] = list[16][1] = list[16][2] = list[16][3] = 0; for (int i1 = 0; i1 < 16; i1++)for (int i2 = 0; i2 < 16; i2++)for (int i3 = 0; i3 < 16; i3++) { if (i1 == i2 || i1 == i3 || i2 == i3)continue; for (int i4 = 0; i4 < 16; i4++)for (int i5 = 0; i5 < 16; i5++) { if (i1 == i4 || i2 == i4 || i3 == i4 || !ok(list, 16, i1, i3, i4))continue; if (same(i1, i2, i3, i4, i5) || !ok(list, i1, i2, i4, i5))continue; for (int i6 = 0; i6 < 16; i6++)for (int i7 = 0; i7 < 16; i7++) { if (same(i1, i2, i3, i4, i6) || i5 == i6)continue; if (same(i1, i2, i3, i4, i7) || i5 == i7 || i6 == i7)continue; for (int i8 = 0; i8 < 16; 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 < 16; 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; } 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 < 16; 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 < 16; i11++)for (int i12 = 0; i12 < 16; i12++) { if (same(i1, i2, i3, i4, i11) || same(i5, i6, i7, i8, i11) || i9 == i11 || i10 == i11)continue; if (!ok(list, i7, i8, 16, i11) || i9 == i12 || i10 == i12 || i11 == i12)continue; if (same(i1, i2, i3, i4, i12) || same(i5, i6, i7, i8, i12) || !ok(list, i8, i9, i11, i12))continue; if (main3(list, i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12))return true; } } return false; } bool main3(int list[][4], int i1, int i2, int i3, int i4, int i5, int i6, int i7, int i8, int i9,int i10,int i11,int i12) { for (int i13 = 0; i13 < 16; i13++)for (int i14 = 0; i14 < 16; i14++) { if (same(i1, i2, i3, i4, i13) || same(i5, i6, i7, i8, i13) || same(i9, i10, i11, i12, i13))continue; if (!ok(list, i2, i13, i5, i6) || list[i2][1] + list[i13][0] != 10)continue; if (same(i1, i2, i3, i4, i14) || same(i5, i6, i7, i8, i14) || same(i9, i10, i11, i12, i14))continue; if (i13 == i14 || !ok(list, i13, 16, i6, i14))continue; for (int i15 = 0; i15 < 16; i15++)for (int i16 = 0; i16 < 16; i16++) { if (same(i1, i2, i3, i4, i15) || same(i5, i6, i7, i8, i15) || same(i9, i10, i11, i12, i15))continue; if (i13 == i15 || i14 == i15||!ok(list, i6, i14, i10, i15) || list[i14][3] + list[i15][1] != 10)continue; if (same(i1, i2, i3, i4, i16) || same(i5, i6, i7, i8, i16) || same(i9, i10, i11, i12, i16))continue; if (i13 == i16 || i14 == i16 || i15 == i16||!ok(list, i9, i10, i12, i16))continue; if (!ok(list, i10, i15, i16, 16) || list[i12][3] + list[i16][2] != 10)continue; cout << " " << setw(4) << i1 + 1 << setw(4) << i2 + 1 << setw(4) << i13 + 1 << endl << setw(4) << i3 + 1; cout << setw(4) << i4 + 1 << setw(4) << i5 + 1 << setw(4) << i6 + 1 << setw(4) << i14 + 1 << endl; cout << setw(4) << i7 + 1 << setw(4) << i8 + 1 << setw(4) << i9 + 1 << setw(4) << i10 + 1 << setw(4); cout << i15 + 1 << endl << " " << setw(4) << i11 + 1 << setw(4) << i12 + 1 << setw(4) << i16 + 1 ; cout << endl << endl << "这是其中一种解法,数字1的位置表示第一个输入的格子应该排的位置,以此类推" << endl; system("pause>nul"); return true; } } return false; }程序内部编号:(4个角落的编号为16)
运行结果:
对应答案:
文章来源: blog.csdn.net,作者:csuzhucong,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/nameofcsdn/article/details/53098843
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)