NOIP2011TG Mayan Games (search)

Source: Internet
Author: User

Title Description

Mayan Puzzle is a recently popular game. The game interface is a 7-row, 5-column chessboard stacked with blocks that cannot be stacked, that is, the block must be placed on the bottom row or on top of other blocks. Game clearance refers to the elimination of all blocks within the specified number of steps, the rules for eliminating blocks are as follows:

1. Each move can and can only be dragged along the horizontal (that is, left or right) a block of one box: When you drag the block, if you drag the position (hereinafter referred to as the target location) also has a block, then the two squares will be swapped position (see the input and Output sample description in Figure 6 to Figure 7); Then the dragged block will be drawn from the original vertical column and dropped from the target position (until it is not dangling, see figure 1 and Figure 2 below);

2. At any one time, if there are three or more three blocks of the same color in a row or column, they will be eliminated immediately (see Figures 1 to 3).

Attention:

A) If there are more than one set of blocks to meet the elimination conditions, a few sets of blocks will be eliminated at the same time (for example, the following figure 4, three color 1 squares and three color 2 blocks will be eliminated at the same time, and finally left a color of 2 blocks).

b) When rows and columns meet the elimination criteria and the row and column share a block, all squares on rows and columns that meet the elimination criteria are eliminated at the same time (for example, as shown in Figure 5 below, 5 blocks will be eliminated at the same time).

3. After the block is removed, the block above the position will drop and the new block may be removed after the drop. Note: There will be no block removal during the drop.

Figure 1 to Figure 3 above shows the checkerboard changes after moving a block on the board. The bottom-left corner of the chessboard has the coordinates (0, 0), will be located in (3, 3) of the block to the left, the game interface from Figure 1 into the state shown in Figure 2, at this time in a vertical column has three blocks of color 4, to meet the elimination condition, eliminate 3 blocks of 4 color of the block, the upper color of 3 blocks dropped, forming The situation shown in Figure 3.

Input/output format

Input format:

Input file mayan.in, total 6 lines.

The first behavior is a positive integer n, which indicates the number of steps required for game clearance.

The next 5 lines describe 7*5 's game interface. A number of integers per line, separated by a space between each two integers, each line ends with a 0, from the bottom up represents the color number of each vertical block (no more than 10 colors, numbering from 1, and the same number representing the same color).

The input data guarantees that there are no blocks in the initial checkerboard that can be eliminated.

Output format:

The output file name is Mayan.out.

If there is a solution, output n rows, each line contains 3 integers x,y,g, represents a move, each two integers separated by a space, where (x, y) represents the coordinates of the block to be moved, G for the direction of movement, 1 for the right, and 1 for the left move. Note: When multi-group solution, according to X is the first health word, Y is the second key word, 1 takes precedence over-1, gives a set of the smallest solution of the dictionary order. The coordinates of the bottom left corner of the game interface are (0, 0).

If there is no solution, the output line contains an integer-1.

Input and Output Sample input example # #:
31 02 1 02 3 4 03 1 02 4 3 4 0
Sample # # of output:
2 1 13 1 13 0 1
Description

"Input and Output sample description"

In the order of Arrow direction, figure 6 to Figure 11, respectively

Sample input game situation as shown in the first picture above, the three steps to move is: (2, 1) in the box to move to the right, (3,1) at the square to move to the right, (3, 0) at the square to the right, and finally can be eliminated all the squares on the board.

"Data Range"

For 30% of data, the squares on the initial checkerboard are on the bottom line of the chessboard;

For 100% of data, 0 < n≤5.

The 3rd question of noip2011 raising group Day1

The reason that the teacher said that this is a search I am a little bit of a clue, well, after a long time or not. Finally in the teacher explained under finally AC ...

1#include <cstdio>2#include <cmath>3#include <cstring>4#include <cstdlib>5#include <queue>6#include <vector>7#include <stack>8#include <iostream>9#include"algorithm"Ten using namespacestd; OnetypedefLong LongLL; A Const intmax=Ten; - intN; - structnode{ the     intG[max][max]; -     intC; -     voidDown () { -         inti,j,k; +          for(i=1; i<=5; i++) -           for(k=1, j=1; j<=7; j + +) +{if(!g[i][j])Continue; A           if(k!=j) Swap (G[i][j],g[i][k]); atk++; -          } -     } -     voidUpDate () { -         inti,j,k; -         BOOLtemp[max][max],f; in          Do - {down (); tof=false; +memset (temp,false,sizeof(temp)); -           for(i=1; i<=5; i++) the            for(j=1; j<=7-2; j + +) *            if(g[i][j]!=0&& g[i][j]==g[i][j+1] && g[i][j+1]==g[i][j+2]) ${temp[i][j]=temp[i][j+1]=temp[i][j+2]=true;Panax Notoginsengf=true; -            } the           for(j=1; j<=7; j + +) +            for(i=1; i<=5-2; i++) A            if(g[i][j]!=0&& g[i][j]==g[i+1][J] && g[i+1][j]==g[i+2][j]) the{temp[i][j]=temp[i+1][j]=temp[i+2][j]=true; +f=true; -            } $           for(i=1; i<=5; i++) $            for(j=1; j<=7; j + +) -            if(Temp[i][j]) -{g[i][j]=0; thec--; -            }Wuyi} while(f); the     } - }q; Wu structans{ -     intx, y, z About }ans[max]; $ intLANs; - void  out(){ -     inti,j; -      for(i=1; i<=lans;i++) Aprintf"%d%d%d\n", ans[i].x-1, ans[i].y-1, ans[i].z); + } the BOOLDFS (Node &now,intN) { -     if(n==0&& now.c==0) ${ out(); the      inti,j; theExit0); the     } the     if(n<=0) -      return false; in     if(now.c==0) the      return false; the Node ZT; Aboutzt=Now ; the     inti,j; the      for(i=1; i<=5; i++) the       for(j=1; j<=7; j + +) +{if(zt.g[i][j]!=0) -{Swap (zt.g[i][j],zt.g[i+1][j]); thelans++;Bayians[lans].x=i; theans[lans].y=J; theans[lans].z=1; - ZT. UpDate (); -DFS (zt,n-1); thezt=Now ; thelans--; the       } the       if(zt.g[i][j]!=0&& zt.g[i-1][j]==0) -{if(i==1)Continue; theSwap (zt.g[i][j],zt.g[i-1][j]); thelans++; theans[lans].x=i;94ans[lans].y=J; theans[lans].z=-1; the ZT. UpDate (); theDFS (zt,n-1);98zt=Now ; Aboutlans--; -       }101      }102 }103 intMain () {104Freopen ("mayan.in","R", stdin); theFreopen ("Mayan.out","W", stdout);106     inti,j,k;107scanf"%d",&n);108Q.c=0;109memset (Q.G,0,sizeof(Q.G)); the      for(i=1; i<=5; i++)111{k=0; the       while(1)113{scanf ("%d",&j); the       if(j==0) the         Break; theq.g[i][++k]=J;117Q.c++;118      }119     } -     if(!DFS (q,n))121Puts"-1");122     return 0;123}

NOIP2011TG Mayan Games (search)

Related Article

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.