Please help me fix my code for ROBOTG

My solution CodeChef: Practical coding for everyone also got WA whereas I can’t think of any test case where it might fail.
This solution CodeChef: Practical coding for everyone 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

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.)

Example-
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

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!

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.
https://code.hackerearth.com/5e46b7z
Are you giving input?
1
3 3
LLRLRRR

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:

Please provide some test cases where this approach may fail

Check this one-

1
3 3
LLDDRRUR

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

@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.

Link- CodeChef: Practical coding for everyone

Congratz bro!

Thanks man!

I find it silly asking, but what did you change?
I can’t find the difference between CodeChef: Practical coding for everyone and CodeChef: Practical coding for everyone

@vijju123 So, I saw what you changed. Doesn’t make any sense to me. I am already doing calculations for max inside the loop. Doing them again makes it redundant, I think. I kept the last bit outside the loop because there were no comparisons in the end

In that case dear, here is the test case input 10003 6DDLRURLRD8 8LLU10 1R3 2RUDU6 10ULRRLLUDUL5 1DLDUD - Pastebin.com and here is expected output safesafeunsafesafesafeunsafesafesafeunsafeunsafesafesafe - Pastebin.com Check that for which case your code fails, and see how loops etc. work. There must be some minor mistake, because shifting that calculation inside loop gave AC. Have a check! :slight_smile:

Thank you so much for that input. I checked mine against a correct one. It turns out that the mistake was a typo. After correcting the typo, I got AC. Thank you for helping me out!
Here’s the solution, if you want to see CodeChef: Practical coding for everyone

1 Like

I am glad it worked dear :slight_smile: