OI 2048

· · 休闲·娱乐

#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h> 
#define endl '\n'
using namespace std;

const int N = 150;
int board[N][N], score, siz;

void ss(int col, int row)
{
    char cmd[50];
    sprintf(cmd, "mode con cols=%d lines=%d", col, row);
    system(cmd);
}

void setcolor(string col)
{
    if(col == "black")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0);
    else if(col == "blue")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 1);
    else if(col == "green")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 2);
    else if(col == "cyan")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3);
    else if(col == "red")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 4);
    else if(col == "purple")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 5);
    else if(col == "yellow")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6);
    else if(col == "white")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
    else if(col == "grey")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
    else if(col == "light_blue")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 9);
    else if(col == "light_green")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
    else if(col == "light_cyan")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
    else if(col == "light_red")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
    else if(col == "light_purple")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 13);
    else if(col == "light_yellow")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
    else if(col == "light_white")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
    else if(col == "cholly")
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 96);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

void output_single(int x)
{
    if(x == 0)
        printf("        ");
    else if(x == 1)
        setcolor("light_blue"), printf("   栈   "), setcolor("white");
    else if(x == 2)
        setcolor("light_green"), printf("  队列  "), setcolor("white");
    else if(x == 3)
        setcolor("light_cyan"), printf("   堆   "), setcolor("white");
    else if(x == 4)
        setcolor("light_yellow"), printf(" 并查集 "), setcolor("white");
    else if(x == 5)
        setcolor("light_white"), printf("树状数组"), setcolor("white");
    else if(x == 6)
        setcolor("light_red"), printf(" 线段树 "), setcolor("white");
    else if(x == 7)
        setcolor("light_purple"), printf("树链剖分"), setcolor("white");
    else if(x == 8)
        setcolor("cyan"), printf(" 平衡树 "), setcolor("white");
    else if(x == 9)
        setcolor("yellow"), printf(" 树套树 "), setcolor("white");
    else if(x == 10)
        setcolor("red"), printf("K-D Tree"), setcolor("white");
    else if(x == 11)
        setcolor("purple"), printf(" 动态树 "), setcolor("white");
    else if(x == 12)
        setcolor("grey"), printf("   ETT  "), setcolor("white");
    else if(x == 13)
        setcolor("blue"), printf("  SATT  "), setcolor("white");
    else
        setcolor("cholly"), printf("珂朵莉树"), setcolor("white"); 
}

void output()
{
    system("cls"); 
    printf("合成");
    output_single(14);
    printf("\n使用↑↓←→移动\n");
    printf("你目前的得分是:%d\n", score); 
    printf("合成顺序:");
    for(int i = 1; i <= 14; i++)
    {
        output_single(i);
        if(i != 14) printf("->");   
    }
    printf("\n");
    for(int i = 1; i <= siz; i ++)
    {
        if(i == 1) printf("╭");
        else printf("|");
        if(i != 1) for(int i = 1; i <= 9 * siz - 1; i ++) printf("-");
        else for(int i = 1; i <= 9 * siz - 2; i ++) printf("-");
        if(i == 1) printf("╮");
        else printf("|");
        printf("\n|");
        for(int j = 1; j <= siz; j ++)
        {
            output_single(board[i][j]);
            printf("|");
        }
        printf("\n");
    }
    printf("╰");
    for(int i = 1; i <= 9 * siz - 1; i ++) printf("-");
    printf("╯");
}

void get_rd()
{
    int x = rand() % siz + 1, y = rand() % siz + 1, cnt = 0;
    while(board[x][y] != 0 && cnt <= 1000)
    {
        x = rand() % siz + 1;
        y = rand() % siz + 1;
        cnt ++;
    }
    board[x][y] = rand() % 2 + 1;
}

int r[N], tot, nw[N], nt;

void left(int (&board)[N][N])
{
    for(int i = 1; i <= siz; i ++)
    {
        tot = 0;
        nt = 0;
        memset(r, 0, sizeof r);
        memset(nw, 0, sizeof nw);
        for(int j = 1; j <= siz; j ++)
            if(board[i][j] != 0) r[++ tot] = board[i][j];
        for(int j = 1; j <= tot; j ++)
            if(r[j] == r[j + 1]) j ++, nw[++ nt] = r[j] + 1, score += (1 << (r[j] - 1));
            else nw[++ nt] = r[j];
        memset(board[i], 0, sizeof board[i]);
        for(int j = 1; j <= nt; j ++)
            board[i][j] = nw[j];
    }
}

void right(int (&board)[N][N])
{
    for(int i = 1; i <= siz; i ++)
    {
        tot = 0;
        nt = 0;
        memset(r, 0, sizeof r);
        memset(nw, 0, sizeof nw);
        for(int j = 1; j <= siz; j ++)
            if(board[i][j] != 0) r[++ tot] = board[i][j];
        for(int j = 1; j <= tot; j ++)
            if(r[j] == r[j + 1]) j ++, nw[++ nt] = r[j] + 1, score += (1 << (r[j] - 1));
            else nw[++ nt] = r[j];
        memset(board[i], 0, sizeof board[i]);
        for(int j = 1; j <= nt; j ++)
            board[i][siz - j + 1] = nw[nt - j + 1];
    }
}

