poj3279 Search Detailed

Source: Internet
Author: User

This search problem and most of the questions are not the same, there is no obvious idea, the state of the lattice between each other, can only be enumerated in the first row, row by line down search.

Detailed

1. How to search: if you search from top to bottom, whether the current line needs to be reversed depends on the state of the previous row, by flipping the current row to 0, instead of flipping over the previous line to 0, look at the current line's state to determine whether or not you need to flip, otherwise it will continue to affect the previous row . So enumerate the first line all the state, search to the end of the last line, if you can guarantee that the last line is 0, then the scheme can, otherwise redefine the state of the first row, continue to search, to find the least number of reversal scenarios.

2. Minimum dictionary order: Search by dictionary order from small to large (how to see below)

Tips:

How to enumerate: If a row has n squares, and each lattice has two states, then there are altogether 2^n species, such as n = 6;001100, which is the 2nd, 3-bit (I starting from 0) flips. The dictionary order from small to large is I from 0 to 1<<n traversal (from the last one in a row to consider flipping)

Gets the state of the first row definition I>>j &1:i>>j is the status of I state under J-state, why &1? Because the definition of 0 is not required to reverse, in the Get function to determine whether a certain row of the flip also need to pay attention to the get function enumeration up and down and its own 5 lattice, accumulate these several lattice of the flip value, if the 5 lattice has s in the flip over, then this grid is equivalent to the original lattice flipped s times , plus the original state, and 1 phase is the value of whether this lattice needs to be flipped. Let's think about it. If the initial problem is set to (0 need to flip, 1 does not need), it is the same as 0.

A bit around, whether the current line needs to be reversed depends on whether the previous line is black!!

1#include <stdio.h>2#include <iostream>3#include <string>4#include <algorithm>5#include <string>6#include <string.h>7#include <math.h>8#include <queue>9 #defineMAXN 16Ten #defineINF 1<<30 One using namespacestd; A  - intA[MAXN][MAXN],FLIP[MAXN][MAXN],ANS[MAXN][MAXN]; - intM,n; the intdx[5] = {0,1,0,-1,0}; - intdy[5] = {-1,0,0,0,1}; - int Get(intXinty) - { +     intCNT = A[x][y];//The current is T, the State is equivalent to (t+n)%2 after it has been turned over n times -      for(inti =0;i<5; i++) +     { A         intA = x +Dx[i]; at         intB = y +Dy[i]; -         if(a>=0&&a<m&&b>=0&&b<N) -CNT + =Flip[a][b]; -     } -     returncnt&1; - } in intcal () - { to     inti,j; +      for(i =1; i<m;i++) -     { the          for(j =0; j<n;j++) *         { $             if(Get(I-1, J))Panax NotoginsengFLIP[I][J] =1; -         } the     } +      for(i =0; i<n;i++) A     { the         if(Get(M-1, i)) (I-1,J) is black, then reversed (i,j) +             returnINF; -     } $     intCNT =0; $       for(i =0; i<m;i++) -          for(j =0; j<n;j++) -cnt+=Flip[i][j]; the  -     returnCNT;Wuyi } the intMain () - { WuFreopen ("Caicai.txt","R", stdin); -      while(cin>>m>>N) About     { $         inti,j; -          for(i =0; i<m;i++) -              for(j =0; j<n;j++) -Cin>>A[i][j]; A         intCNT =INF; +          for(i =0; I <1<<n; i++) the         { -memset (Flip,0,sizeof(flip)); $              for(j =0; j<n;j++) the             { theflip[0][J] = i>>j &1; the             } the             inttemp =cal (); -             if(Temp <CNT) in             { theCNT =temp; the                  for(intp =0;p <m;p++) About                      for(intQ =0; q<n;q++) theANS[P][Q] =Flip[p][q]; the             } the  +         } -         if(CNT = =INF) thecout<<"impossible\n";Bayi         Else{ the              for(i =0; i<m;i++) the             { -                  for(j =0; j<n;j++) -                 { thecout<<Ans[i][j]; the                     if(j!=n-1) thecout<<' '; the                     Else -cout<<'\ n'; the                 } the             } the         }94  the     } the     return 0; the}

poj3279 Search Detailed

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.