POJ 3279 (Fliptile)

Source: Internet
Author: User

Just in case, the original title and links are attached to the end of the text. So first the topic analysis:

"A Word test instructions"

Given the long and wide black and white chess board is full of chess pieces, each operation can reverse a position and its upper and lower left and right a total of five positions of the color of the pieces, to use the minimum number of flips to turn all the pieces to black the need to flip what pieces (this sentence is very long ...) )。

"Problem Analysis"

Staring at the strange topic for a long day to find and cows have no egg relationship. Cow IQ High milk production is what the devil say ...

This problem is just beginning to be placed under the category of search. But what does this have to do with search? Have no experience so think of a variety and search the method, the result did not think of solution, until looked at the Internet, not search ah have wood.

And then the online classification of the problem is simple question. Simple question: Konjac Konjac on his knees.

All right, start talking about solutions. First of all, according to the topic, each operation will affect the surrounding "pieces", and to make each 1 is reversed to 0, then we should always reverse 1 below the pieces to change 1 to 0.

So, when we deal with 1 to N-1 Line, the former n-1 line is already 0, and finally we just need to check that the last line is not all 0 can check whether the operation is correct. If it is correct and minimal, save it. The final output, everything.

Of course, because we want to change the position of the x+1 line where the 1 of the line x 0 needs to be reversed. And the whole rule is that we verify that a set of operations can be used to achieve the purpose, then we need to determine the operation before validation (no action how to verify it). )。

Therefore, according to the rule content, only need to be able to confirm the first line of the rollover situation, you can roll out all the following and verify the correct. So what needs to be done is to enumerate the first line of the case.

"Algorithmic Flow"

The whole code is divided into four parts, processing input part of nothing to say, Next is dowork work part.

The job that needs to be done is to enumerate all the cases in the first row and then calculate for each enumeration whether the validation meets the requirements and the number of times the reversal is required. Where the number of States in the first row can be optimized with left-shift operations (higher efficiency than POW), then the total number of enumerations is 1<<col times. Also, because this allows the state of a row to be saved by a number, the bitwise operation is still used to get the status of the rollover.

Once the enumerated first row state is saved, it can be handed to Calc for calculation and verification. Validation is done through the above rollover rules. where get (x, y) is the method (procedure) that gets the state of a position of 1.

The final check results are good. Here is the code.
Oh yes, I seem to have abused each macro definition ... Ignore it.

1#include <cstdio>2#include <cstdlib>3#include <iostream>4#include <cstring>5#include <queue>6 #defineEach (i,n) (int i=1;i<= (n); ++i)7 #defineINF 0x3f3f3f3f8 9 using namespacestd;Ten  One intRow,col; A intarr[ -][ -]; - intflip[ -][ -],ans[ -][ -]; - intdir[5][2] = { the     0,0,0,1,0,-1, -1,0,1,0  - }; -  - int Get(intXinty) { +     intc = arr[x+1][y+1]; -      for(inti =0;i<5; i++) { +         intDX = x + dir[i][0]; A         intDy = y + dir[i][1]; at         if(DX >=0&& Dx<row && dy>=0&& dy<Col) { -c+=Flip[dx][dy]; -         } -     } -     returnc&1;//with flip state, if odd return 1 - } in  - intCalc () { to      foreach (i,row-1) { +          for(intj =0; j<col;j++) { -             if(Get(I-1, j)) + +Flip[i][j]; the         } *     } $      for(intI=0; i<col;i++) {//Check last linePanax Notoginseng         if(Get(row-1, i))return 0; -     } the     intCNT =0; +      for(intI=0; i<row;i++) {//Number of statistics rollover A          for(intj=0; j<col;j++) { theCNT + =Flip[i][j];  +         } -     } $     returnCNT; $ } -  -  the voiddoWork () { -     intCNT =INF;Wuyi      for(intI=0;i< (1<<col); i++) {//from 0000 to 1111 thememset (Flip,0,sizeof(flip)); -          for(intj=0; j<col;j++) { Wuflip[0][col-j-1] = (i>>j) &1;//get POS state form binary number -         } About         intnum =Calc (); $         if(num<cnt && num!=0) { -CNT =num; -memcpy (Ans,flip,sizeof(flip)); -         } A     } +     if(Cnt==inf) printf ("impossible\n"); the     Else { -          for(intI=0; i<row;i++) {   $printf"%d", ans[i][0]);  the              foreach (j,col-1) { theprintf"%d", Ans[i][j]); the             } theprintf"\ n");  -         } in     } the } the  About intMain () { the      the      while(~SCANF ("%d%d",&row,&col)) { thememset (arr,0,sizeof(arr)); +          forEach (i,row) { -              forEach (j,col) { thescanf"%d",&arr[i][j]);Bayi             } the         } the          - doWork (); -     } the      the}


Title Link: fliptile (POJ 3279)

Topic Properties: Enumeration Simple optimizations (I don't know if it's a search ...) )

Related Topics: poj3276

Original title:
"Desc"
Farmer John knows that an intellectually satisfied cow was a happy cow who would give more milk. He has arranged a brainy activity for cows in which they manipulate an mxn grid (1≤m≤15; 1≤n≤15) of square tiles , each of the which are colored black on one side and white in the other side.

As one would guess, when a-white tile was flipped, it changes to black; When a single black tile was flipped, it changes to white. The cows is rewarded when they flip of the tiles so, each tile had the white side face up. However, the cows has rather large hooves and when they try-to-flip a certain tile, they also flip all the adjacent tiles (Tiles that share a full edge with the flipped tile). Since the flips is tiring, the cows want to minimize the number of flips they has to make.

Help the cows determine the minimum number of flips required, and the locations-to-flip to achieve that minimum. If There is multiple ways to achieve the task with the minimum amount of flips, return the one with the least lexicograph ical ordering in the output when considered as a string. If The task is impossible, print one line with the word "impossible".
"In"
Line 1:two space-separated integers:m and N
Lines 2.. M+1:line i+1 describes the colors of the row I of the grid with N space-separated integers which is 1 for BL ACK and 0 for White
"Out"
Lines 1.. M:each line contains N space-separated integers, each specifying what many times to flip this particular location.
"Sampin"
4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
"Sampout"
0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

POJ 3279 (Fliptile)

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.