google校招在线测试题---2048
来源:互联网 发布:农村淘宝服务站流程 编辑:程序博客网 时间:2024/04/29 11:04
先附代码:(简单地说就是给出一个矩阵代表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, 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
- google校招在线测试题---2048
- 2014校招 Google在线测试第二场题解 A
- 2014校招 Google在线测试第二场题解 B
- 2014校招 Google在线测试第二场题解 C
- 2014校招 Google在线测试第二场题解 D
- 2014校招 Google在线测试第二场题解 E
- 2014校招 Google在线测试第二场题解 C
- google 2015最新的校招测试题与思路
- 微软2014实习生及校招秋令营技术类职位在线测试第二题
- 2014年8月29日阿里校招在线笔试部分题--软件测试开发
- Google2015校招在线测试题1----扫雷最少点击次数
- Google 2014校招测试赛 Problem A
- Google 2014校招测试赛 Problem B
- Google 2014校招测试赛 Problem C
- 2015阿里巴巴校招在线笔试题
- 2015阿里巴巴校招在线笔试题
- [2017腾讯校招在线笔试题]
- 阿里校招在线编程题
- 打造字符界面的多媒体Linux系统
- 基于 Windows 7 的高低权限进程通信的研究与实现
- 自定义多选TextView组件
- cocos2dx lua 下查找文件是否存在的方法
- Android4.4源码及内核编译整理学习
- google校招在线测试题---2048
- FMDB
- tcpdump抓包
- 为什么程序员需要关心顺序一致性(Sequential Consistency)而不是Cache一致性(Cache Coherence)
- IT忍者神龟之Hibernate联合主键下,主键生成策略
- 在iOS的XCode工程配置中为什么要用-all_load&-ObjC
- CocoaPods安装和使用教程
- 交通换乘GIS地图应用解决方案
- 对二维数据进行边界拓展