google校招在线測试题---2048
先附代码:(简单地说就是给出一个矩阵代表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, T. T 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, DIR. N 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 |
Case #1: |
google校招在线測试题---2048的更多相关文章
- Google2015校招在线測试题1----扫雷最少点击次数
Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in so ...
- 【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题
题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了. ...
- (各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)
刚才把最后的10道题又看了下.也发上来吧. 以下给出试题.和我对题目的一些理解 前10道题地址 (各个公司面试原题)在线做了一套CC++综合測试题.也来測一下你的水平吧(一) 11.设已经有A,B,C ...
- 借用Google API在线生成网站二维码地址方法
二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...
- google浙大招聘笔试题 师兄只能帮你到这儿了
google浙大招聘笔试题 一.单选1.80x86中,十进制数-3用16位二进制数表示为?00100002.假定符号-.*.$分别代表减法.乘法和指数运算,且 1)三个运算符优先级顺序是:-最高,*其 ...
- 当当网-前端project师測试题
前端project师測试题(笔试时间20分钟.面试时间20分钟) 一.笔试 1.基础问题 (1)前端页面有哪三层构成,各自是什么? ...
- 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 ...
- 阿里腾讯校招Java面试题总结及答案
阿里校招java面试题汇总 1.HashMap和HashTable的区别,及其实现原理. Hashtable继承自Dictionary类,而HashMap是Java1.2引进的,继承自Abstract ...
- Google Code Jam在线測试题目--Alien Language
Problem After years of study, scientists at Google Labs have discovered an alien language transmitte ...
随机推荐
- javaweb一
JavaWeb就是在服务器端以Java语言为解释运行基础的web程序. php代码要想在服务器端运行,需要在服务器软件(通常是Apache)上加php的解释器,Java也一样,但是这个解释器是Tomc ...
- 一个简易版的Angular js 三层 示例
var myApp = angular.module('produceline', []); myApp.factory('ajax', ["$http", "$q&qu ...
- 使用pandas导出PostgreSQL 模式下的所有表数据并保存
PostgreSQL PostgreSQL 是一个非常强大的数据库,它是一个免费的对象-关系数据库服务器(数据库管理系统).PostgreSQL支持大部分 SQL 标准, 在语句上也有很大的相似的地方 ...
- 【hdu 4289】Control
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=4289 [Description] 给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中 ...
- spring之AOP(转)
Spring之AOP篇: AOP框架是Spring的一个重要组成部分.但是Spring IOC 并不依赖于AOP,这就意味着你有权力选择是否使用AOP,AOP作为Spring IOC容器的一个补充,使 ...
- D3.js加载csv和json数据
1.加载数据的基本命令 D3提供了方法可以对不同的数据类型进行加载,比如d3.text(), d3.xml(), d3.json(), d3.csv(), 和d3.html(). <!DOCTY ...
- Java 批量修改文件后缀
import java.io.*; public class test { public void reName(String path, String from, String to) { File ...
- node:json与csv互转
[单个文件的转化] 1.安装json2csv模块将json转成csv jsonToCSV.js var fs = require('fs'); const Json2csvParser = r ...
- 【2017 Multi-University Training Contest - Team 9】Numbers
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6168 [题意] 有一个长度为n的序列a1--an,根据a序列生成了一个b序列,b[i] = a[i]+a ...
- 看好腾讯,鄙视百度(腾讯的核心竞争力,不是超过10亿的QQ的注册用户,也不是某一项产品、技术方面优势,而是“耐心”:懂得在合适的时间推出合适的产品。”)
百度,自始至终只是一个低劣的模仿者,且一切向前看,完全违背了一个搜索引擎所应该遵循的基本原则.谁给的钱多就能搜着谁,这跟贩毒有什么区别? 腾讯也在模仿别人,但是,它是模仿然后超越.在中国互联网发展历史 ...