google校招在线测试题---2048

2024-08-24 06:32

本文主要是介绍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.

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1101717

相关文章

水位雨量在线监测系统概述及应用介绍

在当今社会,随着科技的飞速发展,各种智能监测系统已成为保障公共安全、促进资源管理和环境保护的重要工具。其中,水位雨量在线监测系统作为自然灾害预警、水资源管理及水利工程运行的关键技术,其重要性不言而喻。 一、水位雨量在线监测系统的基本原理 水位雨量在线监测系统主要由数据采集单元、数据传输网络、数据处理中心及用户终端四大部分构成,形成了一个完整的闭环系统。 数据采集单元:这是系统的“眼睛”,

电力系统中的A类在线监测装置—APView400

随着电力系统的日益复杂和人们对电能质量要求的提高,电能质量在线监测装置在电力系统中得到广泛应用。目前,市场上的在线监测装置主要分为A类和B类两种类型,A类和B类在线监测装置主要区别在于应用场景、技术参数、通讯协议和扩展性。选择时应根据实际需求和应用场景综合考虑,并定期维护和校准。电能质量在线监测装置是用于实时监测电力系统中的电能质量参数的设备。 APView400电能质量A类在线监测装置以其多核

JavaFX应用更新检测功能(在线自动更新方案)

JavaFX开发的桌面应用属于C端,一般来说需要版本检测和自动更新功能,这里记录一下一种版本检测和自动更新的方法。 1. 整体方案 JavaFX.应用版本检测、自动更新主要涉及一下步骤: 读取本地应用版本拉取远程版本并比较两个版本如果需要升级,那么拉取更新历史弹出升级控制窗口用户选择升级时,拉取升级包解压,重启应用用户选择忽略时,本地版本标志为忽略版本用户选择取消时,隐藏升级控制窗口 2.

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

12C 新特性,MOVE DATAFILE 在线移动 包括system, 附带改名 NID ,cdb_data_files视图坏了

ALTER DATABASE MOVE DATAFILE  可以改名 可以move file,全部一个命令。 resue 可以重用,keep好像不生效!!! system照移动不误-------- SQL> select file_name, status, online_status from dba_data_files where tablespace_name='SYSTEM'

css选择器和xpath选择器在线转换器

具体前往:Css Selector(选择器)转Xpath在线工具

com.google.gson.JsonSyntaxException:java.lang.IllegalStateException异常

用Gson解析json数据的时候,遇到一个异常,如下图: 这个异常很简单,就是你的封装json数据的javabean没有写对,你仔细查看一下javabean就可以了 比如:我的解析的代码是             Gson gson = new Gson();             ForgetJson rb = gson.fromJson(agResult.mstrJson, For

C/C++ 网络聊天室在线聊天系统(整理重传)

知识点: TCP网络通信 服务端的流程: 1.创建socket套接字 2.给这个socket绑定一个端口号 3.给这个socket开启监听属性 4.等待客户端连接 5.开始通讯 6.关闭连接 解释: socket:类似于接口的东西,只有通过这个才能跟对应的电脑通信。 每一台电脑都有一个IP地址,一台电脑上有多个应用,每个应用都会有一个端口号。 socket一般分为两种类型,一种是通讯,一种是监听

Google Earth Engine——高程数据入门和山体阴影和坡度的使用

目录 山体阴影和坡度 对图像应用计算 应用空间减速器 高程数据 通过从“重置”按钮下拉菜单中选择“清除脚本”来清除脚本。搜索“elevation”并单击 SRTM Digital Elevation Data 30m 结果以显示数据集描述。单击导入,将变量移动到脚本顶部的导入部分。将默认变量名称“image”重命名为“srtm”。使用脚本将图像对象添加到地图: Map