views:

308

answers:

2

Hello,

Im working on a code challenge problem involving transforming a given binary matrix into another. I wrote the code in C++, and it works for the test cases i looked at, but it does not pass the online judge . The problem as well as my solution is at http://bit.ly/4SEoZ . Can someone please tell me what could be going wrong ?

Thank You.

+2  A: 

Forgive me if this sounds harsh, but the basic problem is that your code is much too long, complicated and interdependent. I can see what appear to be bugs (such as in sortSolution), but it's hard to tell because the meaning of the variables and the intent of the code is so unclear. You use global variables where they really aren't needed, local variables with the same names (which I think the compiler should forbid), and vast arrays which you do not initialize. You use ints where bools would do, you seem to consider toggling a boolean many times, you then seem to assume that these ints will be 1 or 0 in a way that just makes me cringe. You duplicate large sections of code and sometimes have two variables (or fields of an array) doing one job.

It wouldn't surprise me if the online judge found a test case that trips up this solver.

Beta
A: 

You need to learn about scope I think. If you don't want to have problems with a variable, you should try to make its scope as small as possible.

Also, you need more explicit names / commented code.

That being said, I am looking at the 'toggle' word, I think you are misinterpreting this keyword with 'flip', for me toggle means passing everything to '1' regardless of their previous tests. Of course it means that actually going 'backwards' ie obtaining a final grid with a element being 0 where it was 1 in the initial grid is impossible.

Matthieu M.