POJ1222熄灯问题C++

#include<iostream>
#include<cstring>
using namespace std;
void press1(bool (*b)[6],bool (*a)[6])
{
	int i;
	for ( i = 0;i < 6;i++)
	{
		if (b[0][i] == 1)
		{
			if (i != 0 && i != 5)
			{
				a[0][i] = !a[0][i];
				a[0][i - 1] = !a[0][i - 1];
				a[0][i + 1] = !a[0][i + 1];
				a[1][i] = !a[1][i];
			}
			else if (i == 0)
			{
				a[0][i] = !a[0][i];
				a[0][i + 1] = !a[0][i + 1];
				a[1][i] = !a[1][i];
			}
			else
			{
				a[0][i] = !a[0][i];
				a[0][i - 1] = !a[0][i - 1];
				a[1][i] = !a[1][i];
			}
		}
	}
}
void press2(bool (*a)[6], int i, int j)
{
	if (i == 3 && j == 0)
	{
		a[i + 1][j] = !a[i + 1][j];
		a[i][j] = !a[i][j];
		a[i + 1][j + 1] = !a[i + 1][j + 1];
	}
	else if (i == 3 && j == 5)
	{
		a[i + 1][j] = !a[i + 1][j];
		a[i][j] = !a[i][j];
		a[i + 1][j - 1] = !a[i + 1][j - 1];

	}
	else if (j == 0)
	{
		a[i + 1][j] = !a[i + 1][j];
		a[i][j] = !a[i][j];
		a[i + 1][j + 1] = !a[i + 1][j + 1];
		a[i + 2][j] = !a[i + 2][j];
	}
	else if (j == 5)
	{
		a[i + 1][j] = !a[i + 1][j];
		a[i][j] = !a[i][j];
		a[i + 2][j] = !a[i + 2][j];
		a[i + 1][j - 1] = !a[i + 1][j - 1];
	}
	else
	{
		a[i + 1][j] = !a[i + 1][j];
		a[i][j] = !a[i][j];
		a[i + 1][j + 1] = !a[i + 1][j + 1];
		a[i + 2][j] = !a[i + 2][j];
		a[i + 1][j - 1] = !a[i + 1][j - 1];
	}
}
int getbit(char c, int i)
{
	return (c >> i) & 1;
}
bool a[5][6], b[5][6],c1[5][6];
int main()
{
	int n, k = 1,flag;
	cin >> n;
	for (int k1 = 1;k1 <= n;k1++) 
	{
		for (int i = 0;i < 5;i++)
		{
			for (int j = 0;j < 6;j++)
			{
				cin >> a[i][j];
			}
		}
		char c;
		for (int n = 0;n < 64;n++)
		{
			for (int i = 0;i < 5;i++)
			{
				for (int j = 0;j < 6;j++)
				{
					c1[i][j]= a[i][j];
				}
			}
			memset(b, 0, sizeof(b));
			c = n;
			for (int i = 0;i < 6;i++)
			{
				b[0][5 - i] = getbit(c, i);
			}
			press1(b, c1);
			for (int i = 0;i < 4;i++)
			{
				for (int j = 0;j < 6;j++)
				{
					if (c1[i][j] == 1)
					{
						press2(c1, i, j);
						b[i + 1][j] = 1;
					}
				}
			}
			flag = 1;
			for (int i = 0;i < 6;i++)
			{
				if (c1[4][i] != 0)
					flag = 0;
			}
			if (flag)
			{
				cout << "PUZZLE #" << k << endl;
				k++;
				for (int i = 0;i < 5;i++)
				{
					for (int j = 0;j < 6;j++)
					{
						cout << b[i][j] << " ";
					}
					cout << endl;
				}
			}
		}
	}
	return 0;
}

上一篇:字符类型


下一篇:关系数据库与Sql