Language: C++

#include <iostream>
#include <array>
#include <vector>
#include <cctype>
#include <unordered_set>
#include <queue>
#include <algorithm>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
#define repeat_reverse(i,n) for (int i = (n)-1; (i) >= 0; --(i))
#define repeat_from_reverse(i,m,n) for (int i = (n)-1; (i) >= (m); --(i))
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
typedef long long ll;
using namespace std;

struct point_t {
    int y, x;
};
point_t operator + (point_t const & a, point_t const & b) {
    return (point_t){ a.y + b.y, a.x + b.x };
}
bool operator == (point_t const & a, point_t const & b) {
    return make_pair(a.y, a.x) == make_pair(b.y, b.x);
}
bool operator < (point_t const & a, point_t const & b) {
    return make_pair(a.y, a.x) < make_pair(b.y, b.x);
}
static point_t dp[5] = { {-1,0}, {1,0}, {0,1}, {0,-1}, {0,0} };

struct state_t {
    array<point_t,3> a;
    int cost;
};
uint32_t encode(array<point_t,3> const & s) {
    uint32_t q = 0;
    for (point_t p : s) {
        q = (q * 18) + p.y;
        q = (q * 16) + p.x;
    }
    return q;
}
bool is_valid_move(int i, int j, array<point_t,3> const & s, array<point_t,3> const & t) {
    if (t[i] == t[j]) return false;
    if (t[i] == s[j] and t[j] == s[i]) return false;
    return true;
}

int main() {
    while (true) {
        int w, h, n; cin >> w >> h >> n; cin.ignore();
        if (w == 0 and h == 0 and n == 0) break;
        vector<string> c(h);
        repeat (i,h) getline(cin, c[i]);

        assert (1 <= n and n <= 3);
        array<point_t,3> start, goal;
        repeat (y,h) repeat (x,w) {
            if (islower(c[y][x])) {
                start[c[y][x] - 'a'] = { y, x };
            } else if (isupper(c[y][x])) {
                goal[c[y][x] - 'A'] = { y, x };
            }
        }
        if (n == 1 or n == 2) {
            assert (4 <= w);
            string w = c.back();
            c.push_back(w);
            c.back()[2] = '.';
            start[2] = goal[2] = (point_t){ h, 2 };
            if (n == 1) {
                c.back()[1] = '.';
                start[1] = goal[1] = (point_t){ h, 1 };
            }
            c.push_back(w);
            h += 2;
        }

        set<uint32_t> used;
        queue<state_t> que; {
            state_t initial = { start, 0 };
            used.insert(encode(start));
            que.push(initial);
        }
        while (not que.empty()) {
            state_t st = que.front(); que.pop();
            array<point_t,3> & s = st.a;
            if (s == goal) {
                cout << st.cost << endl;
                break;
            }
            state_t tt = st;
            tt.cost += 1;
            array<point_t,3> & t = tt.a;
            repeat (i,5) { // a
                t[0] = s[0] + dp[i];
                if (c[t[0].y][t[0].x] == '#') continue;
                repeat (j,5) { // b
                    t[1] = s[1] + dp[j];
                    if (c[t[1].y][t[1].x] == '#') continue;
                    if (not is_valid_move(0, 1, s, t)) continue;
                    repeat (k,5) { // c
                        t[2] = s[2] + dp[k];
                        if (c[t[2].y][t[2].x] == '#') continue;
                        if (not is_valid_move(1, 2, s, t)) continue;
                        if (not is_valid_move(2, 0, s, t)) continue;
                        if (used.count(encode(t))) continue;
                        used.insert(encode(t));
                        que.push(tt);
                    }
                }
            }
        }
    }
    return 0;
}

Compiler output:
a.cc:36:1: error: 'uint32_t' does not name a type
   36 | uint32_t encode(array<point_t,3> const & s) {
      | ^~~~~~~~
a.cc:9:1: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
    8 | #include <cassert>
  +++ |+#include <cstdint>
    9 | #define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
a.cc: In function 'int main()':
a.cc:80:9: error: 'set' was not declared in this scope
   80 |         set<uint32_t> used;
      |         ^~~
a.cc:9:1: note: 'std::set' is defined in header '<set>'; this is probably fixable by adding '#include <set>'
    8 | #include <cassert>
  +++ |+#include <set>
    9 | #define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
a.cc:80:13: error: 'uint32_t' was not declared in this scope
   80 |         set<uint32_t> used;
      |             ^~~~~~~~
a.cc:80:13: note: 'uint32_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:80:23: error: 'used' was not declared in this scope
   80 |         set<uint32_t> used;
      |                       ^~~~
a.cc:83:25: error: 'encode' was not declared in this scope
   83 |             used.insert(encode(start));
      |                         ^~~~~~
a.cc:108:40: error: 'encode' was not declared in this scope
  108 |                         if (used.count(encode(t))) continue;
      |                                        ^~~~~~
a.cc:109:37: error: 'encode' was not declared in this scope
  109 |                         used.insert(encode(t));
      |                                     ^~~~~~