#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
int num;
bool flag;
//移动
int fx[5] = {-1,1,0,0,0};
int fy[5] = {0,0,-1,1,0};
bool graph[6][6] = {false};
//翻转
void flip(int x, int y)
{
int i;
for( i = 0; i < 5; i++)
if( x + fx[i] < 5 && y + fy[i] < 5 && x + fx[i] >= 1 && y + fy[i] >= 1)
graph[x+fx[i]][y+fy[i]] = !graph[x+fx[i]][y+fy[i]];
}
//检测是否达到要求
bool check( )
{
int i,j;
for( i = 1; i<5; i++)
for( j = 1; j<5; j++)
if(graph[i][j] != graph[1][1])
return false;
return true;
}
{
if(num == step && !flag)
{
flag = check();
return ;
}
//超过四行直接跳出
if(flag || x == 5)
return ;
flip(x,y);
if(y < 4)
dfs(x, y + 1, step + 1);
else//当枚举到第四列时候下次枚举从下一行第一个开始
dfs(x + 1, 1, step + 1);
//当枚举失败,回退时需要翻回
flip(x, y);
if( y < 4)
dfs(x, y+1, step);
else
dfs( x+1, 1, step);
return ;
}
int main()
{
char s;
int i,j;
num = 0;
flag = false;
for( i = 1; i < 5; i++)
for( j = 1; j < 5; j++)
{
cin>>s;
if(s == 'b')
graph[i][j] = true;
else
graph[i][j] = false;
}
for( num = 0 ; num <= 16; num++)
{
dfs(1,1,0);
if(flag ) break;
}
if(flag)
cout<<num<<endl;
else
cout<<"Impossible"<<endl;
return 0;
}
#include <string.h>
#include <stdio.h>
using namespace std;
int num;
bool flag;
//移动
int fx[5] = {-1,1,0,0,0};
int fy[5] = {0,0,-1,1,0};
bool graph[6][6] = {false};
//翻转
void flip(int x, int y)
{
int i;
for( i = 0; i < 5; i++)
if( x + fx[i] < 5 && y + fy[i] < 5 && x + fx[i] >= 1 && y + fy[i] >= 1)
graph[x+fx[i]][y+fy[i]] = !graph[x+fx[i]][y+fy[i]];
}
//检测是否达到要求
bool check( )
{
int i,j;
for( i = 1; i<5; i++)
for( j = 1; j<5; j++)
if(graph[i][j] != graph[1][1])
return false;
return true;
}
//深搜
void dfs( int x, int y, int step){
if(num == step && !flag)
{
flag = check();
return ;
}
//超过四行直接跳出
if(flag || x == 5)
return ;
flip(x,y);
if(y < 4)
dfs(x, y + 1, step + 1);
else//当枚举到第四列时候下次枚举从下一行第一个开始
dfs(x + 1, 1, step + 1);
//当枚举失败,回退时需要翻回
flip(x, y);
if( y < 4)
dfs(x, y+1, step);
else
dfs( x+1, 1, step);
return ;
}
int main()
{
char s;
int i,j;
num = 0;
flag = false;
for( i = 1; i < 5; i++)
for( j = 1; j < 5; j++)
{
cin>>s;
if(s == 'b')
graph[i][j] = true;
else
graph[i][j] = false;
}
for( num = 0 ; num <= 16; num++)
{
dfs(1,1,0);
if(flag ) break;
}
if(flag)
cout<<num<<endl;
else
cout<<"Impossible"<<endl;
return 0;
}