Position and direction on grid | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Position and direction on grid

Movement is in the following rules: 1. At a white square on my grid(indicated by a zero),turn 90 degrees right, move forward one square. 2.At a black square on my grid(indicated by a one), turn 90 degrees left, move forward one square Basically I need to get the new location on my grid after I was on a certain location. Code needs to accept the colour of the cell I'm currently at, as well as position and direction I am facing and computes the cell's new colour, new position and direction after taking a single step. INPUT: The first line of input is a single integer, representing the colour of the cell. The next line consists of two integers m and n and a character d, separated by a single space, specifying the row and column location of the ant, and the direction it is facing (either N, S, E, or W for the four cardinal directions). for example. Input: 1 3 2 E Output: 0 2 2 N https://code.sololearn.com/cyW9P3xVI3Bd/?ref=app

31st May 2022, 11:50 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
17 Answers
+ 1
Ok, I'm sorry for all the blunders. I think I finally fixed it. I checked that it gives the same results as the code in the first reply (the one with multiple if/else if, which presumably works but I wanted to "simplify" it - I wish I didn't :D) https://code.sololearn.com/crVC57xgEnKp https://code.sololearn.com/cgsHo14VlNMu
4th Jun 2022, 11:13 AM
lion
lion - avatar
0
if(d == 'N') color ? (--n, d = 'W') : (++n, d = 'E'); else if(d == 'S') color ? (++n, d = 'E') : (--n, d = 'W'); else if(d == 'E') color ? (--m, d = 'N') : (++m, d = 'S'); else if(d == 'W') color ? (++m, d = 'S') : (--m, d = 'N'); Idk where you got the 0 in your output, if it's the opposite of the input color, then do color = !color;
4th Jun 2022, 7:23 AM
lion
lion - avatar
0
lion the 0 is an indication that I'm now on a white colour of the cell now after initial position if I move a step
4th Jun 2022, 7:28 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
so, it's like a checkerboard. You know, if you could encode d as an index in "NESW", aka 0 for N, 1 for E etc, you could reduce the code to this: d = (d+1+c*2) % 4; m += (d-1)%2; n -= (d-2)%2; c = !c; Like here: https://code.sololearn.com/cSCx0hUEihZ4
4th Jun 2022, 8:10 AM
lion
lion - avatar
0
SORRY, the previous reply was wrong, I'll work out the problem and come back! OK, I fixed it now 😇
4th Jun 2022, 8:31 AM
lion
lion - avatar
0
lion when I run the code it just gives me an output, remember I have to input the initial position on grid(1 or 0 for black or white) on first line and the coordinates on the second line indicating row,column ,direction and then give me an output when I take a step following the movement rules I provided
4th Jun 2022, 9:08 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
oh, I initialized the values in the code because I don't like to enter input on Sololearn, and I wanted to spare you that 🙂 But ok, here I made a slightly different version which also asks for input: https://code.sololearn.com/cvZ9aFfsduCl
4th Jun 2022, 9:26 AM
lion
lion - avatar
0
lion I'm getting the wrong output
4th Jun 2022, 9:28 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
I must have made a mistake again 😮 What was the exact input you gave it?
4th Jun 2022, 9:30 AM
lion
lion - avatar
0
lion what you have to note is that initially the grid looks like my grid on the code I provided. Input this on my code and see how the grid initially looks like. 5 5 0 0 0 0 0 1 0 0 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
4th Jun 2022, 9:31 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
lion the input was : 1 3 2 E
4th Jun 2022, 9:32 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
If you use the colors from your grid, why do you also give the color as input? Shouldn't it be taken from the grid, given the coordinates? Also, grid[3][2] is 0 (not 1 as in your input). Am I to assume the input coordinates are 1-based, and not 0-based like in C/C++? You do display it using 0-based coordinates in your code though. Here I took the output color from your grid, using 1-based coordinates: https://code.sololearn.com/crVC57xgEnKp/ (I used a char[][] instead of vector<vector<char>> because it was easier to initialize without reading it as input, but it should work the same with your vector grid)
4th Jun 2022, 10:36 AM
lion
lion - avatar
0
lion I'm still not getting the desired output I get wrong coordinates
4th Jun 2022, 10:41 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
wait, it's still wrong :(
4th Jun 2022, 10:44 AM
lion
lion - avatar
0
lion run it by yourself and see
4th Jun 2022, 10:45 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
lion this what I was looking for.🔥🤝
4th Jun 2022, 11:24 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar
0
lion 🐐 🐐 🔥🔥🔥🔥🤝
4th Jun 2022, 11:25 AM
Siyabonga Mbendane
Siyabonga Mbendane - avatar