#include <iostream>
#include <fstream>
#include <algorithm>
usingnamespace 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???
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 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.
Please help determine the minimum number of '.' squares that should be occupied by cows to form a successful bucket brigade.
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.
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.