Bucket Brigade help

Problem Info

Bucket Brigade, https://usaco.guide/general/expected#problem-usaco-939

My Work

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  ifstream fin("buckets.in");
  ofstream fout("buckets.out");

  int b[2], r[2], l[2];

  for (int i = 0; i < 10; ++i) {
    for (int j = 0; j < 10; ++j) {
      char c;
      fin >> c;
      if (c == 'B') {
        *b = i;
        *(b + 1) = j;
      } else if (c == 'R') {
        *r = i;
        *(r + 1) = j;
      } else if (c == 'L') {
        *l = i;
        *(l + 1) = j;
      }
    }
  }

  if ((((abs(*b - *l) == 1) && (*(b + 1) == *(l + 1))) || ((*b == *l) && (abs(*(b + 1) - *(l + 1)) == 1))) || ((*b == *r && *r == *l) && (*(l + 1) < *(r + 1) < *(b + 1) || *(b + 1) < *(r + 1) < *(l + 1))) || ((*(b + 1) == *(r + 1) && *(r + 1) == *(l + 1)) && (*l < *r < *b || *b < *r < *l))) {
    fout << abs(*(l + 1) - *(b + 1)) + abs(*l - *b) + 1 << '\n';
  } else {
    fout << abs(*(l + 1) - *(b + 1)) + abs(*l - *b) - 1 << '\n';
  }

  return 0;
}

*b is vertical coordinate of B, *(b+1) is horizontal, with top left 0,0, and similar for R and L.
Long condition in words is:
if ( [(B,L vertically adjacent) or (B,L horizontally adjacent)] or [{(B,R,L have same ver coord) and (arranged as L…R…B or B…R…L)} or {(B,R,L have same hor coord) and (arranged as L…R…B or B…R…L but vertically)}] )

What I’ve Tried

I’ve tried many self-devised test cases, which all work (including special cases like B,L adjacent and R elsewhere, BRL adjacent etc.) but every time I run on USACO servers cases 6 and 7 are consistently wrong (rest pass), and so there’s probably a special case I’m missing that I’m failing to see. I’ll admit my logic in the last if condition is very unreadable, but having gone through it twice I can’t see an issue. (also apologies for the use of * everywhere, I learnt about pointers and wanted to test it, but ended up copy pasting similar parts and now it’s everywhere)

Question

Can anyone else spot said potentially missing special case, or any logic issue? Thank you in advance.

In C++, expressions like a < b < c don’t work as you would expect. It will be evaluated as (a < b) < c, where a < b first returns true or false, which is converted to 1 or 0 then compared with c, which is not what you want.

1 Like

Ah got it. Actually I initially did the same for the equals operators, and changed it, but it didn’t occur to me to repeat for the inequalities for some reason. Thanks a lot for your help!

Just for completeness, altered the condition to

(((abs(*b - *l) == 1) && (*(b + 1) == *(l + 1))) || ((*b == *l) && (abs(*(b + 1) - *(l + 1)) == 1))) || (((*b == *r) && (*r == *l)) && (((*(l + 1) < *(r + 1)) && (*(r + 1) < *(b + 1))) || ((*(b + 1) < *(r + 1)) && (*(r + 1) < *(l + 1))))) || (((*(b + 1) == *(r + 1)) && (*(r + 1) == *(l + 1))) && (((*l < *r) && (*r < *b)) || ((*b < *r) && (*r < *l))))

and all cases pass