1647: [Usaco2007 open]fliptile turn lattice game

Source: Internet
Author: User

1647: [Usaco2007 open]fliptile Check game time limit:5 Sec Memory limit:64 MB
submit:423 solved:173
[Submit] [Status] [Discuss] Description

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 M x N grid (1 <= M <= 1 <= N <= 15) of square tiles, each of which are colored black on one side and white on 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 lexicographical ordering in the output when considered as a string. If The task is impossible, print one line with the word "impossible".

John knows that high-intelligence and happy cows produce particularly high amounts of milk. So he did a flip-flop puzzle game to entertain cows. On the skeleton of an MXN (1≤m,n≤15), each lattice has a tile that can be flipped. The side of the tile is black, and the other side is white. Flipping a tile can make the black white, or the white to black. However, the cows ' hooves are so huge and clumsy that they flip a tile and the adjacent tiles with the public side are flipped. So how many times do these cows need to flip at least to make all the tiles turn white? As Gao can do, the result of the output dictionary order is minimal (the result is treated as a string). If not, output "impossible". Input

* Line 1:two space-separated integers:m and N

* Lines 2..m+1:line i+1 describes the colors (left to right) of the row I of the grid with N space-separated integers which a Re 1 for Black and 0 for white

In line 1th, enter M and N, then m row n columns, and enter the tile state at the beginning of the game. 0 means white face upward, and 1 for black. Output

* Lines 1..m:each line contains N space-separated integers, each specifying what many times to flip, particular Locati On.

Output m lines, each row of n a space-separated integer, indicating how many times the corresponding lattice is flipped. Sample Input4 4
1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1


Sample Output0 0 0 0
1 0 0 1
1 0 0 1
0 0 0 0

OUTPUT DETAILS:

After flipping at row 2 column 1, the board would look like:
0 0 0 1
1 0 1 0
1 1 1 0
1 0 0 1

After flipping at row 2 column 4, the board would look like:
0 0 0 0
1 0 0 1
1 1 1 1
1 0 0 1

After flipping at row 3 column 1, the board would look like:
0 0 0 0
0 0 0 1
0 0 1 1
0 0 0 1

After flipping at row 3 column 4, the board would look like:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Another solution might be:
0 1 1 0
0 0 0 0
0 0 0 0
0 1 1 0
But this solution is lexicographically higher than the solution above.
HINT Source

Silver

The problem: Remember, this is the embarrassing God (embarrassing God =jszkc, save the Orz on the eve of saving the RP) out of the Noip simulation of the T3, then I only know \ (O ({2}^{nm}) \) Pure violence enumeration, but in fact, do not--

In fact, it is an enumeration, but actually as long as \ (O ({2}^{n}) \) enumeration, see \ (n\), is clearly the first line of enumeration, in fact, the first row of the decision will directly determine the next row of the decision, and then the next line affects the next row, that is, the decision of the first row determines the overall Then, based on the first line of binary poor lift out of the simulation, and then determine whether the scheme is feasible, and then Daleitai recorded

One might make a dictionary comparison in the process, because the dictionary is the smallest, but there is one more detail that you seem to overlook--our binary poor lifting from small numbers to large numbers in itself is in line with the dictionary order rise, for a first row decision-making situation, there is only one legal solution, so there is no need to compare, direct " Preconceived "can

1/**************************************************************2Problem:16473 User:hansbug4 language:pascal5 result:accepted6Time: -Ms7Memory:232KB8****************************************************************/9  Ten var One I,j,k,l,m,n,ans:longint; AA,c,e,f:Array[0.. -,0.. -] ofLongint; -B:Array[0.. -] ofLongint; - begin the readln (n,m); -       fori:=1  toN Do -          begin -                forj:=1  toM Doread (a[i,j]); + Readln; -          End; +Fillchar (b,sizeof (b),0); ans:=Maxlongint; A       whileb[0]=0  Do at            begin -                  fori:=1  toM Doc[0, i]:=B[i]; -                  fori:=1  toN Do -                      forj:=1  toM Doc[i,j]:=A[i,j]; -Fillchar (E,sizeof (E),0); k:=0; -                  fori:=1  toN Do in                     begin -                           forj:=1  toM Do to                              ifc[i-1, j]=1  Then +                                 begin -e[i,j]:=1; Inc (k); thec[i,j]:=1-C[i,j]; *c[i,j-1]:=1-c[i,j-1]; $c[i,j+1]:=1-c[i,j+1];Panax Notoginsengc[i-1, j]:=1-c[i-1, j]; -c[i+1, j]:=1-c[i+1, j]; the                                 End; +                     End; Al:=0; the                  fori:=1  toM DoInc (L,c[n,i]); +                 ifL=0  Then -                    begin $                         ifK<ans Then $                            begin -ans:=K; -                                  fori:=1  toN Do the                                      forj:=1  toM Do -f[i,j]:=E[i,j];Wuyi                            End; the                    End; -i:=m; Wu                  whileb[i]=1  Do -                       begin Aboutb[i]:=0; $ Dec (i); -                       End; -b[i]:=1; -            End; A      ifAns=maxlongint Then +         begin theWriteln ('Impossible'); - Halt; $         End; the       fori:=1  toN Do the           forj:=1  toM Do the              ifJ<m ThenWrite (F[i,j],' ')ElseWriteln (F[i,j]); the Readln; - End.

1647: [Usaco2007 open]fliptile turn lattice game

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.