B - 2048

2048 is a single-player puzzle game created by Gabriele Cirulli1. It is played on a 4×4 grid that contains integers ≥2

that are powers of 2. The player can use a keyboard arrow key (left/up/right/down) to move all the tiles simultaneously. Tiles slide as far as possible in the chosen direction until they are stopped by either another tile or the edge of the grid. If two tiles of the same number collide while moving, they will merge into a tile with the total value of the two tiles that collided. The resulting tile cannot merge with another tile again in the same move. Please observe this merging behavior carefully in all Sample Inputs and Outputs.
Input

The input is always a valid game state of a 2048 puzzle. The first four lines of input, that each contains four integers, describe the 16 integers in the 4×4
grid of 2048 puzzle. The j-th integer in the i-th line denotes the content of the cell located at the i-th row and the j

-th cell. For this problem, all integers in the input will be either {0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024}. Integer 0 means an empty cell.

The fifth line of input contains an integer 0, 1, 2, or 3 that denotes a left, up, right, or down move executed by the player, respectively.
Output

Output four lines with four integers each. Two integers in a line must be separated by a single space. This describes the new state of the 4×4

grid of 2048 puzzle. Again, integer 0 means an empty cell. Note that in this problem, you can ignore the part from the 2048 puzzle where it introduces a new random tile with a value of either 2 or 4 in an empty spot of the board at the start of a new turn.
Sample Input 1
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
0

Sample Output 1
4 0 0 0
4 16 8 2
2 64 32 4
2048 64 0 0

Sample Input 2
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
1

Sample Output 2
2 16 8 4
4 64 32 4
2 1024 64 0
1024 0 0 0

Sample Input 3
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
2

Sample Output 3
0 0 0 4
4 16 8 2
2 64 32 4
0 0 2048 64

Sample Input 4
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
3

Sample Output 4
2 0 0 0
4 16 8 0
2 64 32 4
1024 1024 64 4
思路:以向左移动为例,我们目标是把数都移到左边,0移到右边,所以从第二列开始,若它的前一个是0,就交换两个数,然后再第三列和第二列比,交换完后的第二列还是要和第一列再比,因为可能是0 0 2 0,的情况,就是一二两列交换完第一列还是0,以此类推。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[4][4];
void left_remove(){
    int i,j,k;
     for(int i=0;i<4;i++){
        for(int j=1;j<4;j++){
            k=j;
            while(k-1>=0&&a[i][k-1]==0){
                swap(a[i][k],a[i][k-1]);
                k--;
            }
        }
    }
}
void right_remove(){
    int i,j,k;
     for(int i=0;i<4;i++){
        for(int j=2;j>=0;j--){
            k=j;
            while(k+1<=3&&a[i][k+1]==0){
                swap(a[i][k],a[i][k+1]);
                k++;
            }
        }
    }
}
void up_remove(){
    int i,j,k;
     for(int j=0;j<4;j++){
        for(int i=1;i<4;i++){
            k=i;
            while(k-1>=0&&a[k-1][j]==0){
                swap(a[k][j],a[k-1][j]);
                k--;
            }
        }
    }
}
void down_remove(){
    int i,j,k;
     for(int j=0;j<4;j++){
        for(int i=2;i>=0;i--){
            k=i;
            while(k+1<=3&&a[k+1][j]==0){
                swap(a[k][j],a[k+1][j]);
                k++;
            }
        }
    }
}
void left(){
    int i,j;
    for(int i=0;i<4;i++){
        for(int j=0;j<3;j++){
            if(a[i][j]==a[i][j+1]){
                a[i][j]+=a[i][j+1];
                a[i][j+1]=0;
                left_remove();
            }
        }
    }
}
void right(){
    int i,j;
     for(int i=0;i<4;i++){
        for(int j=3;j>0;j--){
             if(a[i][j]==a[i][j-1]){
                a[i][j]+=a[i][j-1];
                a[i][j-1]=0;
                right_remove();
            }
        }
    }
}
void up(){
    int i,j;
     for(int j=0;j<4;j++){
        for(int i=0;i<3;i++){
            if(a[i][j]==a[i+1][j]){
                a[i][j]+=a[i+1][j];
                a[i+1][j]=0;
                up_remove();
            }
        }
    }
}
void down(){
    int i,j;
     for(int j=0;j<4;j++){
        for(int i=3;i>0;i--){
            if(a[i][j]==a[i-1][j]){
                a[i][j]+=a[i-1][j];
                a[i-1][j]=0;
                down_remove();
            }
        }
    }
}

int main()
{
    int t;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
            scanf("%d",&a[i][j]);
    scanf("%d",&t);
    if(t==0){
        left_remove();
        left();
    }
    else if(t==1){
        up_remove();
        up();
    }
    else if(t==2){
        right_remove();
        right();
    }
    else{
        down_remove();
        down();
    }
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(j==3)
                printf("%d\n",a[i][j]);
            else
                printf("%d ",a[i][j]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值