CCF 201604-2 俄罗斯方块


一、题目简介

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、代码示例(C++)

1.引入库

代码如下(示例):

#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int board[15+1][10];
int block[4][4];
//坐标点 
struct square{
   int row;
   int col;
};

存放4个小方块坐标。

int main()
{
   int row, col;
   square coords[4];

   // 输入数据
   for(int i=0; i<15; i++)
       for(int j=0; j<10; j++)
           cin >> board[i][j];
   for(int i=0; i<4; i++)
       for(int j=0; j<4; j++)
           cin >> block[i][j];
   cin >> col;
   // 底边全放1
   for(int j=0; j<10; j++)
       board[15][j] = 1;

防止小方块落到15行时不停止。

   // 提取小方块坐标
   int k = 0;
   for(int i=0; i<4; i++)
       for(int j=0; j<4; j++)
           if(block[i][j] == 1) {
               coords[k].row = i;
               coords[k].col = j;
               k++;
           }
           
   // 模拟小方块落下过程
   row = 0;
   col--;
   bool checkflag;
   for(;;) {
       checkflag = false;

       for(int i=0; i<4; i++)
           if(board[row + coords[i].row][col + coords[i].col] == 1) {
               checkflag = true;
               break;
           }


       if(checkflag)
           break;

当board的这四行相对block中1的坐标位置已经为1(即落下的小方块已与原本的方块相交),退出循环,停止下落。

       row++;
   }
   row--;

退回一行(因为此位置下落小方块已经与原本的小方块相交)。

   // 合并小方块到方格
   for(int i=0; i<4; i++)
       board[row + coords[i].row][col + coords[i].col] = 1;

   // 输出结果
   for(int i=0; i<15; i++) {
       for(int j=0; j<10; j++) {
           if(j != 0)
               cout << " ";
           cout << board[i][j];
       }
       cout << endl;
   }

   return 0;
}

总结

小方块下落的实现可以建立结构体,存储小方块相对坐标。再利用row,col存储相对坐标系的原点位置,通过循环使原点逐行下降,从而实现全部小方块的逐行下降。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值