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();
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章