void up(int (&board)[N][N])
{
    for(int i = 1; i <= siz; i++)
    {
        tot = 0;
        nt = 0;
        memset(r, 0, sizeof r);
        memset(nw, 0, sizeof nw);
        for(int j = 1; j <= siz; j ++)
            if(board[j][i] != 0) r[++ tot] = board[j][i];
        for(int j = 1; j <= tot; j++)
            if(r[j] == r[j + 1]) j ++, nw[++ nt] = r[j] + 1, score += (1 << (r[j] - 1));
            else nw[++ nt] = r[j];
        for(int j = 1; j <= siz; j++)
            board[j][i] = 0;
        for(int j = 1; j <= nt; j++)
            board[j][i] = nw[j]; 
    }
}

void down(int (&board)[N][N])
{
    for(int i = 1; i <= siz; i++)
    {
        tot = 0;
        nt = 0;
        memset(r, 0, sizeof r);
        memset(nw, 0, sizeof nw);
        for(int j = 1; j <= siz; j ++)
            if(board[j][i] != 0) r[++ tot] = board[j][i];
        for(int j = 1; j <= tot; j++)
            if(r[j] == r[j + 1]) j ++, nw[++ nt] = r[j] + 1, score += (1 << (r[j] - 1));
            else nw[++ nt] = r[j];
        for(int j = 1; j <= siz; j++)
            board[j][i] = 0;
        for(int j = 1; j <= nt; j++)
            board[siz - j + 1][i] = nw[nt - j + 1]; 
    }
}

bool cant_move()
{
    int board_res[N][N];
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            board_res[i][j] = board[i][j];
    left(board_res);
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            if(board_res[i][j] != board[i][j]) return false;
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            board_res[i][j] = board[i][j];
    right(board_res);
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            if(board_res[i][j] != board[i][j]) return false;
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            board_res[i][j] = board[i][j];
    up(board_res);
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            if(board_res[i][j] != board[i][j]) return false;
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            board_res[i][j] = board[i][j];
    down(board_res);
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            if(board_res[i][j] != board[i][j]) return false;
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            board_res[i][j] = board[i][j]; 
    return true;
}

bool win()
{
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j ++)
            if(board[i][j] > 13) return true;
    return false;
}

int nowmx = 0;

int getmax()
{
    int res = 0;
    for(int i = 1; i <= siz; i ++)
        for(int j = 1; j <= siz; j++)
            res = max(res, board[i][j]);
    return res;
}

void display(int x)
{
    if(x == 1)
        MessageBox(NULL, "栈,一种基础的 FILO 的数据结构。", "栈", MB_OK);
    else if(x == 2)
        MessageBox(NULL, "队列,一种基础的 FIFO 的数据结构。", "队列", MB_OK);
    else if(x == 3)
        MessageBox(NULL, "堆,一种处理集合最值问题的工具。", "堆", MB_OK);
    else if(x == 4)
        MessageBox(NULL, "并查集,处理集合合并查询类问题的工具。", "并查集", MB_OK);
    else if(x == 5)
        MessageBox(NULL, "如果需要单点修改,区间查询,就靠树状数组了!", "树状数组", MB_OK);
    else if(x == 6)
        MessageBox(NULL, "区间修改,区间查询,正是线段树所擅长的问题。", "线段树", MB_OK);
    else if(x == 7)
        MessageBox(NULL, "树链剖分用于将树上问题映射到序列上。", "树链剖分", MB_OK);
    else if(x == 8)
        MessageBox(NULL, "对于集合问题,平衡树几乎是万能的工具。", "平衡树", MB_OK);
    else if(x == 9)
        MessageBox(NULL, "对于序列问题有着仅次于分块的处理能力,适配面十分广。", "树套树", MB_OK);
    else if(x == 10)
        MessageBox(NULL, "K-D Tree 是处理高维空间问题的利器。", "K-D Tree", MB_OK);
    else if(x == 11)
        MessageBox(NULL, "在树上,动态树就是无敌的。连边断边,链查链改!", "动态树", MB_OK);
    else if(x == 12)
        MessageBox(NULL, "一个很全面的数据结构,可惜用 LCT 维护的话会有 2 只 log。", "ETT", MB_OK);
    else if(x == 13)
        MessageBox(NULL, "这种数据结构也只会在重工业 Sone1 用得到了……", "SATT", MB_OK);
    else
        MessageBox(NULL, "我永远喜欢珂朵莉!", "珂朵莉树", MB_OK); 
}

int main()
{
    srand(time(0));
    ss(200, 50);
    printf("请输入大小(4-20):");
    while(!(4 <= siz && siz <= 20)) scanf("%d", &siz);
    while(1)
    {
        get_rd();
        output();
        if(cant_move())
        {
            MessageBox(NULL, "你失败了!无法移动!", "失败", MB_OK);
            return 0;
        }
        int k = getmax();
        if(k != nowmx)
        {
            for(int i = nowmx + 1; i <= k; i++) display(i);
            nowmx = k;
        }
        char c = 114, r = rand() % 4;
/*下面这个是自动随机代打的代码*/
        if(r == 0) c = 75;
        else if(r == 1) c = 77;
        else if(r == 2) c = 72;
        else c = 80;
/*下面这个是手动输入的代码*/
        //while(c != 75 && c != 77 && c != 72 && c != 80) c = _getch();
        if(c == 75) left(board);
        else if(c == 77) right(board);
        else if(c == 72) up(board);
        else down(board);
        if(win())
        {
            output(); 
            MessageBox(NULL, "你胜利了!你合成了珂朵莉树!", "胜利", MB_OK);
            display(14);
            return 0;
        }
        Sleep(10);
    }
    return 0;
}