#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#define maxn 5 + 10
#define ll long long
#define INF 1000000000
#define FOR(i, a, b) for(int i = a; i < b; ++i)
using namespace std;
bool change[3][3], light[maxn][maxn];
int n, m, minlen;
int ans[maxn*maxn], finalans[maxn*maxn];
void lit(int x, int y)
{
FOR(i, -1, 2) FOR(j, -1, 2)
{
int dx = x + i;
int dy = y + j;
if(x < 0 || y < 0 || x >= n || y >= m) continue;
if(change[i+1][j+1]) light[dx][dy] = !light[dx][dy];
}
return;
}
bool check(int x)
{
if(x < 0 || x >= n) return true;
FOR(i, 0, m) if(!light[x][i]) return false;
return true;
}
bool dfs(int cur, int len_ans)
{
int x = cur/m, y = cur - m*x;
if(x >= 2 && !check(x-2))
return false;
if(x == n-1 || cur == n*m)
{
if(check(n-1) && check(n-2)) {minlen = len_ans; return true;}
if(cur == n*m) return false;
}
if(dfs(cur+1, len_ans)) return true;
lit(x, y);
ans[len_ans] = cur + 1;
if(dfs(cur+1, len_ans+1)) return true;
lit(x, y);
return false;
}
int main()
{
int kase = 0;
while(scanf("%d%d", &n, &m), n+m)
{
memset(change, false, sizeof(change));
memset(light, false, sizeof(light));
minlen = INF;
char s[3];
FOR(i, 0, 3){ scanf("%s", s); FOR(j, 0, 3) change[i][j] = s[j] == '*'? true:false;}
//FOR(i, 0, 3) {FOR(j, 0, 3) printf(change[i][j] == true? "*":"."); printf("\n");}
printf("Case #%d\n", ++kase);
if(dfs(0, 0))
{
printf("%d", ans[0]);
FOR(i, 1, minlen) printf(" %d", ans[i]);
printf("\n");
}
else puts("Impossible.");
}
return 0;
}
uva 10318 - Security Panel
最新推荐文章于 2015-08-29 14:26:00 发布
