B

来源:互联网 发布:tcp套接字编程java 编辑:程序博客网 时间:2024/05/04 07:36

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;}
原创粉丝点击