先附代码:(简单地说就是给出一个矩阵代表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. 将BT下载对抗到底

    将BT下载对抗到底      随着互联网业务的多元化,各种P2P应用也越来越多,在企业中多数流量都会被类似于BT的下载所占用,BT之所以会危害到局域网,是因为它占用了大量网络带宽.网络管理员可以通过一 ...

  2. javafx KeyCombination

    import javafx.application.Application; import javafx.application.Platform; import javafx.event.Actio ...

  3. Model、ModelMap、ModelAndView的作用及区别

    Model.ModelMap.ModelAndView的作用及区别 对于MVC框架,控制器controller执行业务逻辑 用于产生模型数据Model 视图view用来渲染模型数据 Model和Mod ...

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

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

  5. 【Codeforces Round #427 (Div. 2) A】Key races

    [Link]:http://codeforces.com/contest/835/problem/A [Description] [Solution] 傻逼题. [NumberOf WA] [Revi ...

  6. C# 的时间戳 在flash actionscript中使用

    眼下在做一个项目,要以字节的方式传时间戳到flash中, 错误的就不写了.仅仅写一个能够使用的例如以下: C# DateTime centuryBegin = new DateTime(1970, 1 ...

  7. Install Docker Mac OS X

    检查 Mac OS version 要求必须是 OS X 10.6 Snow Leopard or newer to run Boot2Docker 安装 Boot2Docker 列表内容 下载地址: ...

  8. 思科模拟器之路由器-RIP-DNS解析server

    思科三层交换机之下的局域网搭建,请看这. 接下来将解说怎样通过路由器的RIP协议来连接多个局域网. 并设置DNSserver. 1.路由器RIP配置 RIP协议有个非常致命的缺点:就是它是依据路径长短 ...

  9. 移动GPU全解读(二)

    [编者按]:本文作者为爱搞机特约作者.技术达人"炮神"@ioncannon. 在上一篇移动GPU解读中,对移动GPU的架构.相关參数进行了介绍,本部分介绍的则是移动GPU的Shad ...

  10. Android开发经验小知识点

    <1> 设置屏幕无标题栏 在AndroidManifest.xml设置: Android:theme="@android:style/Theme.NoTitleBar"