Please help me fix my code for ROBOTG

My contest submissions here
My most recent submissions here

Can anyone of you take a look at my code and help me fix it? I saw other codes, the logic was similar. But I failed somewhere in implementation. This problem got me like “WHY ARE YOU NOT SOLVING WTFFF” quite badly, so I will appreciate if you guys can help me fix my code.(Meaning, I don’t want to know the answer, I want to see where I failed in my approach and how could I fix it to be correct.)

Thanks in advance :slight_smile:

EDIT- Logic-

My logic involved on checking at every step what the command is, modifying the change in vertical and/or horizontal direction accordingly. In case the ABSOLUTE change in vertical/horizontal direction proved greater than the limits n and m, the loop stops and prints “unsafe”.

A corner case was seen that-

Let n=2 and m=3. Let command be LLRRR. The “change” in horizontal is reported to be 1, and it will print safe. However, it is not the case.

Hence another 2 counters were set to record contiguous command changes. If a command in same direction occurred more times than the dimensions could accommodate, it would break out of loop and print “unsafe”

same here with same approach i got wa,check mine:

@vijju123 i think you are not placing the robot at every position of matrix n*m. Correct solution would be that you have to run the string command after placing the robot at every position of matrix N*M

Tell me if you need, i will provide which test case your code is giving WA

1 Like

give me any test case,plz

Actually I didn’t “check any position”. I made 2 assumptions- Maximum ups possible from bottom-most corner and vice versa, maximum lefts possible from rightmost corner and vice versa. So I checked that if absolute value of change in horizontal or vertical direction is more than grid dimension, print “unsafe”. Thanks, I will tell if I fail to find any test case. :slight_smile:

That’s a wrong approach! See other solutions! You will get idea about that!

3 3
This should give “unsafe” as an answer. Check your code


It is giving unsafe. I especially included 2 counters to check for contiguous same directions. Thanks for the test case though!

Your most recent code gives “safe”. I re-checked.
Are you giving input?
3 3

1 Like

Sorry, I found and fixed that bug just after contest :p. THANKS for pointing it out though! Updated the links. Sorry for inconvenience :slight_smile:

My solution also got WA whereas I can’t think of any test case where it might fail.
This solution also uses a similar approach and both of us get WA.
Can someone explain why this approach is wrong (provide a testcase where they fail, or plainly explain)?

1 Like

Please provide some test cases where this approach may fail

Check this one-

3 3

Answer is unsafe.

My solution does print unsafe for that case. The other one doesn’t, though. So, we have found a case breaking the second one but I can’t find any case which breaks the first one

To all those trying my approach-

I found that the approach was just full of loopholes. At least my approach was. My main variables failed to perform their intended function.

The correct way to approach this problem is finding, assume robot starts at (0,0) [this origin can be anywhere]. Now find the maximum and minimum values of x and y. The difference between maximum and minimum values could be used to tell if robot will fall off the grid. (i.e. if the difference b/w max and min of x is more than what number of columns can accommodate, then its obvious that robot will fall off the edge.)

2 rows 3 columns
Take case LLRRR.
Let positive x axis be along right (x increases on right and decreases on left).

Min X = -2
Max X= 1.

Difference = 3.

But note that robot falls on third jump.

==> if difference between Min X and Max X is STRICTLY LESS THAN 3 (or no. of columns), then robot will be safe, else it will fall.

Here difference is equal to 3. 3<3 is false, hence robot falls.

This is a cleaner way. I found that correcting my previous approach was like “stitching a cloth to hold water” (i.e. full of loopholes). That approach needed a condition for every type of case, and that’s a mammoth task.

Closing the question, however, if any of you ahs doubt, comment and I will try to resolve it.

Link to my code for reference here

@vijju123 Please help me with my code. I’m using the approach you suggested (maximum and minimum). The implementation might be a bit fuzzy but the concept is same. I can’t get a test case that breaks my solution. Any help will be much appreciated!

Ok. Give me time to debug.

Error spotted- Your calculation of max and minimum was outside loop. This was wrong. It would not be able to get any global minima etc occurring in middle of operations.

Corrections- I cut-pasted that part inside. Result? Hm…ACCEPTED XD.


Congratz bro!

Thanks man!

I find it silly asking, but what did you change?
I can’t find the difference between and