跳到正文

休闲·娱乐

OI 2048

__LCT__约 14 分钟
原文

这是 2026年3月18日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年3月18日
收录
2026年3月18日
更新
2026年10月12日
篇幅
约 14 分钟

版本

  1. v5当前
  2. v4
  3. v3
  4. v2
更早的 1 个版本
  1. v1
C++
#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h> 
#define endl '\n'
using namespace std;

const int N = 15;
int board[N][N], score;

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
		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
		setcolor("purple"), printf(" 动态树 "), setcolor("white");
}

void output()
{
	system("cls"); 
	printf("合成");
	output_single(11);
	printf("\n");
	printf("你目前的得分是:%d\n", score); 
	for(int i = 1; i <= 5; i ++)
	{
		if(i == 1) printf("╭");
		else printf("|");
		if(i != 1) for(int i = 1; i <= 44; i ++) printf("-");
		else for(int i = 1; i <= 43; i ++) printf("-");
		if(i == 1) printf("╮");
		else printf("|");
		printf("\n|");
		for(int j = 1; j <= 5; j ++)
		{
			output_single(board[i][j]);
			printf("|");
		}
		printf("\n");
	}
	printf("╰");
	for(int i = 1; i <= 44; i ++) printf("-");
	printf("╯");
}

void get_rd()
{
	int x = rand() % 5 + 1, y = rand() % 5 + 1, cnt = 0;
	while(board[x][y] != 0 && cnt <= 1000)
	{
		x = rand() % 5 + 1;
		y = rand() % 5 + 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 <= 5; i ++)
	{
		tot = 0;
		nt = 0;
		memset(r, 0, sizeof r);
		memset(nw, 0, sizeof nw);
		for(int j = 1; j <= 5; 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 <= 5; i ++)
	{
		tot = 0;
		nt = 0;
		memset(r, 0, sizeof r);
		memset(nw, 0, sizeof nw);
		for(int j = 1; j <= 5; 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][5 - j + 1] = nw[nt - j + 1];
	}
}

void up(int (&board)[N][N])
{
	for(int i = 1; i <= 5; i++)
	{
		tot = 0;
		nt = 0;
		memset(r, 0, sizeof r);
		memset(nw, 0, sizeof nw);
		for(int j = 1; j <= 5; 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 <= 5; 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 <= 5; i++)
	{
		tot = 0;
		nt = 0;
		memset(r, 0, sizeof r);
		memset(nw, 0, sizeof nw);
		for(int j = 1; j <= 5; 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 <= 5; j++)
			board[j][i] = 0;
		for(int j = 1; j <= nt; j++)
			board[5 - j + 1][i] = nw[nt - j + 1]; 
	}
}

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

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

int main()
{
	srand(time(0));
	ss(80, 20);
	while(1)
	{
		get_rd();
		output();
		if(cant_move())
		{
			MessageBox(NULL, "你失败了!无法移动!", "失败", MB_OK);
			return 0;
		}
		char c = 'a';
		while(c != 'l' && c != 'r' && c != 'u' && c != 'd') c = getch();
		if(c == 'l') left(board);
		else if(c == 'r') right(board);
		else if(c == 'u') up(board);
		else down(board);
		if(win())
		{
			output(); 
			MessageBox(NULL, "你胜利了!你合成了动态树!", "胜利", MB_OK);
			return 0;
		}
	}
	return 0;
}