### Code Outputting Different output on two different graders

So I wrote a solution to a competitive programming problem as follows
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243`` ``````#include #include #include using namespace std; int main (void) { int by, bx; int ly, lx; int ry, rx; int x, y, z; char map [10][10]; ifstream fin ("buckets.in"); ofstream fout ("buckets.out"); for (int i=1; i<=10; i++) { for (int j=1; j<=10; j++){ fin>>map [i][j]; } } for (int i=1; i<=10; i++) { for (int j=1; j<=10; j++){ if (map[i][j]=='B') { by=i; bx=j; x=1; break; } } if (x==1) break; } for (int i=1; i<=10; i++) { for (int j=1; j<=10; j++){ if (map[i][j]=='L') { ly=i; lx=j; y=1; break; } } if (y==1) break; } fout<<(ly-by)+(lx-bx-1); }``````

For the test case that is given, the correct output is 7, which I receive when I run the code in my IDE, however, when I upload it to the online grader, I get an output of 52 for some reason. Why???
Last edited on
The index 10 is out of bounds for `map` because it starts with 0.

So either you change the loop to

`for (int i=0; i<10; i++) // Note: < instead of <= `

or you change the size of the array to

`char map [11][11]; // Note: 11 `
Hey, thanks for the reply! I have changed the array size to 11, but now, instead of outputting 52, I get -1. I'll post the problem for reference. The input I am dealing with is the sample test case below. Note that I have not taken into account the rock quite yet, but, in the case of the sample case, it should not matter.

A fire has broken out on the farm, and the cows are rushing to try and put it out!

The farm is described by a 10×10

grid of characters like this:

..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........

The character 'B' represents the barn, which has just caught on fire. The 'L' character represents a lake, and 'R' represents the location of a large rock.

The cows want to form a "bucket brigade" by placing themselves along a path between the lake and the barn so that they can pass buckets of water along the path to help extinguish the fire. A bucket can move between cows if they are immediately adjacent in the north, south, east, or west directions. The same is true for a cow next to the lake --- the cow can only extract a bucket of water from the lake if she is immediately adjacent to the lake. Similarly, a cow can only throw a bucket of water on the barn if she is immediately adjacent to the barn.

A cow cannot be placed on the square containing the large rock, and the barn and lake are guaranteed not to be immediately adjacent to each-other.

INPUT FORMAT (file buckets.in):
The input file contains 10 rows each with 10 characters, describing the layout of the farm.

OUTPUT FORMAT (file buckets.out):
Output a single integer giving the minimum number of cows needed to form a viable bucket brigade.

SAMPLE INPUT:

..........
..........
..........
..B.......
..........
.....R....
..........
..........
.....L....
..........

SAMPLE OUTPUT:

7

In this example, here is one possible solution, which involves the optimal number of cows (7):

..........
..........
..........
..B.......
..C.......
..CC.R....
...CCC....
.....C....
.....L....
..........
Last edited on
It is a geometric problem. With the coordinates of B and L you can calculate the slope. See:

https://en.wikipedia.org/wiki/Slope

With this formula you can calculate the position of the cow the next row. If the cow isn't exactly north/south you need to add 1.

If you hit a rock you need to calculate the cow on the row beyond. If the relative position is more west/east you place the next cow more west/east.
Manhattan distance.
 ```..B....... ..7....... ..65.R.... ...432.... .....1.... .....L....```

is no shorter than:
 ```..B....... ..7....... ..6..R.... ..5....... ..4321.... .....L....```

or:
 ```..B....... ..7....... ..6..R.... ..5....... ..4....... ..321L....```

The `fout << (ly-by)+(lx-bx-1);`
is in principle right, but only as long as the by<=ly and bx<=lx. Distances are always positive.

The rock poses only one special case to handle.
Hey guys,
I'm not so concerned about the logic of my code, but rather why it outputs something different on the automated grader as on the compiler. Is it because of uninitialized memory or variables or something?
The original post had undefined behavior that coder777 mentioned. I don't think increasing the size to 11 is the best solution here, but it looks like the logic is equivalent so my guess is that isn't the issue.

If your if-branches on lines 20-25 and 32-37 are never reached, then a bunch of your variables will never be properly initialized. This is also potentially undefined behavior. I suggest initializing your variables in lines 6-9 either with 0 or some really outrageous number that would make the existence of an issue easy to see (e.g. -999999).

But without seeing the up-to-date code, I'd only just be guessing as to other problems.
Last edited on
Topic archived. No new replies allowed.