[Google APAC 2015]Round A.Problem B.Super 2048
来源:互联网 发布:卖家怎么联系淘宝达人 编辑:程序博客网 时间:2024/06/08 05:11
题目描述
Problem B. Super 2048
6 points
13 points
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
解题思路
- 对于up操作,我们只需按列从行n-1向行0向上扫描;对于down操作,我们只需按列从行0向n-1向下扫描;对于left操作,我们只需按行从列0向n-1向右扫描;对于right操作,我们只需按行从列n-1向0向左扫描;
- 扫描的过程中,记录需要覆盖的位置cover,如果当前位置值v[current]==v[cover],那么v[cover] = v[cover] *2,v[current]置为0;否则,如果v[cover]==0,则置v[cover] = v[current],v[current]置为0,如果v[cover]!=0,则将cover--,v[cover] = v[current],v[current] = current==cover? v[current]:0;
代码
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.List;public class Main {/** * @param args */public static void main(String[] args) {try {solveSuper2048();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}/** * Google APAC 2015 University Graduates Test * Round A APAC Test * Problem B. Super 2048 * @throws IOException */public static void solveSuper2048() throws IOException {File file = new File("F:\\B-large-practice.in");BufferedReader br;BufferedWriter bw = new BufferedWriter(new FileWriter("F:\\B-large-practice.out"));br = new BufferedReader(new FileReader(file));String line = br.readLine();int T = Integer.parseInt(line);for (int i = 0; i < T; i++) {line = br.readLine();String[] line1 = line.split(" ");int N = Integer.parseInt(line1[0]);int[][] m = new int[N][N];for (int j = 0; j < N; j++) {String[] nums = br.readLine().split(" ");for (int k = 0; k < N; k++) {m[j][k] = Integer.parseInt(nums[k]);}}m = move(m, N, line1[1]);bw.write("Case #" + (i + 1) + ":" + "\n");for (int j = 0; j < N; j++) {for (int k = 0; k < N; k++) {bw.write(m[j][k] + " ");}bw.write("\n");}}br.close();bw.close();}/** * * @param m 方阵 * @param n 方阵边长 * @param dir 移动的方向 * @return */public static int[][] move(int[][] m, int n, String dir) {// i = column,j = row;if (dir.equals("up")) {for (int j = 0; j < n; j++) {int i = 1;int end = 0;while (i < n) {if (m[i][j] != 0) {if (m[i][j] == m[end][j]) {m[end][j] *= 2;m[i][j] = 0;end++;} else {if (m[end][j] == 0) {m[end][j] = m[i][j];m[i][j] = 0;} else {m[end + 1][j] = m[i][j];end++;m[i][j] = end == i ? m[i][j] : 0;}}}i++;}}} else if (dir.equals("down")) {for (int j = 0; j < n; j++) {int i = n - 2;int end = n - 1;while (i >= 0) {if (m[i][j] != 0) {if (m[i][j] == m[end][j]) {m[end][j] *= 2;m[i][j] = 0;end--;} else {if (m[end][j] == 0) {m[end][j] = m[i][j];m[i][j] = 0;} else {m[end - 1][j] = m[i][j];end--;m[i][j] = end == i ? m[i][j] : 0;}}}i--;}}} else if (dir.equals("left")) {for (int i = 0; i < n; i++) {int j = 1;int end = 0;while (j < n) {if (m[i][j] != 0) {if (m[i][j] == m[i][end]) {m[i][end] *= 2;m[i][j] = 0;end++;} else {if (m[i][end] == 0) {m[i][end] = m[i][j];m[i][j] = 0;} else {m[i][end + 1] = m[i][j];end++;m[i][j] = end == j ? m[i][j] : 0;}}}j++;}}} else if (dir.equals("right")) {for (int i = 0; i < n; i++) {int j = n - 2;int end = n - 1;while (j >= 0) {if (m[i][j] != 0) {if (m[i][j] == m[i][end]) {m[i][end] *= 2;m[i][j] = 0;end--;} else {if (m[i][end] == 0) {m[i][end] = m[i][j];m[i][j] = 0;} else {m[i][end - 1] = m[i][j];end--;m[i][j] = end == j ? m[i][j] : 0;}}}j--;}}}return m;}}
- [Google APAC 2015]Round A.Problem B.Super 2048
- Google APAC test 2015 Round B Problem A - Password Attacker
- Google APAC 2015第一轮 Problem B. Super 2048
- [Google Round A APAC 2017] Problem B. Rain
- Problem B. Rain Google APAC 2017 University Test Round A
- Problem A. Travel Google APAC 2016 University Test Round B
- Problem B. gCube Google APAC 2016 University Test Round A
- Google APAC test 2015 Round B Problem C - Card Game
- Google APAC test 2015 Round B Problem D-Parentheses Order
- Google 2015 APAC Round A Problem C - Addition
- Problem B. gWheels Google APAC 2016 University Test Round B
- Problem A. Sherlock and Parentheses Google APAC 2017 University Test Round B
- Google APAC test 2015 Round B Problem B - New Years Eve
- Problem B. gFiles Google APAC 2016 University Test Round C
- Problem B. gBalloon Google APAC 2016 University Test Round D
- Problem C. gNumbers Google APAC 2016 University Test Round B
- Problem A. Vote Google APAC 2017 University Test Round D
- Problem A. gRanks Google APAC 2016 University Test Round C
- 希尔排序-MW
- 数据库丢失或不完整处理方式:回滚
- linux下创建用户
- nc57单据开发注意点
- Ubuntu开机自动挂载Windows分区
- [Google APAC 2015]Round A.Problem B.Super 2048
- ios开发时,在Xcode中添加多个targets进行版本控制
- java——线程
- git的使用 [ ubuntu ]
- java打印的代码
- Word Break II
- 规则引用声明符:&&
- Linux对文件目录rwx权限的总结
- iOS 根据target不同执行不同的操作