Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2010 13:37
Show Gist options
  • Select an option

  • Save anonymous/414487 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/414487 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
int btn[5][5];
int tbl[3][3];
void init();
void onoff(int, int);
int check(int, int);
int pow[25];
int main() {
pow[1] = 2;
pow[2] = 4;
pow[3] = 8;
pow[4] = 16;
pow[5] = 32;
pow[6] = 64;
pow[7] = 128;
pow[8] = 256;
pow[9] = 512;
pow[10] = 1024;
pow[11] = 2048;
pow[12] = 4096;
pow[13] = 8192;
pow[14] = 16384;
pow[15] = 32768;
pow[16] = 65536;
pow[17] = 131072;
pow[18] = 262144;
pow[19] = 524288;
pow[20] = 1048576;
pow[21] = 2097152;
pow[22] = 4194304;
pow[23] = 8388608;
pow[24] = 16777216;
pow[25] = 33554432;
int r, c, cases = 1;
while (cin >> r >> c && r && c) {
int b = 26;
int ans = -1;
char ch;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cin >> ch;
if (ch == '*') {
tbl[i][j] = 1;
} else {
tbl[i][j] = 0;
}
}
}
for (int s = 0; s < pow[r * c]; s++) {
init();
int cnt = 1;
int now = 0;
int flag = 0;
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (i >= 2) {
for (int k = 0; k < c; k++) {
if (btn[i - 2][k] == 0) {
flag = 1;
break;
}
}
}
if ((s % pow[cnt++]) >> (cnt - 1) == 1) {
onoff(i, j);
now++;
if (now >= b) {
flag = 1;
}
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
break;
}
}
if (flag == 1) {
continue;
}
if (check(r, c)) {
b = now;
ans = s;
}
}
cout << "Case #" << cases++ << endl;
if (ans == -1) {
cout << "Impossible." << endl;
} else {
int first = 0;
for (int i = 1; i <= r * c; i++) {
if ((ans % pow[i]) >> (i - 1) == 1) {
if (first == 0) {
cout << i;
first = 1;
} else {
cout << " " << i;
}
}
}
cout << endl;
}
}
return 0;
}
void init() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
btn[i][j] = 0;
}
}
}
void onoff(int x, int y) {
if (tbl[0][0] == 1 && x - 1 >= 0 && y - 1 >= 0) {
btn[x - 1][y - 1] = !btn[x - 1][y - 1];
}
if (tbl[0][1] == 1 && x - 1 >= 0) {
btn[x - 1][y] = !btn[x - 1][y];
}
if (tbl[0][2] == 1 && x - 1 >= 0 && y + 1 <= 4) {
btn[x - 1][y + 1] = !btn[x - 1][y + 1];
}
if (tbl[1][0] == 1 && y - 1 >= 0) {
btn[x][y - 1] = !btn[x][y - 1];
}
if (tbl[1][1] == 1) {
btn[x][y] = !btn[x][y];
}
if (tbl[1][2] == 1 && y + 1 <= 4) {
btn[x][y + 1] = !btn[x][y + 1];
}
if (tbl[2][0] == 1 && x + 1 <= 4 && y - 1 >= 0) {
btn[x + 1][y - 1] = !btn[x + 1][y - 1];
}
if (tbl[2][1] == 1 && x + 1 <= 4) {
btn[x + 1][y] = !btn[x + 1][y];
}
if (tbl[2][2] == 1 && x + 1 <= 4 && y + 1 <= 4) {
btn[x + 1][y + 1] = !btn[x + 1][y + 1];
}
}
int check(int r, int c) {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
if (btn[i][j] == 0) {
return 0;
}
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment