Poj1753 flip game

Source: Internet
Author: User

Question understanding: 4x4 = 16 grids, each of which has two colors: Black (B) or white (W). After giving the initial color, you can flip a grid at a time, that is, from black to white, or from white to black. Each time you flip a grid, the four grids (Up, down, left, and right) Around It also need to be flipped together. The input is a 4x4 matrix, indicating the initial color of the grid. The output is the minimum number of grids or impossible needed to flip all grids to the same color.

Thought Change:

1. the 4x4 matrix can correspond to a 4x4 two-dimensional array. The black value is 1, and the white value is 0. There are three scenarios for flip grids.

A. When the position of a matrix (), (), and () is flipped, two grids are affected, that is, three grids are flipped in total.

B. When the four grids around the matrix (), and () are flipped, the four grids are affected.

C. When the rest of the matrix is flipped, three grids are affected, that is, four grids are flipped in total.

Assume that the flipped times of each of the above three grids are X, Y, and Z. At first, the number of black grids is m, and the number of white grids is N. Therefore, 3x + 5y + 4Z and m are considered, N may have a certain relationship.

2. Recognize the mistake in the above thinking: the location information of the grid cannot be attached, and this is decisive.

New discoveries:

A. There is no difference between the results of an odd number of flipped ings in a grid and the results of an even number of flipped ings in a grid.

B. If there is a solution, the result must be less than or equal to 16.

C. The flipped order has no effect on the result.

3. Introducing the Backtracking Method: defines the solution space. Each grid can perform two operations: Flip or not flip, so the maximum number of searches is 65536. In this case, the space is set to S, and s has 65536 elements. Each element is composed of 16 0 or 1, which can be considered as S = {0... 0 },......, {1... 1}, to do is to find the elements that meet the conditions and find the minimum. In this case, the space is the number of subsets, so referTemplate code for searching the subset tree by backtracking:

void backtrack(int t) {    if (t > n)        output(x);    for (int i = 0; i <= 1; i++) {        x[t] = i;        if (constraint(t) && bound(t))            backtrack(t + 1);    }}

We can see that the array X [N] is the space for decoding. Each position of the array can be 0 or 1, the constraint (t) is the constraint function, and the bound (t) is the restriction function, use it to pruning the subset tree

Based on the changes required in this question:

The array value of A. X [N] is 1, which indicates flip. The value 0 indicates no flip.

B. if you flip to the same color, the search is successful, not only on the leaf node.

C. to indicate that the search is successful, the Backtracking function requires another parameter.

D. You need to record the minimum number of flipped overs. Therefore, you need to calculate the number of each node in the solution space, that is, the X [N] array.

E. When the flipped node returns back, it is flipped once again, that is, the original value.

4. Accelerate your algorithm:

A. 4x4 matrix is changed to numbers and read in sequence. The weights at the I-th position are the 15-I power of 2.

B. Perform the bitwise operation on the reverse side. If the bitwise operation is used, the bitwise operation is the same as 0 or the bitwise operation is the same. If the bitwise operation is the same as the bitwise operation, the bitwise operation is the opposite.

C. Change the space time, calculate in advance the numbers required for each position flip, and save them with arrays.

The Code is as follows:

#include<stdio.h>void backtrack(int sum, int t);int find=0;int min=16;int mypow[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};int flip[16]={51200,58368,29184,12544,35968,20032,10016,4880,2248,1252,626,305,140,77,39,19};int x[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};int main(){    int i=0;    char a;    int sum = 0;    for(i=0; i<=15; ) {        scanf("%c", &a);        if(a==‘b‘) {            sum += mypow[15-i];            i++;                }        else if(a == ‘w‘)             i++;    }    backtrack(sum, 0);    if(find == 0)        printf("Impossible\n");    else        printf("%d\n", min);    return 0;}void backtrack(int sum, int t){    int i;    int result=x[0];    for(i=1; i<=15; i++) {        result+=x[i];    }    if(sum == 65535 || sum == 0)    {        find=1;        if(result < min)            min = result;        }        if(t == 16)        return;    for(i=0; i<=1; i++) {        x[t] = i;        if(i==0)            backtrack(sum, t+1);        else            backtrack(sum^flip[t], t+1);    }        }

5. Use iterations instead of recursion. ConsiderIterative template of subset tree backtracking:

X [N] = {-1}; // initialization T = 0; while (T> = 0) {While (X [T] <= 1) {x [T] ++; If (X [T] <= 1) {if X is the final legal solution record or the output else if X is a partial decomposition T = t + 1;} X [T] =-1; or other recovery operations t --; // backtracking}

Therefore, after the above recursion is converted to iteration, the specific code is as follows:

#include<stdio.h>using namespace std;bool find=false;int min=16;int mypow[16]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768};int flip[16]={51200,58368,29184,12544,35968,20032,10016,4880,2248,1252,626,305,140,77,39,19};int x[16]={-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};int main(){    int i=0, j=0;    char a;    int sum = 0;    int result, t;    for(i=0; i<=15; ) {    scanf("%c", &a);    if(a==‘b‘) {        sum += mypow[15-i];        i++;    }    else if(a == ‘w‘)         i++;    }    while(t>=0) {        while(x[t]<=1) {        x[t]++;        if(x[t] == 1) {            sum = sum^flip[t];        }        if(x[t]<=1) {            if(t==15) {            for(j=1, result=x[0]; j<=15; j++)                result += x[j];            if(sum == 65535 || sum == 0) {                find = true;                if(result < min)                min = result;            }            } else {            t++;            }        }        }        x[t] = -1;        sum = sum^flip[t];        t--;    }    if(find)      printf("%d\n", min);    else      printf("Impossible\n");    return 0;}

 

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.