具体布局图如上:1-11个可选建筑位置(编号1-11)+中间1个固定建筑(未编号)。共计18条连续产生效果(a[0]-a[11],然后多次尝试之后,写出如下代码。
#include <iostream>
using namespace std;
int f = 1, y = 2, g = 3;
string jianzhuming[] = { "房子","娱乐","工厂" };
struct Zonghe
{int renkou;int chanchu;int yule;Zonghe(int renkou = 0, int chanchu = 0, int yule = 0) :renkou(renkou), chanchu(chanchu), yule(yule) {};
};
Zonghe operator +(const Zonghe& a, const Zonghe& b)
{return Zonghe(a.renkou + b.renkou, a.chanchu + b.chanchu, a.yule + b.yule);
}Zonghe jisuan(Zonghe z, int a, int b)
{if (a == b){if (a == f){z.chanchu = 0;z.yule = 0;z.renkou = 0;}else{if (a == y){z.chanchu = 0;z.yule = 0;z.renkou = 0;}else{z.chanchu = 0;z.yule = 0;z.renkou = 0;}}}else{if ((a == f && b == y) || (a == y && b == f)){z.chanchu = 0;z.yule = 1;z.renkou = 0;}if ((a == f && b == g) || (a == g && b == f)){z.chanchu = 12;z.yule = 0;z.renkou = 0;}if ((a == y && b == g) || (a == g && b == y)){z.chanchu = 0;z.yule = -1;z.renkou = 0;}}return z;
}int max(int a, int b)
{return a > b ? a : b;
}
int q[] = { 1,1,1,1,1,1,1,1,1,1,1,1 };
Zonghe bia(Zonghe a, Zonghe b,int sss[])
{if (a.chanchu == b.chanchu){if (a.yule > b.yule){return a;}else{for (int i = 0; i < 11; i++){q[i] = sss[i];}return b;}}if (a.chanchu > b.chanchu){return a;}else{for (int i = 0; i < 11; i++){q[i] = sss[i];}return b;}}Zonghe qiuhe(Zonghe t, int p[])
{Zonghe k = Zonghe(0, 0, 0);Zonghe a[18] = { k,k,k,k,k,k,k,k,k,k,k,k,k,k,k,k,k,k };a[0]= jisuan(a[0], p[0], p[1]);
a[1] = jisuan(a[1], p[1], p[2]);
a[2] = jisuan(a[2], p[2], p[5]);
a[3] = jisuan(a[3], p[1], p[9]);
a[4] = jisuan(a[4], f, p[1]);
a[5] = jisuan(a[5], f, p[3]);
a[6] = jisuan(a[6], p[3], p[4]);
a[7] = jisuan(a[7], p[4], p[5]);
a[8] = jisuan(a[8], p[4], p[6]);
a[9] = jisuan(a[9], p[5], p[6]);
a[10] = jisuan(a[10], f, p[5]);
a[11] = jisuan(a[11], f, p[10]);
a[12] = jisuan(a[12], f, p[9]);
a[13] = jisuan(a[13], p[8], p[9]);
a[14] = jisuan(a[14], p[10], p[8]);
a[15] = jisuan(a[15], p[7], p[10]);
a[16] = jisuan(a[16], p[6], p[7]);
a[17] = jisuan(a[17], p[7], p[8]);for (int i=0; i < 18; i++){t = t + a[i];}/*if (t.chanchu > 0){cout << "产出:" << t.chanchu << "娱乐:" << t.yule << "人口:" << t.renkou << endl;getchar();}*/return t;
}int main()
{int diyige = 0;Zonghe k = Zonghe(0, 0, 0);Zonghe zui = Zonghe(0, 0, 0);Zonghe kz= Zonghe(0, 0, 0);int p[] = {1,1,1,1,1,1,1,1,1,1,1,1};for (p[0] = 1; p[0] <4; p[0]++){for (p[1] = 1; p[1] <4; p[1]++){for (p[2] = 1; p[2] < 4; p[2]++){for (p[3] = 1; p[3] < 4; p[3]++){for (p[4] = 1; p[4] < 4; p[4]++){for (p[5] = 1; p[5] < 4; p[5]++){for (p[6] = 1; p[6] < 4; p[6]++){for (p[7] = 1; p[7] < 4; p[7]++){for (p[8] = 1; p[8] < 4; p[8]++){for (p[9] = 1; p[9] < 4; p[9]++){for (p[10] = 1; p[10] < 4; p[10]++){k = Zonghe(0, 0, 0);k=qiuhe(k, p);Zonghe guding = Zonghe(0, 0, 0);for (int i = 0; i < 11; i++){if (p[i] == y){guding.yule = guding.yule + 1;}if (p[i] == f){guding.renkou = guding.renkou + 10;}if (p[i] == g){guding.yule = guding.yule - 1;}}kz = k + guding;if (kz.yule >= 0){if (diyige == 0){zui = kz;diyige = 1;}else{zui = bia(zui,kz,p);}}}}}}}}}}}}}cout << "产出:" << zui.chanchu << "娱乐:" << zui.yule << "人口:" << zui.renkou << endl;for (int i = 0; i < 11; i++){cout <<"位置"<<i+1<<" : "<<jianzhuming[q[i]-1]<<" " << endl;}return 0;
}
运行结果如下图: