Kattis - 2048

2048 is a single-player puzzle game created by Gabriele Cirulli1. It is played on a 4×44×4 grid that contains integers 2≥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./problems/2048/file/statement/en/img-0001.jpg

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×44×4 grid of 2048 puzzle. The jj-th integer in the ii-th line denotes the content of the cell located at the ii-th row and the jj-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×44×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 1Sample Output 1
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
0
4 0 0 0
4 16 8 2
2 64 32 4
2048 64 0 0
Sample Input 2Sample Output 2
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
1
2 16 8 4
4 64 32 4
2 1024 64 0
1024 0 0 0
Sample Input 3Sample Output 3
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
2
0 0 0 4
4 16 8 2
2 64 32 4
0 0 2048 64
Sample Input 4Sample Output 4
2 0 0 2
4 16 8 2
2 64 32 4
1024 1024 64 0
3
2 0 0 0
4 16 8 0
2 64 32 4
1024 1024 64 4
Sample Input 5Sample Output 5
2 2 4 8
4 0 4 4
16 16 16 16
32 16 16 32
0
4 4 8 0
8 4 0 0
32 32 0 0
32 32 32 0

Sample Input 6Sample Output 6
2 2 4 8
4 0 4 4
16 16 16 16
32 16 16 32
2
0 4 4 8
0 0 4 8
0 0 32 32
0 32 32 32
一个大模拟题……就是模拟2048这个游戏,应该没有人没玩过吧(我之前还沉迷过一阵子来着的,但最高也只玩出过4096orz)。样例都过了基本就能ac了。

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <stack>
#include <set>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int mp[4][4], t;
void input() {
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            cin >> mp[i][j];
    cin >> t;
}
void output() {
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++) {
            cout << mp[i][j];
            if (j == 3) cout << endl;
            else cout << " ";
        }
}
void exchange0() {
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            for (int k = j + 1; k < 4; k++) {
                if (mp[i][k] && mp[i][j] == mp[i][k]) {
                    mp[i][j] *= 2;
                    mp[i][k] = 0;
                    break;
                }
                else if (mp[i][k] && mp[i][j] != mp[i][k]) break;
            }
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            if (!mp[i][j]) {
                int k = j + 1;
                while (!mp[i][k] && k < 4) k++;
                if (k == 4) continue;
                else {
                    int tmp = mp[i][j];
                    mp[i][j] = mp[i][k];
                    mp[i][k] = tmp;
                }
            }
}
void exchange1() {
    for (int j = 0; j < 4; j++)
        for (int i = 3; i >= 0; i--)
            for (int k = i - 1; k >= 0; k--) {
                if (mp[k][j] && mp[i][j] == mp[k][j]) {
                    mp[i][j] *= 2;
                    mp[k][j] = 0;
                    break;
                }
                else if (mp[k][j] && mp[i][j] != mp[k][j]) break;
            }
    for (int j = 0; j < 4; j++)
        for (int i = 0; i < 4; i++)
            if (!mp[i][j]) {
                int k = i + 1;
                while (!mp[k][j] && k < 4) k++;
                if (k == 4) continue;
                else {
                    int tmp = mp[i][j];
                    mp[i][j] = mp[k][j];
                    mp[k][j] = tmp;
                }
            }
    
}
void exchange2() {
    for (int i = 0; i < 4; i++)
        for (int j = 3; j >= 0; j--)
            for (int k = j - 1; k >= 0; k--) {
                if (mp[i][k] && mp[i][j] == mp[i][k]) {
                    mp[i][j] *= 2;
                    mp[i][k] = 0;
                    break;
                }
                else if (mp[i][k] && mp[i][j] != mp[i][k]) break;
            }
    for (int i = 0; i < 4; i++)
        for (int j = 3; j >= 0; j--)
            if (!mp[i][j]) {
                int k = j - 1;
                while (!mp[i][k] && k >= 0) k--;
                if (k == -1) continue;
                else {
                    int tmp = mp[i][j];
                    mp[i][j] = mp[i][k];
                    mp[i][k] = tmp;
                }
            }
}
void exchange3() {
    for (int j = 0; j < 4; j++)
        for (int i = 0; i < 4; i++)
            for (int k = i + 1; k < 4; k++) {
                if (mp[k][j] && mp[i][j] == mp[k][j]) {
                    mp[i][j] *= 2;
                    mp[k][j] = 0;
                    break;
                }
                else if (mp[k][j] && mp[i][j] != mp[k][j]) break;
            }
    for (int j = 0; j < 4; j++)
        for (int i = 3; i >= 0; i--)
            if (!mp[i][j]) {
                int k = i - 1;
                while (!mp[k][j] && k >= 0) k--;
                if (k == -1) continue;
                else {
                    int tmp = mp[i][j];
                    mp[i][j] = mp[k][j];
                    mp[k][j] = tmp;
                }
            }
}
int main() {
    input();
    if (t == 0) exchange0();
    else if (t == 1) exchange1();
    else if (t == 2) exchange2();
    else exchange3();
    output();
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值