pangfengliu / programmingtasks

programming tasks from my courses
68 stars 17 forks source link

Robot Simulation #334

Open littlehug opened 6 years ago

littlehug commented 6 years ago

Write a program to simulate a robot movement on a grid.

Task Description

We have an N by M grid G of squares with a robot in it. The horizontal index is from 0 to N - 1, and the vertical index is from 0 to M - 1. The robot can either move right, left, up and down within the grid. The robot moves according to a sequence of instructions, as positive integers between 1 and 1000. The robot moves as follows.

Note that if an instruction intends to move the robot out of the grid, the instruction is illegal, and the robot will not move.

Write a program to simulate a robot movement on a grid, and output the location of the robot for every instruction, except an illegal one. The program simulates the robot, which is initially located at (0, 0), and the movement according to the input instructions until the end of file. It is guaranteed that there is at least one instruction.

Subtask

Input Format

The input contains only one test case. The first line contains N and M, representing the width and height of grids. The second line contains the instruction for the robot, and the number of instructions is at least one.

Output Format

Print two lines for the initial location and each instruction except an illegal instruction. The first line contains one integer, representing the horizontal index of the robot, and the second line contains one integer, representing the vertical index of the robot.

Note

Do not use array for this problem. You can solve this problem with very little memory. We will make sure that you will not have enough memory, and see the "memory limit exceeded" error, if you use array.

Sample Input 1

10 20
1 3 6 7 8

Sample Output 1

0
0
1
0
1
3
7
3
0
3
0
11

Sample Input 2

10 20
1 2 3 4 5 6 7 8

Sample Output 2

0
0
1
0
1
3
7
3
0
3
0
11