71颜色方块(5)84(6)97(7)108(8)

智力游戏

71颜色方块(5)84(6)97(7)108(8)_第1张图片


71颜色方块(5)84(6)97(7)108(8)_第2张图片

在14颜色方块(1)里面,方块是不能转动的,在 26颜色方块(2)41(3)56(4)里面方块是可以转动的。
但是他们的代码是差不多的。

这一关虽然不能转动,但是后面的关卡可以转动,所以我干脆写成了1个代码。
可以转动,和不能转动,区别只在于有没有
if (ni || nj || nk || nl || nri || nrj || nrk || nrl || nsi || nsj || nsk || nsl || njj1 || nj2 || nk1 || nk2)continue;
这句话,其他地方一模一样。

代码:

#include<iostream>
#include<iomanip>
using namespace std;

char list[16][4][4];

int notBeLine(char llist[][4][4], int i, int j, int ni, int nj)
{
	if (llist[i][2][ni] == llist[j][0][nj] && llist[i][3][ni] == llist[j][1][nj])return 0;
	else return 1;
}

int notBeRow(char llist[][4][4], int i, int j, int ni, int nj)
{
	if (llist[i][1][ni] == llist[j][0][nj] && llist[i][3][ni] == llist[j][2][nj])return 0;
	else return 1;
}

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;
}

int i, ni, j, nj, k, nk, l, nl;
int ri, nri, rj, nrj, rk, nrk, rl, nrl;
int si, nsi, sj, nsj, sk, nsk, sl, nsl;
int jj1 = 0, njj1, j2, nj2, k1, nk1, k2, nk2;
bool main2();
bool main3();
bool main4();
void out();

int main()
{

	cout << "输入每个格子的4个点的颜色,用任何字符代表颜色都可以" << endl;
	for (int i = 0; i < 16; i++)
	{
		for (int j = 0; j < 4; j++)cin >> list[i][j][0];
		for (int temp = 1; temp < 4; temp++)
		{
			list[i][1][temp] = list[i][0][temp - 1];
			list[i][2][temp] = list[i][3][temp - 1];
			list[i][3][temp] = list[i][1][temp - 1];
			list[i][0][temp] = list[i][2][temp - 1];
		}
	}

	for (i = 0; i < 16; i++)for (ni = 0; ni < 4; ni++)
	for (j = 0; j < 16; j++)for (nj = 0; nj < 4; nj++)
	{
		if (i == j || (notBeLine(list, i, j, ni, nj)))continue;
		for (k = 0; k < 16; k++)for (nk = 0; nk < 4; nk++)
		{
			if (i == k || j == k || (notBeLine(list, j, k, nj, nk)))continue;
			for (l = 0; l < 16; l++)for (nl = 0; nl < 4; nl++)
			{
				if (i == l || j == l || k == l)continue;
				if (notBeLine(list, k, l, nk, nl))continue;
				if (main2())return 0;
			}
		}
	}
	return 0;
}


bool main2()
{
	for (ri = 0; ri < 16; ri++)for (nri = 0; nri < 4; nri++)
	{
		if (notBeRow(list, i, ri, ni, nri) || same(i, j, k, l, ri))continue;
		for (rj = 0; rj < 16; rj++)for (nrj = 0; nrj < 4; nrj++)
		{
			if (notBeRow(list, j, rj, nj, nrj) || same(i, j, k, l, rj))continue;
			if (ri == rj || (notBeLine(list, ri, rj, nri, nrj)))continue;
			for (rk = 0; rk < 16; rk++)for (nrk = 0; nrk < 4; nrk++)
			{
				if (notBeRow(list, k, rk, nk, nrk) || same(i, j, k, l, rk))continue;
				if (ri == rk || rj == rk || (notBeLine(list, rj, rk, nrj, nrk)))continue;
				for (rl = 0; rl < 16; rl++)for (nrl = 0; nrl < 4; nrl++)
				{
					if (notBeRow(list, l, rl, nl, nrl) || same(i, j, k, l, rl))continue;
					if (ri == rl || rj == rl || rk == rl || notBeLine(list, rk, rl, nrk, nrl))continue;
					if (main3())return true;
				}
			}
		}
	}
	return false;
}

