"Shanghai Jiaotong University OJ" Smooth Toothpick Bag (modified) (enumeration + simulation)

Source: Internet
Author: User

1391. The smooth Toothpick Bag (change) title description

The last attempt to hide the display failed, so the smooth is not happy. This time, he changed a toothpick bag and made a comeback. This time he chose the 10-shaped toothpick bag (that is, each paste of a toothpick bag will affect 5 areas, when the center point must be affixed to the monitor, if the center point on the edge, there will be some blocks exposed to the display, but it does not matter, the smooth will be cut out of these blocks), so he felt this time will be able to

Affixed and tore, tore and affixed, startled a moment later, he found the entire display is uneven, very uncomfortable. Because of obsessive-compulsive disorder, he decided to put every piece of the area into an even-numbered toothpick bag (that is, it could overlap), and he now wants to know how many toothpick bags he needs at least.

Or for that reason, the job is yours.

Input format

Enter the first line with two integers, H and W (1<=h,w<=16), representing the display size.

Next there are h lines, each row w integer L (0<=l<=10000), which represents the number of toothpick bags already in each region.

Output format

The minimum number of toothpick bags required is output-1 if the requirement cannot be completed anyway.

Sample INPUT1
1 20 1
Sample OUTPUT1
-1
Sample Input2
2 20 12 3
Sample Output2
2

The exam time thinking set, thought is also moving back, but looks good trouble, so there is no courage to try, really should not. In fact, the problem is to use brute force solution, you can find that when the state of the previous line is determined, the next line has a unique solution, that is, the last behavior of the singular line must be paved, then we just enumerate the first row of all States, to see the state under the simulation can not meet the requirements, to find a minimum is good, Enumeration The first line can write a W-heavy loop, can also be recursive (DFS), or directly with the compressed state (binary) enumeration, the simulation is a row of lines according to the state of the previous row changes the state of the row and the next row.

Code:
1#include <iostream>2 using namespacestd;3 4 intbegin[ -][ -];5 intnowline[ -];6 intnextline[ -];7 intlastline[ -];8 intMain () {9     intH,w,min =9999999;Ten      OneCin>>h>>W; A      for(inti =0; I < h;++i) -          for(intj =0; J < W;++j) cin>>Begin[i][j]; -  the     if(h==1){ -         inti,cnt =0; -          for(i =0; i < W;++i)if(begin[0][i] &1){ -             if(i+1< W) begin[0][i+1]++; +             if(i+2< W) begin[0][i+2]++; -cnt++; +         } A         if(begin[0][w-1] &1) cout<<-1<<Endl; at         Elsecout<<cnt<<Endl; -         return 0; -     } -      for(inti =0; I <1<<w;++i) {//enumerate the first row -       -          for(intj =0; J < W;++j) {//Initialize inNOWLINE[J] = begin[0][j]; -NEXTLINE[J] = begin[1][j]; to}//For (int j = 0;j < W;++j) cout<<nowline[j]<< ";cout<<endl; +         //For (int j = 0;j < W;++j) cout<<nextline[j]<< ";cout<<endl; -         intt =1, cnt =0; the           for(intj =0; J < w;++j,t<<=1){ *              if(I &t) { $nextline[j]++;Panax Notoginsengnowline[j]++; -                  if(J >0) nowline[j-1]++; the                  if(j<w-1) nowline[j+1]++; +cnt++; A              } the         } +          -          for(intj =1; J < H1; j + +) {//Start Simulation $              for(intK =0; k < w;++k) { $LASTLINE[K] =Nowline[k]; -NOWLINE[K] =Nextline[k]; -Nextline[k] = begin[j+1][k]; the             } -              for(intK =0; k < w;++k) {Wuyi                 if(Lastline[k] &1){ thenextline[k]++; -nowline[k]++; Wu                     if(k>0) nowline[k-1]++; -                     if(k<w-1) nowline[k+1]++; Aboutcnt++; $                 } -             } -         } -         //Process the second -to-last line A          for(intj =0; J < W;++j)if(Nowline[j] &1) { +nextline[j]++; the             if(j>0) nextline[j-1]++; -             if(j< W1) nextline[j+1]++; $cnt++; the         } the         //Judging whether it is feasible the         intj =0; the          for(j =0; J < w;++j) { -             if(Nextline[j] &1) { in                  Break; the             } the         } About         if(J==w && cnt<min) {min =CNT;} the      } the       the      if(Min <9999999) cout<<min<<Endl; +      Elsecout<<-1<<Endl; -      the     return 0;Bayi}
View Code

"Shanghai Jiaotong University OJ" Smooth Toothpick Bag (modified) (enumeration + simulation)

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.