代码拉取完成,页面将自动刷新
#include <iostream>
#include <memory>
#include <string>
#include <cstring>
using namespace std;
#define ROWS 3
#define COLS 3
char oriLights[ROWS];
char lights[ROWS];
char result[ROWS];
int GetBit(char 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, char result[])
{
cout << "PUZZLE #" << t << endl;
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << GetBit(result[i], 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 nmax = 1 << COLS;
for (int n = 0; n < nmax; ++n) {
memcpy(lights, oriLights, sizeof(oriLights));
switchs = n;
for (int i = 0; i < ROWS; ++i) {
result[i] = switchs;
for (int j = 0; j < COLS; ++j) {
if (GetBit(switchs, j)) { // the 1 in a row bingo, then flip it and all adjesants.
if (j > 0) { // if middles
FlipBit(lights[i], j-1); // flip its left.
}
FlipBit(lights[i], j); // flip itself.
if (j < COLS-1) { // if middles
FlipBit(lights[i], j+1); // flip its right.
}
}
}
if (i < ROWS-1) {
lights[i+1] ^= switchs;
}
switchs = lights[i];
}
if (lights[ROWS-1] == 0) {
OutputResult(t, result);
break;
}
}
}
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。