bool main3()
{
	for (si = 0; si < 16; si++)for (nsi = 0; nsi < 4; nsi++)
	{
		if (notBeRow(list, ri, si, nri, nsi))continue;
		if (same(i, j, k, l, si) || same(ri, rj, rk, rl, si))continue;
		for (sj = 0; sj < 16; sj++)for (nsj = 0; nsj < 4; nsj++)
		{
			if (notBeRow(list, rj, sj, nrj, nsj))continue;
			if (same(i, j, k, l, sj) || same(ri, rj, rk, rl, sj))continue;
			if (si == sj || (notBeLine(list, si, sj, nsi, nsj)))continue;
			for (sk = 0; sk < 16; sk++)for (nsk = 0; nsk < 4; nsk++)
			{
				if (notBeRow(list, rk, sk, nrk, nsk))continue;
				if (same(i, j, k, l, sk) || same(ri, rj, rk, rl, sk))continue;
				if (si == sk || sj == sk || (notBeLine(list, sj, sk, nsj, nsk)))continue;
				for (sl = 0; sl < 16; sl++)for (nsl = 0; nsl < 4; nsl++)
				{
					if (notBeRow(list, rl, sl, nrl, nsl))continue;
					if (same(i, j, k, l, sl) || same(ri, rj, rk, rl, sl))continue;
					if (si == sl || sj == sl || sk == sl || notBeLine(list, sk, sl, nsk, nsl))continue;
					if (main4())return true;
				}
			}
		}
	}
	return false;
}


bool main4()
{
	for (jj1 = 0; jj1 < 16; jj1++)for (njj1 = 0; njj1 < 4; njj1++)
	{
		if (same(i, j, k, l, jj1) || same(ri, rj, rk, rl, jj1) || same(si, sj, sk, sl, jj1))continue;
		if (notBeRow(list, jj1, j, njj1, nj))continue;
		for (j2 = 0; j2 < 16; j2++)for (nj2 = 0; nj2 < 4; nj2++)
		{
			if (same(i, j, k, l, j2) || same(ri, rj, rk, rl, j2) || same(si, sj, sk, sl, j2))continue;
			if (notBeRow(list, sj, j2, nsj, nj2))continue;
			for (k1 = 0; k1 < 16; k1++)for (nk1 = 0; nk1 < 4; nk1++)
			{
				if (same(i, j, k, l, k1) || same(ri, rj, rk, rl, k1) || same(si, sj, sk, sl, k1))continue;
				if (notBeRow(list, k1, k, nk1, nk))continue;
				if (jj1 == j2 || j2 == k1 || k1 == jj1)continue;
				for (k2 = 0; k2 < 16; k2++)for (nk2 = 0; nk2 < 4; nk2++)
				{
					if (ni || nj || nk || nl || nri || nrj || nrk || nrl || nsi || nsj || nsk || nsl || njj1 || nj2 || nk1 || nk2)continue;
					if (same(i, j, k, l, k2) || same(ri, rj, rk, rl, k2) || same(si, sj, sk, sl, k2))continue;
					if (notBeRow(list, sk, k2, nsk, nk2))continue;
					if (jj1 == k2 || j2 == k2 || k1 == k2)continue;
					if (notBeLine(list, jj1, k1, njj1, nk1) || notBeLine(list, j2, k2, nj2, nk2))continue;
					out();
					return true;
				}
			}
		}
	}
	return false;
}

void out()
{
	cout << " " << setw(4) << i + 1 << setw(2) << ni << setw(4) << ri + 1 << setw(2) << nri << setw(4) << si + 1 << setw(2) << nsi << endl;
	cout << setw(4) << jj1 + 1 << setw(2) << njj1 << setw(4) << j + 1 << setw(2) << nj << setw(4) << rj + 1 << setw(2) << nrj << setw(4) << sj + 1 << setw(2) << nsj << setw(4) << j2 + 1 << setw(2) << nj2 << endl;
	cout << setw(4) << k1 + 1 << setw(2) << nk1 << setw(4) << k + 1 << setw(2) << nk << setw(4) << rk + 1 << setw(2) << nrk << setw(4) << sk + 1 << setw(2) << nsk << setw(4) << k2 + 1 << setw(2) << nk2 << endl;
	cout << " " << setw(4) << l + 1 << setw(2) << nl << setw(4) << rl + 1 << setw(2) << nrl << setw(4) << sl + 1 << setw(2) << nsl << endl << endl;
	system("pause>nul");
}


71颜色方块(5)84(6)97(7)108(8)_第3张图片
本来是j1的,后来改成了jj1,因为math.h里面有j1这个函数,所以编译有冲突。

运行结果:
71颜色方块(5)84(6)97(7)108(8)_第4张图片

对应答案:


84(6)
这一关可以转动。
if (ni || nj || nk || nl || nri || nrj || nrk || nrl || nsi || nsj || nsk || nsl || njj1 || nj2 || nk1 || nk2)continue;
这句话去掉,然后像上面一样,输入16个格子的信息,即可输出答案。

97(7)108(8)
一样的。

你可能感兴趣的:(71颜色方块(5)84(6)97(7)108(8))