代码:
//#include<iostream>
#include<string.h>
#include<stdio.h>
//using namespace std;
int step=0;
int visit[5][5]= {0};
char a[5][5];
bool b[5][5];
bool c[5][5];
int k=0;
struct Str
{
int x;
int y;
};
Str d[50];
void roll(int row,int col)
{
int i,j;
for(i=1; i<=4; i++)
{
b[row][i]=!b[row][i];
b[i][col]=!b[i][col];
}
b[row][col]=!b[row][col];
}
bool Judge()
{
int i,j;
for(i=1; i<=4; i++)
for(j=1; j<=4; j++)
if(b[i][j]==0)
return 0;
return 1;
}
void huanyuan()
{
int i,j;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
b[i][j]=c[i][j];
}
}
}
void dfs(int depth)
{
int i,j;
if(depth==step)
{
if(Judge()==1)
{
k=1;
printf("%d\n",step);
//cout<<step<<endl;
for(i=0; i<depth; i++)
{
printf("%d %d\n",d[i].x,d[i].y);
//cout<<d[i].x<<' '<<d[i].y<<endl;
}
return ;
}
return ;
}
for(i=1; i<=4; i++)
{
for(j=1; j<=4; j++)
{
if(visit[i][j]==0&&k==0)
{
visit[i][j]=1;
d[depth].x=i;
d[depth].y=j;
roll(i,j);
dfs(depth+1);
visit[i][j]=0;
roll(i,j);
}
}
}
}
int main()
{
int i,j;
memset(visit,0,sizeof(visit));
for(i=1; i<=4; i++)
{
for(j=1; j<=4; j++)
{
scanf("%c",&a[i][j]);
// cin>>a[i][j];
if(a[i][j]=='+')
b[i][j]=0;
else
b[i][j]=1;//开
c[i][j]=b[i][j];
}
getchar();
}
for(step=1; step<=16; step++)
{
dfs(0);
if(k)
break;
}
return 0;
}