这个题与poj 1753的思想是一样的这里就不解释了;这里有点问题就是当打不开怎么办该输出什么????
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<set> #include<vector> using namespace std; class Node { public: int x, y; }; Node node[20]; int step; bool flag; void Opt( bool map[][4],int x,int y ) { for( int i = 0 ; i < 4 ; i ++ ) { if( y != i )//避免map[x][y]转换2次回到原来的状态 map[x][i] = !map[x][i]; map[i][y] = !map[i][y]; } } bool Judge( bool map[][4] ) { for( int i = 0 ; i < 4 ; i ++ ) { for( int j = 0; j < 4 ; j ++ ) { if( map[i][j] ==false ) return false; } } return true; } void DFS( bool map[][4] , int x, int y , int n ) { if( n==step ) { flag = Judge( map ); return ; } if( flag || x ==4 ) return ; Opt( map , x , y ); node[n].x = x ; node[n].y = y; if( y < 3 ) { DFS( map , x , y + 1, n+ 1 ); } else { DFS( map , x + 1 , 0 , n + 1 ); } Opt( map , x ,y ); if( y < 3 ) DFS( map , x , y + 1, n ); else DFS( map , x + 1 , 0 , n ); } int main( ) { bool map[4][4]; char c[4][4]; while( scanf( "%s",c[0] )==1 ) { flag = false; for( int i = 1 ; i < 4 ; i ++ ) { scanf( "%s",c[i] ); } for( int i = 0 ; i < 4 ; i ++ ) { for( int j = 0 ; j < 4 ; j ++ ) { if( c[i][j] =='+' ) map[i][j] = false; else map[i][j] = true; } } for( step = 0 ; step <= 16 ; step ++ ) { DFS( map , 0 , 0 , 0 ); if( flag ) break; } printf( "%d\n",step ); for( int i = 0 ; i < step ; i ++ ) { printf( "%d %d\n",node[i].x+1, node[i].y + 1 ); } } //system( "pause" ); return 0; }