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

C Programming Exercice

The document describes a programming problem to simulate the movement of a car on a 2D grid map to collect jewels. The car is represented as a 2D array and can rotate clockwise and move forward. The program must implement functions for rotating the car, moving it, and placing/removing objects on the map. The input specifies jewels, walls, and a sequence of commands, and the output displays the car's final position and earnings.

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)
88 views

C Programming Exercice

The document describes a programming problem to simulate the movement of a car on a 2D grid map to collect jewels. The car is represented as a 2D array and can rotate clockwise and move forward. The program must implement functions for rotating the car, moving it, and placing/removing objects on the map. The input specifies jewels, walls, and a sequence of commands, and the output displays the car's final position and earnings.

Uploaded by

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

12/23/14, 12:14 AM

10161 - I2P homework5


Time limit: s
Memory limit: MB

Problem Description
Complete the following program of car and jewels.
map[][] is a two-dimensional array representing the content of the map.
map_reset() is to clear the map and reset the boundary.
map_show() is to display the map.
blocks[][] is also a two-dimensional array that indicating the locations of brick walls.
We may use blocks_read() to read the locations of brick walls from the input, and then use
blocks_put_on_map() to place the bricks at the indicated locations in the map.
Note that the car will stop moving if it runs into a wall.
jewels[][] is a two-dimensional array recording the locations of jewels.
We use jewels_read() to read the jewel locations from the input, and then use jewels_put_on_map() to place
the jewels at the indicated locations in the map.
Note that if the cars location overlaps the locations of some jewels, those jewels will be picked up into the
car.
The cars shape is also represented by a two-dimensional array. The two symbols @ @ denoting the head
lights, so we can decide which direction the car is heading by the two symbols @ @. The starting position of
the car is row 3 column 4 (the center of the car). We use two variables car_row and car_col to store the cars
current position. The variable car_direction denotes the cars direction. The variable car_earnings denotes
the number of picked jewels in the car.
Note that we can use 0, 1, 2, 3 to represent the cars direction as right, down, left, and up. That is, if the value
of car_direction is 2, it means that the car is moving toward the left. The initial value of car_direction is 3
(heading upward).
Our task is to complete the two functions car_rotate90() and car_move()
The function car_rotate90() is to rotate the car (2D array) clockwise by 90 degrees and update the value of
car_direction.
The function car_move() is to move the car forward by one step according to the current direction of the car.
We need to check if the car is going to hit the wall before we let it move forward. We need to pick up the
jewel underneath the car before we move. Once the jewels are picked up, they will not be available on the
map anymore.
The input contains two types of commands, corresponding to two actions.
'R' represents rotating clockwise by 90 degrees
'F' represents moving forward by one step.
We need to show the final state of the car after carrying out all of commands. The output should include
car_earnings, car_row, car_col, and car_direction. Note that the size of the map is fixed to 12.
The following is an example of a possible game state at some moment.
HHHHHHHHHHHH
H....@[email protected]
http://acm.cs.nthu.edu.tw/problem_print.php?pid=10161

Page 1 of 5

12/23/14, 12:14 AM

H.#..OOO.#.H
H....OOO...H
H..........H
H..........H
H..........H
H..........H
H.......$..H
H.#......#.H
H...$......H
HHHHHHHHHHHH
The symbol 'H' denotes a wall at the map border, the symbol '#' denotes a brick wall, the symbol '$' denotes a
jewel, and the car is
@O@
OOO
OOO
In this case car_row is 2, car_col is 6 and car_direction is 3.
Note that when you test your program on your pc, you should press control+Z to send an EOF to the program
and thus exit the main loop.
The code template:
#include <stdio.h>
#define MAP_SIZE 12
#define CAR_SIZE 3
int map[MAP_SIZE][MAP_SIZE];
void map_reset(void);
void map_show(void);
int blocks[MAP_SIZE][MAP_SIZE];
void blocks_read(void);
void blocks_put_on_map(void);
int jewels[MAP_SIZE][MAP_SIZE];
void jewels_read(void);
void jewels_put_on_map(void);
int car[CAR_SIZE][CAR_SIZE] = {{'@', 'O', '@'}, {'O', 'O', 'O'}, {'O', 'O', 'O'}};
int car_row = 3, car_col = 4;
int car_direction = 3;
int car_earnings;
void car_rotate90(void);
void car_put_on_map(void);
void car_move(void);
int main(void)
{
int ch;
char dirs[] = "RDLU";

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

Page 2 of 5

12/23/14, 12:14 AM

jewels_read();
blocks_read();
while ((ch=getchar()) != EOF) {
if (ch=='R') {
car_rotate90();
}
if (ch=='F') {
car_move();
}
map_reset();
jewels_put_on_map();
blocks_put_on_map();
car_put_on_map();
/* You may call map_show() to print the current state.
Be sure to remove it before you submit the code */
/*map_show();*/
}
printf("%d\n", car_earnings);
printf("%d %d\n", car_row, car_col);
printf("%c\n", dirs[car_direction]);
return 0;
}
void blocks_read(void)
{
int n, i;
int row, col;
scanf("%d", &n);
for (i=0; i<n; i++) {
scanf("%d%d", &row, &col);
blocks[row][col] = 1;
}
}
void jewels_read(void)
{
int n, i;
int row, col;
scanf("%d", &n);
for (i=0; i<n; i++) {
scanf("%d%d", &row, &col);
jewels[row][col] = 1;
}
}
void blocks_put_on_map(void)
{
int i, j;
for (i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
if (blocks[i][j])
http://acm.cs.nthu.edu.tw/problem_print.php?pid=10161

Page 3 of 5

12/23/14, 12:14 AM

map[i][j] = '#';
}
}
}
void jewels_put_on_map()
{
int i, j;
for (i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
if (jewels[i][j])
map[i][j] = '$';
}
}
}
void map_reset(void)
{
int i, j;
for (i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
map[i][j] = '.';
}
}
for (i=0; i<MAP_SIZE; i++) {
map[i][0] = 'H';
map[i][MAP_SIZE-1] = 'H';
}
for (j=0; j<MAP_SIZE; j++) {
map[0][j] = 'H';
map[MAP_SIZE-1][j] = 'H';
}
}
void map_show(void)
{
int i, j;
for (i=0; i<MAP_SIZE; i++) {
for (j=0; j<MAP_SIZE; j++) {
printf("%c", map[i][j]);
}
printf("\n");
}
}
void car_put_on_map(void)
{
int i, j;
for (i=0; i<CAR_SIZE; i++) {
for (j=0; j<CAR_SIZE; j++) {
map[i+car_row-1][j+car_col-1] = car[i][j];
}
}
}
void car_rotate90(void)
{
http://acm.cs.nthu.edu.tw/problem_print.php?pid=10161

Page 4 of 5

12/23/14, 12:14 AM

/* your code */
}
void car_move(void)
{
/* your code */
}

Input
The input contains several lines. The first line contains an integer N indicating the number of jewels. The next
N lines are the locations of the N jewels. The next line contains an integer M indicating the number of brick
walls. The next M lines are the locations of the M brick walls. The last line of the input contains a sequence
of commands for the cars actions.
Note that the jewels may appear underneath the car in the beginning and should be picked up.

Output
The output contains three lines displayed using
printf("%d\n", car_earnings);
printf("%d %d\n", car_row, car_col);
printf("%c\n", dirs[car_direction]);

Sample Input
5
25
27
88
95
10 4
4
22
99
92
29
FRFFFFFFFFRRFFFFFFFFF

Sample Output
2
24
L

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

Page 5 of 5

You might also like