8.12 俄罗斯方块 1863

本文介绍了一款基于经典游戏“俄罗斯方块”的算法模拟问题,旨在通过编程计算不同形状方块落地后的形态总数。文中提供了具体的输入输出示例,并详细阐述了针对7种方块的不同落地情况的处理方法及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

相信大家都玩过“俄罗斯方块”游戏吧,“俄罗斯方块”是一个有趣的电脑小游戏,现有一个有C列、行不受限定游戏平台,每一次下落的方块是下列的7个图形的一种:
这里写图片描述
在下落的过程中,游戏者可以作90、 180或270 度旋转,还可以左右移动,对于每一次方块落地,我们要求方块的每一部分都必须与地面(最底面或己落下的方块上表面)接触,例如,有一个宽度为6列的平台,每一列的初始高度(已经占用的方格数)分别为2, 1, 1, 1, 0 和 1。编号为5的方块下落,有且仅有5种不同的落地方法:
这里写图片描述
现给出每一列的初始高度和下落方块的形状,请你编写一个程序,求出落地的方法总数,也就是落地后,地表面形成的不同的形状总数。

第一行为二个整数C和P,1 ≤ C ≤ 100, 1 ≤ P ≤ 7,表示列数和下落方块的编号
第二行共有用一个空隔隔开的C个整数,每一个数字在 0 到 100,之间(包含0和100),表示每一列的初始高度

Input1                 Output1
 6 5                      5
 2 1 1 1 0 1

Input2                 Output2
 5 1                      7
 0 0 0 0 0

Input3                 Output3
 9 4                      1
 4 3 5 4 6 5 7 6 6

题解

水水的模拟,分别判断每种方块的符合条件的下落方式,然后O(n)的枚举

时间复杂度O(n)

代码

var
  n,p,i,j,k,ans:longint;
  a:array[1..100]of longint;

procedure one;
var
  i:longint;
begin
  ans:=ans+n;
  for i:=4 to n do
    if (a[i]=a[i-1])and(a[i]=a[i-2])and(a[i]=a[i-3]) then inc(ans);
end;

procedure two;
var
  i:longint;
begin
  for i:=2 to n do
    if a[i]=a[i-1] then inc(ans);
end;

procedure three;
var
  i:longint;
begin
  for i:=2 to n do
    if a[i]+1=a[i-1] then inc(ans);
  for i:=3 to n do
    if (a[i-1]=a[i-2])and(a[i]=a[i-1]+1) then inc(ans);
end;

procedure four;
var
  i:longint;
begin
  for i:=2 to n do
    if a[i]=a[i-1]+1 then inc(ans);
  for i:=3 to n do
    if (a[i]=a[i-1])and(a[i]+1=a[i-2]) then inc(ans);
end;

procedure five;
var
  i:longint;
begin
  for i:=3 to n do
    if (a[i]=a[i-1])and(a[i]=a[i-2]) then inc(ans);
  for i:=3 to n do
    if (a[i]=a[i-2])and(a[i]=a[i-1]+1) then inc(ans);
  for i:=2 to n do
    if a[i]=a[i-1]+1 then inc(ans);
  for i:=2 to n do
    if a[i]+1=a[i-1] then inc(ans);
end;

procedure six;
var
  i:longint;
begin
  for i:=3 to n do
    if (a[i]=a[i-1])and(a[i]=a[i-2]) then inc(ans);
  for i:=2 to n do
    if a[i]=a[i-1] then inc(ans);
  for i:=2 to n do
    if a[i]+2=a[i-1] then inc(ans);
  for i:=3 to n do
    if (a[i]=a[i-1])and(a[i]=a[i-2]+1) then inc(ans);
end;

procedure seven;
var
  i:longint;
begin
  for i:=3 to n do
    if (a[i]=a[i-1])and(a[i]=a[i-2]) then inc(ans);
  for i:=2 to n do
    if a[i]=a[i-1] then inc(ans);
  for i:=2 to n do
    if a[i]=a[i-1]+2 then inc(ans);
  for i:=3 to n do
    if (a[i-1]=a[i-2])and(a[i]+1=a[i-1]) then inc(ans);
end;

begin
  readln(n,p);
  for i:=1 to n do
    read(a[i]);
  if p=1 then one;
  if p=2 then two;
  if p=3 then three;
  if p=4 then four;
  if p=5 then five;
  if p=6 then six;
  if p=7 then seven;
  writeln(ans);
end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值