zoukankan      html  css  js  c++  java
  • I

     

    I - 2048

    Time Limit: 2000/1000 MS (Java/Others)      Memory Limit: 128000/64000 KB (Java/Others)
    Submit Status

    Problem Description

          《2048》是一款比较流行的数字游戏,最早于2014年3月20日发行。原版2048首先在GitHub上发布,原作者是Gabriele Cirulli,后被移植到各个平台。这款游戏是基于《1024》和《小3传奇》的玩法开发而成的新型数字游戏。

           这是20岁的Gabriele Cirulli开发的一款数字游戏。初衷就是觉得好玩,在将其开源版本放到Github上后,意外走红。这款游戏的玩法很简单,每次可以选择上下左右滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加。不断的叠加最终拼凑出2048这个数字就算成功。

        现在要你编程模拟这个游戏的移动过程,每次输入一个字母c表示移动方向,然后再输入一个4*4的矩阵,表示一个游戏局面,然后你要输出一个4*4的矩阵表示移动后的局面。

        例如

        向下移动之后变成:

    Input

    多组样例输入。

    每组样例第一行输入一个字母c,c∈{U,D,L,R}

    接下来4行输入一个4*4的矩阵,矩阵中每个数都为0或2k,1<=k<=16,其中0表示空格

    Output

    第一行输出Case x:

    x表示第几个样例

    接下来输出四行,一个4*4的矩阵,表示移动后的局面(不考虑移动后在空位出现一个新数字)

    Sample Input

    D
    0 4 2 8
    2 0 2 4
    0 0 4 16
    2 8 4 64
    R
    2 2 2 2
    2 0 0 2
    2 0 0 2
    0 2 2 4
    U
    2 2 2 2
    2 0 0 2
    2 0 0 2
    0 2 2 4
    

    Sample Output

    Case 1:
    0 0 0 8
    0 0 0 4
    0 4 4 16
    4 8 8 64
    Case 2:
    0 0 4 4
    0 0 0 4
    0 0 0 4
    0 0 4 4
    Case 3:
    4 4 4 4
    2 0 0 2
    0 0 0 4
    0 0 0 0
    

    Hint

    碰撞时最靠移动的方向的数先相加,如第三个样例中最后一列,注意第二个样例中的2 2 2 2,移动之后会变成0 0 4 4,即相加生成的数不会再相加
     
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int N=1e5+10;
     5 const int INF=0x3f3f3f3f;
     6 int cas=1,T;
     7 int a[4][4],d;
     8 void output(int a[4][4])
     9 {
    10     for(int i=0;i<4;i++)
    11         for(int j=0;j<4;j++) printf("%d%c",a[i][j],j==3?'
    ':' ');
    12 }
    13 void rotate(int a[4][4],int d)
    14 {
    15     int t[4][4];
    16 //    output(a);printf("
    ");
    17     for(int i=0;i<d;i++)
    18     {
    19         memcpy(t,a,sizeof(t));
    20         for(int j=0;j<4;j++)
    21             for(int k=0;k<4;k++)
    22                 a[j][k]=t[k][4-j-1];
    23     }
    24 //    output(a);printf("
    ");
    25 }
    26 void move(int a[4][4])
    27 {
    28     for(int i=0;i<4;i++)
    29     {
    30         for(int j=3;j>0;j--)
    31         {
    32             if(a[i][j])
    33             {
    34                 int k=j-1;
    35                 while(k>0)
    36                 {
    37                     if(a[i][k]==0) k--;
    38                     else break;
    39                 }
    40                 if(a[i][k]==a[i][j])
    41                 {
    42                     a[i][k]=0;
    43                     a[i][j]<<=1;
    44                 }
    45             }
    46         }
    47         for(int j=3;j>0;j--)
    48         {
    49             if(!a[i][j]) 
    50             {
    51                 int k=j-1;
    52                 while(k>0)
    53                 {
    54                     if(a[i][k]==0) k--;
    55                     else break;
    56                 }
    57                 swap(a[i][j],a[i][k]);
    58             }
    59         }
    60     }
    61 }
    62 char dir[2];
    63 int main()
    64 {
    65 //    freopen("1.in","w",stdout);
    66 //    freopen("1.in","r",stdin);
    67 //    freopen("1.out","w",stdout);
    68 //    scanf("%d",&T);
    69     while(scanf("%s",dir)==1)
    70     {
    71         switch(dir[0])
    72         {
    73             case 'R':d=0;break;
    74             case 'U':d=1;break;
    75             case 'L':d=2;break;
    76             case 'D':d=3;break;
    77         }
    78         for(int i=0;i<4;i++)
    79             for(int j=0;j<4;j++) scanf("%d",&a[i][j]);
    80         rotate(a,4-d);
    81         move(a);
    82         rotate(a,d);
    83         printf("Case %d:
    ",cas++);
    84         output(a);
    85     }
    86 //    printf("time=%.3lf
    ",(double)clock()/CLOCKS_PER_SEC);
    87     return 0;
    88 }
    89 /*
    90 →0
    91 ↑1
    92 ←2
    93 ↓3
    94 */
    solve.cpp
    题解:

    直接模拟每个方向的移动,注意细节
    标程只写了一个方向的移动,然后其它方向的通过旋转实现

     
  • 相关阅读:
    angular反向代理配置
    实现对Asp.NetMvc及Asp.NetCore的权限控制
    C# 语言特性发展史
    在angular 6中使用 less
    使用WeihanLi.Redis操作Redis
    [svc]jq神器使用
    [sh]shell脚本栗子
    [k8s]nginx-ingress配置4/7层测试
    [svc]nginx-module-vts第三方模块安装配置
    [k8s]helm原理&私有库搭建&monocularui和kubeapp探究
  • 原文地址:https://www.cnblogs.com/cdyboke/p/7010570.html
Copyright © 2011-2022 走看看