Why is my code wrong? It is not passing all the test cases

The link to my code is - https://ide.codingblocks.com/s/157351

I think you have sent wrong code as per the tag of question.

oh, i updated the code, can you look at this link now - https://ide.codingblocks.com/s/157351

So, I created two arrays l and r. l stores the time it will require to reach the maximum cell if standing on left side and vice versa for r

I might have badly managed my code, but what the first few lines of main do is - they read input in loop and if input is complete zero then i ignore it. Because, the person will only go till the maximum floor where the ight is on. Then, suppose I get some rows with ones. If in between these rows, If I get a row with all zeroes, I simply add the l[row] and r[row] as zero and keep my loop moving of reading the input.

The link to question on codeforces - https://codeforces.com/contest/812/problem/B

@Robin_rst Please look this one!

Bro , your code is too complex.
You can may now correct your code because it is giving wrong answer fot this test case:
4 4
010000
001110
000010
000000

Output should be 14 , but your code is giving 13

Maine dekha test cases, but meko conceptual mistake lagri h. Can u dry run or just tell me an overview of how to handle strings which are complete zero.

Preprocess the MATRIX to find out the index of the last bulb in each floor while entering from left and while entering from right . Then at each floor calculate the total disance to the last bulb while entering floor from left and while entering from right recursively and return the min distance.

Consider the following example -:

00000
00110
00010

This means there is 1 bulb in the ground floor that is turned on. There are 2 bulbs in the second floor that is turned on and 0 bulbs in third floor that is turned on.
Initially Sagheer always enters from left in the ground floor. Intuitvely we can see that the best path Sagheer can take is

00000
001<-1<-0
0->0->0->1->0^(upar)

The minimum distance covered by Sagheeris then 7 . Of course there are other paths that Sagheer can take but all those will result in distance greater than 7.
Before attempting to solve, preprocess the floors so that we have the index of the last bulb in each floor while entering the floor from left and while entering the floor from right .

Then recursively find the min distance at each floor. One edge case to consider is that you only need to go a floor if there is atleast one light turned on on that floor. So we can keep track of final floor where bulb is lit as base case in the recursive function.

Pseudo Code -: https://ide.codingblocks.com/s/159343

You can take help from there.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.