[] Mayan game NOIP2011

Title Description

Mayan puzzle is the latest of a popular game. It is a game interface \ (7 \) OK \ (× 5 \) column board, some stacked above box, the box can not be suspended stack, i.e., the bottom line must be placed in the box, or on top of the other block. Game clearance means the elimination of all the boxes in the prescribed number of steps to eliminate the box rules are as follows:

1, each step movement may be dragged and only a block in the transverse direction (i.e., left or right) a lattice: When the drag block, if arriving drag position (hereinafter referred to as the target position) is also a block , then these two blocks will swap position (see description of the input and output sample FIG \ (6 \) to FIG \ (7 \) ); if there is no position on the target block, then the block is dragged from the original extracting vertical column, and fall (until no vacant, see FIG. 1 and FIG. 2 below) from a target position;

2, at any one time, if there is on the same vertical column of a row or three or more than three consecutive color block, they will immediately be eliminated (see FIG. 1 to FIG. 3).

note:

a) If both block satisfies eliminate multiple sets of conditions, several groups of blocks will also be eliminated (e.g. below FIG \ (4 \) , three color \ (1 \) block and three color \ (2 \) the box will also be eliminated, the last remaining one color is \ (2 \) squares).

b) when there is elimination of both rows and columns satisfying the condition block and share a row, to meet all the conditions will be to eliminate the box while eliminating situations (e.g., as shown in the following rows and columns in FIG. 5, five blocks will also be eliminated ).

3, after the elimination of the box, the box on the elimination of the position of the drop, after drop may cause new box eliminated. Note: There will be a block in the process of eliminating the fall.

The above Figures 1 to 3 shows the change in the board after moving in a square on the board. After the coordinates of the lower left corner of the board to block (0, 0), located (3, 3) block moves to the left, the game interface state shown in FIG 2 from FIG 1, there is in this case a vertical column 4 is a block consecutive three colors meet deletion condition, eliminating color after three consecutive blocks of the block 4, the color of the upper box 3 is dropped to form the situation shown in FIG.

Input and output formats

Input formats:

A total of 6 lines.

Conduct a first positive integer \ (n-\) , represents the amount of steps required clearance.

The next \ (5 \) line described \ (7 × 5 \) game interface. Each line an integer number, between each two integers separated by a space, each line a \ (0 \) ends, from the bottom up for each vertical column represents a block number of colors (color of not more than \ (10 \) species, from \ (1 \) numbered sequentially starting like numerals indicate the same color).

Input data to ensure that there are no initial checkerboard squares can be eliminated.

Output formats:

If there is a solution, the output \ (n-\) rows, each row comprising \ (3 \) integers \ (X, Y, G \) , represents a movement, each separated by a space between two integers, wherein \ ((x, y) \ ) represents the coordinates of the box to be moved, \ (G \) represents the direction of movement, \ (1 \) represents the right, \ (- 1 \) represents the left. Note: When the plurality of solutions, according to \ (X \) is the first key word, \ (Y \) is the second key word, \ (1 \) precedence \ (- 1 \) , given a set lexicographically smallest solution. The coordinates of the lower left corner of the game interface is \ ((0,0) \) .

If there is no solution, output one line containing an integer \ (--1 \) .

Sample input and output

Input Sample # 1:

3
1 0
2 1 0
2 3 4 0
3 1 0
2 4 3 4 0

Output Sample # 1:

2 1 1
3 1 1
3 0 1

analysis

Data range: \ (n-<=. 5 \) , consider search violence + analog, the code amount is very rich. . . . .
Direct enumeration every step of the mobile solutions, that is, every step of each square were \ (the DFS \) , enumerate each state, until the game clearance, if not the final step in the clearance direct output \ (--1 \) .

Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define il inline
#define re register
#define mymax(a,b) a>b?a:b
#define mymin(a,b) a<b?a:b
#define tie0 cin.tie(0),cout.tie(0)
#define fastio ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;

template<typename T>inline void read(T &x){
    T f=1;x=0;char c;
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+(c^48);
    x*=f;
}

int n;
int g[10][10],ans[7][5],last[10][10][10];
bool vis[10][10];

void copy(int step){
    for (int i = 0; i < 5; ++i)
        for (int j = 0; j < 7; ++j)
            last[step][i][j]=g[i][j];
}

void update(){
    for (int i = 0; i < 5; ++i){
        int cnt=0;
        for (int j = 0; j < 7; ++j){
             if(!g[i][j]) cnt++;
             else{
                 if(!cnt) continue;
                g[i][j-cnt]=g[i][j],g[i][j]=0;
             }
        }
    }
}

bool remove(){
    bool flag=0;
    for (int i = 0; i < 5; ++i){
        for (int j = 0; j < 7; ++j){
            if (i-1>=0 && i+1<5 && g[i-1][j]==g[i][j] && g[i][j]==g[i+1][j] && g[i][j]){
                vis[i-1][j] = 1, vis[i][j] = 1, vis[i+1][j] = 1; flag = 1;
            }
            if(j-1>=0 && j+1<7 && g[i][j-1]==g[i][j] && g[i][j+1]==g[i][j] && g[i][j]){
                vis[i][j-1] = 1, vis[i][j+1] = 1, vis[i][j] = 1; flag = 1;
            }
        }
    }
    if(!flag) return 0;
    for(int i = 0; i < 5; ++i)
        for(int j = 0; j < 7; ++j)
            if(vis[i][j]){
                g[i][j] = 0,vis[i][j] = 0;
            }
    return 1;
} 

void move(int i,int j,int opt){
    int tmp = g[i][j];
    g[i][j] = g[i+opt][j];
    g[i+opt][j] = tmp;
    update();
    while(remove()) update();
}

bool check(){
    for(int i = 0; i < 5; ++i)
        if(g[i][0]) return 0;
    return 1;
}

void dfs(int step){
    if (check()){
        for(int i=1;i<=n;++i){
            for(int j=1;j<=3;++j)
                printf("%d ",ans[i][j]);
            printf("\n");
        }
        exit(0);
    }
    if(step==n+1) return;
    copy(step);
    for(int i = 0; i < 5; ++i)
        for(int j = 0; j < 7; ++j){
            if(g[i][j]){
                if(i+1<5 && g[i+1][j] != g[i][j]){
                    move(i,j,1);
                    ans[step][1] = i,ans[step][2] = j,ans[step][3] = 1;
                    dfs(step+1);
                    for(int p = 0; p < 5; ++p)
                        for(int q = 0; q < 7; ++q)
                            g[p][q] = last[step][p][q];
                    ans[step][1] = ans[step][2] = ans[step][3] = -1;
                }
            }
            if(i-1>=0 && g[i-1][j]==0){
                move(i,j,-1);
                ans[step][1] = i,ans[step][2] = j,ans[step][3] = -1;
                dfs(step+1);
                for(int p = 0; p < 5; ++p)
                    for(int q = 0; q < 7; ++q)
                        g[p][q] = last[step][p][q];
                ans[step][1] = ans[step][2] = ans[step][3] = -1;
            }
        }
}

int main(){
    read(n);
    for (int i = 0; i < 5; ++i){
        for(int j = 0; j < 8; ++j){
            int k;
            read(k);
            if(k==0) break;
            g[i][j] = k;
        }
    }
    memset(ans,-1,sizeof(ans));
    dfs(1);
    printf("-1");
    return 0;
}

Guess you like

Origin www.cnblogs.com/hlw1/p/11235655.html