题目描述
Problem B. Super 2048
Small input 6 points |
|
Large input 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 block
e.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;
}
}