先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵)
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
int T;
ifstream ifile("B-large-practice.in");
ofstream ofile("out1.txt");
int num[21][21];
ifile >> T;
for(int n_case = 1; n_case <= T; n_case++)
{
for(int i = 0; i < 21; i++)
for(int j = 0; j < 21; j++)
num[i][j] = 0;
int n;
string flag;
ifile >> n;
ifile >> flag;
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
ifile >> num[i][j];
if(flag == "up")
{
for(int j = 0; j < n; j++)
{
for(int i = 0; i < n; i++)
{
if(num[i][j] != 0)
{
int tmp_i = i;
while(num[++i][j] == 0)
{;}
if(i < n && num[tmp_i][j] == num[i][j])
{
num[tmp_i][j] = num[tmp_i][j] * 2;
num[i][j] = 0;
}
i--;
}
}
}
for(int j = 0; j < n; j++)
{
int index = 0;
for(int i = 0; i < n; i++)
if(num[i][j] != 0)
{
int tmp = num[index][j];
num[index][j] = num[i][j];
num[i][j] = tmp;
index++;
}
}
ofile << "Case #" << n_case << ":\n";
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
ofile << num[i][j];
if(j != n - 1)
ofile << ' ';
}
ofile << endl;
}
}
else if(flag == "down")
{
for(int j = 0; j < n; j++)
{
for(int i = n - 1; i > -1; i--)
{
if(num[i][j] != 0)
{
int tmp_i = i;
while(num[--i][j] == 0)
{}
if(i >= 0 && num[tmp_i][j] == num[i][j])
{
num[tmp_i][j] = num[tmp_i][j] * 2;
num[i][j] = 0;
}
i++;
}
}
}
for(int j = 0; j < n; j++)
{
int index = n - 1;
for(int i = n - 1; i >= 0; i--)
if(num[i][j] != 0)
{
int tmp = num[index][j];
num[index][j] = num[i][j];
num[i][j] = tmp;
index--;
}
}
ofile << "Case #" << n_case << ":\n";
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
ofile << num[i][j];
if(j != n - 1)
ofile << ' ';
}
ofile << endl;
}
}
else if(flag == "left")
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
if(num[i][j] != 0)
{
int tmp_j = j;
while(num[i][++j] == 0)
{}
if(j < n && num[i][tmp_j] == num[i][j])
{
num[i][tmp_j] = num[i][tmp_j] * 2;
num[i][j] = 0;
}
j--;
}
}
}
for(int i = 0; i < n; i++)
{
int index = 0;
for(int j = 0; j < n; j++)
if(num[i][j] != 0)
{
int tmp = num[i][index];
num[i][index] = num[i][j];
num[i][j] = tmp;
index++;
}
}
ofile << "Case #" << n_case << ":\n";
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
ofile << num[i][j];
if(j != n - 1)
ofile << ' ';
}
ofile << endl;
}
}
else
{
for(int i = 0; i < n; i++)
{
for(int j = n - 1; j >= 0; j--)
{
if(num[i][j] != 0)
{
int tmp_j = j;
while(num[i][--j] == 0)
{}
if(j >= 0 && num[i][tmp_j] == num[i][j])
{
num[i][tmp_j] = num[i][tmp_j] * 2;
num[i][j] = 0;
}
j++;
}
}
}
for(int i = 0; i < n; i++)
{
int index = n - 1;
for(int j = n - 1; j >= 0; j--)
if(num[i][j] != 0)
{
int tmp = num[i][index];
num[i][index] = num[i][j];
num[i][j] = tmp;
index--;
}
}
ofile << "Case #" << n_case << ":\n";
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++)
{
ofile << num[i][j];
if(j != n - 1)
ofile << ' ';
}
ofile << endl;
}
}
}
}
2048 is a famous single-player game in which the objective is to slide tiles on a grid to combine them and create a tile with the number 2048.