POJ 3279 Fliptile State compression, thinking difficulty: 2

Source: Internet
Author: User

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&&amp ; 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&LT;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.