0% found this document useful (0 votes)
69 views

Programming Homework

The document describes a 2048 sliding puzzle problem. The puzzle is a 4x4 grid with tiles of value 2^n from 1 to 8192. Tiles slide and combine based on the direction of movement. The problem requires writing code to slide tiles on the grid according to a sequence of directions and display the final grid. Sample input and output are provided to demonstrate sliding two directions.

Uploaded by

yankev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Programming Homework

The document describes a 2048 sliding puzzle problem. The puzzle is a 4x4 grid with tiles of value 2^n from 1 to 8192. Tiles slide and combine based on the direction of movement. The problem requires writing code to slide tiles on the grid according to a sequence of directions and display the final grid. Sample input and output are provided to demonstrate sliding two directions.

Uploaded by

yankev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

12/23/14, 12:17 AM

10295 - I2P homework9


Time limit: s
Memory limit: MB

Problem Description
The game of 2048 is very simple. Given a 4x4 map containing tiles and spaces. The values are displayed on
the tiles, and the spaces are displayed as 0. The values of tiles are always the power of 2. For example,
2
2
64 128
4
0
4
16
4
4
4
4
0
0
2
0
is a valid map.
We are able to slide the tiles in 4 directions(right, down, left and up). The rules of sliding is defined as
follows:
1. All tiles slide past spaces. If you move the puzzle to the left, all spaces will end up on the right.
2. After sliding to collapse spaces, any tiles with the same value combine and the remainder of the row or
column slides to fill in the hole.
3. One tile can only combine(be combined) once.
4. The combination always starts from the side of the direction. For example, after sliding left of 2, 2, 2, 0, it
should be 4, 2, 0, 0.
5. Contrary to the famous game of 2048, there are no new tiles generated.
The direction is represented by a digit d(0 = right, 1 = down, 2 = left, 3 = up). So for instance if we have the
map above and d = 2, after we slide the tiles, the map becomes
4
64 128
0
8
16
0
0
8
8
0
0
2
0
0
0
Your job is to slide the tiles correctly given a map and a sequence of directions.
Hint:
#include <stdio.h>
#define WIDTH 4
#define HEIGHT 4
int map[HEIGHT][WIDTH];
void collapse(int dir){
/* your code here */
}
void slide(int dir){
/* your code here */
http://acm.cs.nthu.edu.tw/problem_print.php?pid=10295

Page 1 of 3

12/23/14, 12:17 AM

}
void show(){
int i, j;
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
printf(" %4d", map[i][j]);
}
printf("\n");
}
}
int main()
{
int N, d;
int i, j;
scanf("%d", &N);
for(i = 0; i < HEIGHT; i++){
for(j = 0; j < WIDTH; j++){
scanf("%d", &map[i][j]);
}
}
for(i = 0; i < N; i++){
scanf("%d", &d);
slide(d);
}
show();
return 0;
}

Input
N(the count of slides)
The map
The sliding sequence

Output
The final map
Note that the value of tiles will not exceed 8192.

Sample Input
2

2
4

2
0

64
4

128
16

http://acm.cs.nthu.edu.tw/problem_print.php?pid=10295

Page 2 of 3

12/23/14, 12:17 AM

2 3

4
0

4
0

4
2

4
0

128
0
0
0

0
0
0
0

Sample Output
4
16
2
0

64
16
8
0

http://acm.cs.nthu.edu.tw/problem_print.php?pid=10295

Page 3 of 3

You might also like