SPOJ BYTESM2

Repeatedly Getting Runtime Error.
Only Possible is ArrayIndexOutofBoundException.
Cant see where the bound will exceed.

Brother, we will solve this problem with dynamic approach meaning that we will get the solution for current cell (X,Y) with the help of (X-1,Y) (X-1,Y-1),(X-1,Y+1). We will call the given array A. We will also keep another array B where B[i][j] will show that maximum number of stones we can collect . Initially every element of the first row of the array B equals to every element of the first row of array A, the others have a value of 0. Now we need to loop over array B and for every coordinate (X,Y) we need to see if we can update B[X+1][Y] , B[X+1][Y-1] or B[X+1][Y+1]. Then we need to find the maximum of the last row of the array B and print it out.
My implementation.
Now compare with your Java solution:-
Java7 : kkpViB - Online IDE & Debugging Tool - Ideone.com
Java : 5UPUcc - Online Java Compiler & Debugging Tool - Ideone.com

2 Likes

@abeyaar: Your code is 100% correct. You are getting Runtime Error because of InputStreamReader. If you use Scanner it will get TLE. If you use DataInputStream you will get AC. Hope it works now.

1 Like

@topcoder_7 I have also solved the problem in the same way. The difference lies in the indices we both have used.Also i have put in checks to make sure that the bound doesn’t exceed anywhere. If you can give me a test case where the bound exceeds, that would be great . :slight_smile:

@michelangelo thanks a lot…btw I never understand how can one get runtime error or something else based on how the input is taken? Provided the input format is correct and no array bound or data type problem is there in the code

SPOJ has I/O issues in older problems. When I used getchar_unlocked (fast I/O for C++, similar to InputStreamReader in Java) in those problems most of the times I got WA/RE for correct codes. So, I started using getchar which till now hasn’t given any problems. If you find that DataInputStream is accepted in those problems, you should stick to it. And remember that these fast I/O methods are needed only when the input file is very large. Otherwise using Scanner is best. Note that these methods are almost never required in CC or CF.

1 Like