CSP201604-2 Tetris (Python)

Article directory

topic

Question No.: 201604-2
Question name: Tetris
time limit: 1.0s
Memory limit: 256.0MB

Problem Description
  Tetris is a casual game invented by Russian Alexei Pajitnov.
  The game is played on a grid with 15 rows and 10 columns. Each grid on the grid may or may not have been placed. Every round, there will be a new plate consisting of 4 small squares falling from the top of the grid. Players can operate the plate to move left and right and put it in a suitable position. When the upper edge of the square above coincides or reaches the lower boundary, the plate will no longer move. If a line of the grid diagram is completely filled with squares at this time, the line will be eliminated and scored.
  In this problem, you need to write a program to simulate the falling of the board. You don't need to deal with the player's operation, and you don't need to deal with the row and score.
  Specifically, given an initial grid map, the shape of a plate and the initial position where it falls, you want to give the final grid map.
  
Input format
  The first 15 lines of input contain the initial grid map, each line contains 10 numbers, and adjacent numbers are separated by spaces. If a number is 0, it means that there is no square in the corresponding square, and if the number is 1, it means that there is a square at the beginning. The input guarantees that the numbers in the first 4 lines are all 0.
  Lines 16 to 19 of the input contain the shape of the newly added plate, and each line contains 4 numbers to form a plate pattern. Similarly, 0 means no block, and 1 means there is a block. The input guarantees that the pattern of the board contains exactly 4 squares, and the 4 squares are connected together (to be precise, the 4 squares are four-connected, that is, the given board is a standard board of Tetris).
  Line 20 contains an integer between 1 and 7, indicating in which column of the grid chart the leftmost side of the tile pattern starts. Note that the plate pattern here refers to the plate pattern entered in lines 16 to 19. If the leftmost column of the plate pattern is all 0, its left side is inconsistent with the left side of the actual plate (see example)
  
Output format
  Output 15 lines, each line contains 10 numbers, and adjacent numbers are separated by a space, indicating the grid map after the plate falls. Note that you don't need to handle the final row.
  
Sample input
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0
  0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 1 0 0
  0 0 0 0 0 0 1 0 0 0 0 0 0 0 0
  0 1 0
  0 0 1 1 1 0 0 0 1 1 1 1
  0 0 0 0 1 0 0 0 0 0
  0 0 0 0
  0 1 1 1
  0 0 0 1
  0 0 0 0
  3
  
sample output
  0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 0 0 0
  0 0 0 0 0 0 0 1 0 0
  0 0 0 0 0 0 1 0 0 0
  0 0 0 0 0 0 1 0 0 0
  1 1 1 1 1 1 1 1 1 1
  0 0 0 0 1 1 0 0 0 0


code

# 输入初始方格图(先添加4行空行和底端边界)
numbers = []
for i in range(4):
    numbers.append([0 for i in range(10)])
for i in range(15):
    temp = list(map(int,input().split()))
    numbers.append(temp)
numbers.append([1 for i in range(10)])

# 输入新板块形状
new = []
for i in range(4):
    temp = list(map(int,input().split()))
    new.append(temp)

# 输入下落位置
n = int(input())

# 确定新板块方格位置并加入顶端四行空行中
check = []
check_0 = []
for i in range(4):
    for j in range(4):
        if new[i][j] == 1:
            check.append([i,j+n-1])
            # 寻找新板块中下方为空的方格用于判断下移距离
            if i == 3 or new[i+1][j] == 0:
                check_0.append([i,j+n-1])

# 判断合法的下移行数
down = 0
flag = True # 循环标记
while flag:
    for x_y in check_0:
        x,y = x_y
        if numbers[x+1][y] != 0:
            flag = False
    down += 1
    for i in range(len(check_0)):
        check_0[i][0] += 1

# 改变下落最终位置的数字
for i in range(4):
    x,y = check[i]
    numbers[x + down-1][y] = 1

# 输出
for i in range(4,19):
    for j in range(10):
        print(numbers[i][j],end=" ")
    print()

Guess you like

Origin blog.csdn.net/qq_45899597/article/details/113815114
Recommended