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
时间: 2024-10-31 08:49:15

google校招在线测试题---2048的相关文章

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

Problem Minesweeper is a computer game that became popular in the 1980s, and is still included in some versions of the Microsoft Windows operating system. This problem has a similar idea, but it does not assume you have played Minesweeper. In this pr

google 2014在线笔试题

1.个人做google在线笔试题感觉时间很紧,笔试时间只做出来了一个题目就是第二道题,这第一道题是当天晚上才做出来的...突然感觉自己很水... 个人解决方案需要解决如下子问题 1)判断一个具有坏灯管的数字灯是否显示的是某个数字 2)判断一串灯管显示的是否是以某个数字为首递减的一串数字 3)检查一串匹配正确数字的灯管中坏掉的灯管是哪些 4)输出显示一串正确递减数字的灯管 4)输出显示一串有坏灯管的来显示数字的灯管 #include<iostream> #include <map>

去哪儿网2017校招在线笔试(前端工程师)编程题及JavaScript代码

编程题很简单.整个试卷结构为: 一.问答题: 对前端的理解,了解哪些框架库? 二.在线编程题:身份证分组 如下第一道:身份证分组 三.在线编程题:身份证分组.统计字符.酒店价格(三选二) 如下第二三四道题中三选二作答. 我也搞不懂为什么有两部分编程题~~~下面详细说一说编程题目及JS代码实现: 第一道:身份证分组 代码: 1 var line; 2 while(line = read_line()){ 3 while(line.indexOf(" ") != -1){ 4 line =

C++ 写的地图控件,支持google 百度 在线离线地图

C++处理google  百度地图在网上查阅了很多都是通过浏览器方式显示地图信息, 跟我目前项目很不符合, 所以仔细研究了一下C++方式显示地图.通过地图投影以及墨卡托投影,在通过平面地图计算经纬度. 最终今天终于搞定的初步功能,鼠标移动位置显示经纬度.切换Zoom  选择地图信息. 再也不用为C++调用浏览器控件不方便等因素发愁了. 每天进步一点点. 放上部分代码: //墨卡托坐标转像素坐标void MercatorToPixel(DOUBLE pdX, DOUBLE pdY, int piZ

indeed2017校招在线编程题(网测)三

A. Calculate Sequence 分析:就是斐波那契递推公式,但是初始值是指定的,只用求第10个数,数据范围和复杂度都比较小,直接写. B. 忘了叫啥了. 就是有a-j十个字符组成的字符串,要求去掉一些字符,使得剩下的长度大于k,且这些满足要求里面的字符串字典序最小的.然后需要枚举删除的字符串,2^10 = 1024,字符长度最大值100,所以复杂度为1024*100,在1s的时限内完全可以,直接编写. C. Anagram Multiple Number 这题好像更简单哎,求一个数的

阿里校招在线笔试题。。。感觉被虐了

1.按照CommonJS规范,在任何模块代码的作用域下内置了以下哪些变量? module context require exports 2.一下方法中哪些存在兼容性问题? spilt indexof trim Date.parse 3.下面哪些技术可用于优化 CSS 图片加载 ? ·       CSSSprite ·       SVGSprite ·       Iconfont ·       Base64 4.有一道选择题问哪些是html5的新特性. 5.从前端工程师的角度如何提高页面

阿里巴巴集团2015年秋季校招在线笔试附加题分析

刚做完,选择题做吐血,好多智力题....附加题有两道编程题.题面是回忆的内容. 1.在text中查找子串quary,返回符合匹配的quary中连续的最大的子串长度,例如 quary = "acbac",text = "acaccbabb",quary 中 "cba"是最大的连续子串,返回3. [分析] 两重循环获得quary的所有连续子串,使用KMP算法在text 查找匹配,如果匹配,则记录子串长度,最后返回最大的子串长度. 代码如下,编译通过

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

题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大. 思路:对于一个圆,里面整点个数的x是能确定的.你找到x的上下界就可以了.就是mix = ceil(x0-r)//因为是小的值,所以要向上取整.mxx=floor(x0+r)//大的值要向下取整 对于y.我们也能用欧股定理确定.y也是有一个范围.但是并不是所有y都要枚举的.明显.y的值是离圆心越远越好.所以对于每一个x而言只需要枚举最远的两个y值 #include <cs

Google APAC 2016 University Graduates Test(google校招笔试)

Problem A. Bad Horse 题意:给n个关系,每个关系有两个人,要求把这些人分成两组,每组里的人之间都没有关系. 二分图染色即可.关系=边,人=点. #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<queue> #include<set> #include&