先附代码:(简单地说就是给出一个矩阵代表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;
}
}
}
}

Problem

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.

2048 is played on a simple 4 x 4 grid with tiles that slide smoothly when a player moves them. For each movement, the player can choose to move all tiles in 4 directions, left, right, up, and down, as
far as possible at the same time. 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. In one movement, one newly created tile can not be merged again and always is merged
with the tile next to it along the moving direction first.
 E.g. if the three "2" are in a row "2 2 2" and the player choose to move left, it will become "4 2 0", the most left 2 "2" are merged.

image=2048.png&p=5742336445251584&c=3214486" alt="" style="border:0px; vertical-align:middle">The above figure shows how 4 x 4 grid varies when
player moves all tiles 'right'.

Alice and Bob accidentally find this game and love the feel when two tiles are merged. After a few round, they start to be bored about the size of the board and decide to extend the size of board to N x N,
which they called the game "Super 2048".

The big board then makes them dazzled (no zuo no die -_-| ). They ask you to write a program to help them figure out what the board will be looked like after all tiles move to one specific direction on
a given board.

Input

The first line of the input gives the number of test cases, TT test cases follow. The first line of each test case gives the side length of the board, N,
and the direction the tiles will move to, DIRN and DIR are separated by a single space. DIR will be one of four strings: "left", "right", "up", or "down".

The next N lines each contain N space-separated integers describing the original state of the board. Each line represents a row of the board (from top to bottom); each
integer represents the value of a tile (or 0 if there is no number at that position).

Output

For each test case, output one line containing "Case #x:", where x is the test case number (starting from 1). Then output N more lines, each containing N space-separated
integers which describe the board after the move in the same format as the input.

Limits

Each number in the grid is either 0 or a power of two between 2 and 1024, inclusive.

Small dataset

1 ≤ T ≤ 20 

1 ≤ N ≤ 4

Large dataset

1 ≤ T ≤ 100 

1 ≤ N ≤ 20

Sample



Input 

 


Output 

 
3
4 right
2 0 2 4
2 0 4 2
2 2 4 8
2 2 4 4
10 up
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0
3 right
2 2 2
4 4 4
8 8 8
Case #1:
0 0 4 4
0 2 4 2
0 4 4 8
0 0 4 8
Case #2:
4 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
Case #3:
0 2 4
0 4 8
0 8 16

google校招在线測试题---2048的更多相关文章

  1. Google2015校招在线測试题1----扫雷最少点击次数

    Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...

  2. 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题

    题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...

  3. (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)

    刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...

  4. 借用Google API在线生成网站二维码地址方法

    二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...

  5. google浙大招聘笔试题 师兄只能帮你到这儿了

    google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...

  6. 当当网-前端project师測试题

                                     前端project师測试题(笔试时间20分钟.面试时间20分钟)   一.笔试 1.基础问题 (1)前端页面有哪三层构成,各自是什么? ...

  7. jsfiddle在线測试Html、CSS、JavaScript——http://jsfiddle.net/

    jsfiddle在线測试Html.CSS.JavaScript,并展示測试结果 1.选择jQuery1.9.1 2.选择jQuery UI 1.9.2 3.Html <ul id="n ...

  8. 阿里腾讯校招Java面试题总结及答案

    阿里校招java面试题汇总 1.HashMap和HashTable的区别,及其实现原理. Hashtable继承自Dictionary类,而HashMap是Java1.2引进的,继承自Abstract ...

  9. Google Code Jam在线測试题目--Alien Language

    Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...

随机推荐

  1. Impala储存与分区

    不多说,直接上干货! hive的元数据存储在/user/hadoop/warehouse Impala的内部表也在/user/hadoop/warehouse. 那两者怎么区分,看前面的第一列. 下面 ...

  2. Sparse Coding: Autoencoder Interpretation

    稀疏编码 在稀疏自编码算法中,我们试着学习得到一组权重参数 W(以及相应的截距 b),通过这些参数可以使我们得到稀疏特征向量 σ(Wx + b) ,这些特征向量对于重构输入样本非常有用. 稀疏编码可以 ...

  3. BZOJ3511: 土地划分(最小割)

    Description Y国有N座城市,并且有M条双向公路将这些城市连接起来,并且任意两个城市至少有一条路径可以互达. Y国的国王去世之后,他的两个儿子A和B都想成为新的国王,但他们都想让这个国家更加 ...

  4. visualSVN+花生壳实现外网访问局域网内SVN

    使用SubVersion+TortoiseSVN局域网内访问SVN成功后,想从外网访问SVN,使用花生壳绑定路由器动态DNS,但是折腾半天没搞定,突然发现一个帖子http://hi.baidu.com ...

  5. [Docker 官方文档] 理解 Docker

    http://segmentfault.com/a/1190000002609286 什么是 Docker? Docker 是一个用于开发.交付和执行应用的开放平台,Docker 设计用来更快的交付你 ...

  6. js---26组合模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  7. 新手前端笔记之--初识html标签

    接触前端(好大气的名字啊)已经一个多月了,看了很多视频和博客,有了一定的感性认识,但还是需要总结一下以便系统化所学习的知识,就从html标签开始吧.关于标签,谈论最多的就是简洁和语义化.简洁指html ...

  8. BZOJ5020: [THUWC 2017]在美妙的数学王国中畅游(LCT,泰勒展开,二项式定理)

    Description 数字和数学规律主宰着这个世界.   机器的运转,   生命的消长,   宇宙的进程,   这些神秘而又美妙的过程无不可以用数学的语言展现出来.   这印证了一句古老的名言:   ...

  9. 通过no-gui模式运行jmeter脚本与生成报告

    说明:使用NO-GUI 模式,即命令行模式运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 步骤:在GUI(图形化界面)模式调整好脚本,通过FTP工具将需要测试的.jmx文件传输到linux ...

  10. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Palindrome Function

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6156 [题意] 已知函数f(x, k),如果10进制数x在k进制下是个回文数,那么f(x, k)值为k, ...