50065. Move the Car

I'm a slow walker, but I never walk backwards.

Task Description

Given the initial position and the amount of gas of a car, and a series of $N$ commands, write a function carSimulation to simulate the movements of the car and put the result into a CarStatusList structure.

The status of the car is given by a structure CarStatus. It contains the position and the amount of gas of the car, defined as follows:

typedef struct{
    int x, y;
    int g;
} CarStatus;

The car will move according to the commands. Each command is an instance of structure Command, containing a command type $t$ and a value $v$. The structure is defined as follows:

typedef struct{
    int t, v;
} Command;

There are six types of commands, and the command type $t$ is an integer from $0$ to $5$. Let the current position of car be $(x, y)$, and the gas amount be $g$:

  • $t = 0$ - the car stops and the value $v$ is ignored.
  • $t = 1$ - the gas amount increases to $(g + v)$.
  • $t = 2$ - the car moves to $(x+v, y)$ and the amount of gas decreases to $(g - v)$.
  • $t = 3$ - the car moves to $(x-v, y)$ and the amount of gas decreases to $(g - v)$.
  • $t = 4$ - the car moves to $(x, y+v)$ and the amount of gas decreases to $(g - v)$.
  • $t = 5$ - the car moves to $(x, y-v)$ and the amount of gas decreases to $(g - v)$.

A movement is invalid if the the amount of gas is not enough for the movement. An invalid movement should not be performed and the car will stop and ignore the rest of the commands.

Your goal is to create a CarStatus instance after each valid movement command, i.e. command type from $2$ to $5$. Put the results into a CarStatusList structure, defined as follows:

typedef struct{
    int num;
    CarStatus status[MAXN];
} CarStatusList;

Integer $num$ is the number of valid movements, the car information after the $i$-th movement should be put into $status\lbrack i]$.

The prototype of the function you need to implement is as follows:

CarStatusList carSimulation(CarStatus car, Command commands[]);

The commands is guaranteed to have a stop command (type $t$ equals to $0$). However, the simulation should also terminate when it encountered an invalid movement command.

Header and Main Program

car.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef _CAR_H
#define _CAR_H
 
#define MAXN 100
 
typedef struct{
    int x, y;
    int g;
} CarStatus;
 
typedef struct{
    int t, v;
} Command;
 
typedef struct{
    int num;
    CarStatus status[MAXN];
} CarStatusList;
 
CarStatusList carSimulation(CarStatus car, Command commands[]);
 
#endif

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <assert.h>
#include "car.h"
 
int main(){
    int N;
    CarStatus car;
    Command commands[MAXN];
 
    assert(scanf("%d%d%d", &car.x, &car.y, &car.g) == 3);
    assert(scanf("%d", &N) == 1);
    for(int i = 0; i < N; i++)
        assert(scanf("%d%d", &commands[i].t, &commands[i].v) == 2);
 
    CarStatusList result= carSimulation(car, commands);
 
    for(int i = 0; i < result.num; i++)
        printf("%d %d %d\n", result.status[i].x, result.status[i].y, result.status[i].g);
 
    return 0;
}

Complie

gcc -std=c99 -O2 -c car.c -lm
gcc -std=c99 -O2 -c main.c -lm
gcc -std=c99 -O2 car.o main.o -lm

Input Format

Input contains one test case. The first line contains three integers $sx$, $sy$ and $sg$, indicating the initial position and gas amount of the car. The second lines contains an integer $N$, indicating the number of commands. For the next $N$ lines, each line contains two integers $t$ and $v$, indicating the command type and command value.

  • $-1000 \leq sx, sy \leq 1000$
  • $0 \leq sg \leq 1000$
  • $1 \leq N \leq 110$
  • $0 \leq t \leq 5$
  • $1 \leq v \leq 100$

Output Format

Output the car status after each valid movement.

Sample Input 1

20 30 50
4
2 5
1 6
4 3
0 100

Sample Output 1

25 30 45
25 33 48

Sample Input 2

50 10 5
6
3 4
4 20
1 10
3 6
2 11
0 10

Sample Output 2

46 10 1

Discussion