http://poj.org/problem?id=3279
Obviously, each one on the only need is 0 or 1,
Iterating through all the values of the first row may be, (1<<15, time enough) for each of the values possible:
For line 0-n-2, you can determine the next line because the previous line and the row are determined
Finally check if line n-1 satisfies the condition
#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; int maz[15][15];int op[15][15];int ans[15][15],mn;int n,m;void getop (int sta) {memset (op,0,sizeof (OP)); for (int i=0;i<n;i++) {if (sta& (1<<i)) {op[0][i]=1; }}}int GetOne () {int num=0; for (int. i=0;i<n;i++) {for (int j=0;j<m;j++) {num+=op[i][j]; }} return num;} const int Dx[4]={0,-1,0,0};const int dy[4]={0,0,1,-1};bool in (int x,int y) {return x>=0&&x<n&& ; y>=0&&y<m;} BOOL Judge (int x,int y) {int num=maz[x][y]; for (int i=0;i<4;i++) {int tx=x+dx[i],ty=y+dy[i]; if (in (Tx,ty)) num+=op[tx][ty]; } return (num&1) ==0;} int main () {scanf ("%d%d", &n,&m); for (int i=0;i<n;i++) foR (int j=0;j<m;j++) scanf ("%d", maz[i]+j); int mn=0x7ffffff; for (int sta=0;sta< (1<<n); sta++) {getop (STA); for (int i=0;i<n-1;i++) {for (int j=0;j<m;j++) {if (!judge (i,j)) { Op[i+1][j]=1; }}} bool Fl=true; for (int j=0;j<m;j++) {if (!judge (n-1,j)) {fl=false; Break }} if (fl) {int num=getone (); if (NUM<MN) {for (int i=0;i<n;i++) {copy (op[i],o P[i]+m,ans[i]); } Mn=num; } }} if (Mn<=n*m) for (int i=0;i<n;i++) {for (int j=0;j<m;j++) { printf ("%d%c", ans[i][j],j==m-1? \ n ': '); }} else puts ("impossible"); return 0;}
POJ 3279 Fliptile State compression, thinking difficulty: 2