Problem G
Security Panel
Input: standard input
Output: standard output
Time Limit: 2 seconds
Memory Limit: 32 MB
Advanced Control Mechanisms (ACM) produces sophisticated electronic locks and security devices.
The company's most recent invention is a panel of illuminated buttons in r rows and c columns. The buttons are numbered left-to-right, top-to-bottom, starting at 1 in the upper-left corner. Each button has two states: lit and unlit. Initially, all buttons are unlit. Pressing a button switches the state of some buttons from lit to unlit (or vice-versa) according to a 3x3 pattern. Pressing a button on a panel applies the pattern centered on that button. To unlock the panel, the buttons must be pressed in such a way so as to light all of them.
For example, consider the following pattern where pressing a button switches the state of the button pressed, as well as the button above and the buttons to the upper and lower left.
If we use this pattern on a 2x3 panel, then pressing buttons 2, 5, and 6 will light all the buttons. If pressed in that order, the state changes of the panel are: (Light green means lights are off and vice versa)
Input
Each input case will begin with the number of rowsand columns on the panel, 1 <= r, c <= 5 alone on a line. The next three lines describe how pressing a button will affect the nearby lights. This description consists of a 3x3 character grid, where the character "*" indicates that the light in that position switches state (from lit to unlit or from unlit to lit) while "." means its state remains unchanged.
Input ends with 0 0 alone on a line.
Output
For each input case, output "Case #" followed by the number of the case. If there is no way to turn on all the lights, print "Impossible." If it is possible to turn on the lights, output the buttons to be pressed in increasing order, separated by single space. Output the answer that requires the fewest number of buttons possible to be pressed. If there is more than one correct solution, anyone will do.
Sample Input
2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0
Sample Output
Case #1
2 5 6
Case #2
2 3 4 7 9 12 14 17 18 19
Case #3
1 3
Case #4
Impossible.
(The Decider Contest, Source: University of Alberta Local Contest)
目似於,但是的影限定十字。
而定影的情,有有可能全成全。
首先最大只有 5x5,考成一 int,之後的反使用 XOR 算即可。
同理每一格下的遮罩,搜索可能高 O(2^25),
依序,前排的必然已全了,藉此作剪枝。
#include <stdio.h>
#include <algorithm>
using namespace std;
int mark[10][10];
int row[10];
int n, m;
int ch[105], found;
void dfs(int x, int y, int state, int step) {
if(found) return;
if(y == m) {
  if(x) {
if((state&row[x-1]) != row[x-1])
return;
}
x++, y = 0;
}
int i, j;
/*for(i = 0; i < n; i++, puts(""))
for(j = 0; j < m; j++)
printf("%d", (state>>(i*5+j))&1);
printf("-------%d %d %d\n", x, y, step);*/
if(x == n) {
if((state&row[x-1]) != row[x-1])
return;
found = 1;
for(i = 0; i < step; i++) {
if(i) putchar(' ');
printf("%d", ch[i]+1);
}
puts("");
return;
}
dfs(x, y+1, state, step);
ch[step] = x*m+y;
dfs(x, y+1, state^mark[x][y], step+1);
}
int main() {
/*freopen("in.txt","r+t",stdin);
freopen("out2.txt","w+t",stdout);*/
char g[5][5];
int i, j, k, p, q, r;
int a, b;
int cases = 0;
while(scanf("%d %d", &n, &m) == 2 && n) {
for(i = 0; i < 3; i++)
scanf("%s", g[i]);
int x, y;
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
int &val = mark[i][j];
val = 0;
for(p = 0, a = -1; p < 3; p++, a++) {
for(q = 0, b = -1; q < 3; q++, b++) {
x = i+a, y = j+b;
if(x < 0 || y < 0 || x >= n || y >= m)
continue;
if(g[p][q] == '*')
val |= 1<<(x*5+y);
}
}
}
}
/*for(i = 0; i < n; i++, puts(""))
for(j = 0; j < m; j++)
printf("%d ", mark[i][j]);*/
for(i = 0; i < n; i++) {
row[i] = 0;
for(j = 0; j < m; j++)
row[i] |= 1<<(i*5+j);
}
found = 0;
printf("Case #%d\n", ++cases);
dfs(0, 0, 0, 0);
if(found == 0)
puts("Impossible.");
}
return 0;
}
/*
2 3
**.
.*.
*..
4 5
.*.
***
.*.
2 2
...
.**
...
4 3
*.*
...
..*
0 0
*/