代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
using namespace std;
#define ROWS 5
#define COLS 6
char oriLights[ROWS];
char lights[ROWS];
char result[ROWS];
int GetBit(int c, int i)
{
return (c >> i) & 1;
}
void SetBit(char & c, int i, int v)
{
if (v) {
c |= (1 << i);
}
else {
c &= ~(1 << i);
}
}
void FlipBit(char & c, int i)
{
c ^= (1 << i);
}
void OutputResult(int t, int result)
{
cout << "PUZZLE #" << t << endl;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << GetBit(result, i*COLS+j);
if (j < COLS) {
cout << " ";
}
}
cout << endl;
}
}
int main()
{
char switchs;
int T;
cin >> T;
for (int t = 1; t <= T; ++t) {
memset(oriLights, 0, sizeof(oriLights));
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
int s;
cin >> s;
SetBit(oriLights[i], j, s);
}
}
int tryMax = 1 << (COLS*ROWS);
for (int tryNum = 0; tryNum < tryMax; ++tryNum) {
memcpy(lights, oriLights, sizeof(oriLights)); // lights would be changed in each loop.
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
if (GetBit(tryNum, row*COLS+col)) {
FlipBit(lights[row], col); // flip center
if (row == 0) {
if (col == 0)
{
FlipBit(lights[row], col+1);
FlipBit(lights[row+1], col);
} else if (col == COLS-1)
{
FlipBit(lights[row], col-1);
FlipBit(lights[row+1], col);
} else {
FlipBit(lights[row], col-1);
FlipBit(lights[row], col+1);
FlipBit(lights[row+1], col);
}
} else if (row == ROWS-1)
{
if (col == 0)
{
FlipBit(lights[row], col+1);
FlipBit(lights[row-1], col);
} else if (col == COLS-1)
{
FlipBit(lights[row], col-1);
FlipBit(lights[row-1], col);
} else
{
FlipBit(lights[row], col-1);
FlipBit(lights[row], col+1);
FlipBit(lights[row-1], col);
}
} else {
if (row == 0)
{
FlipBit(lights[row-1], col);
FlipBit(lights[row+1], col);
FlipBit(lights[row], col+1);
} else if (col == COLS-1)
{
FlipBit(lights[row-1], col);
FlipBit(lights[row+1], col);
FlipBit(lights[row], col-1);
} else {
FlipBit(lights[row-1], col);
FlipBit(lights[row+1], col);
FlipBit(lights[row], col-1);
FlipBit(lights[row], col+1);
}
}
}
}
}
bool isOK = true;
for (int i = 0; i < ROWS; i++)
{
if (lights[i] != 0)
{
isOK = false;
break;
}
}
if (isOK)
{
OutputResult(t, tryNum);
break;
}
}
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。