Language: C++

#include <bits/stdc++.h>
#define r(i,n) for(int i=0;i<n;i++)
#define int short int
using namespace std;

struct P{
  int ay,ax,by,bx,cy,cx,cost;
  P(int a1,int a2,int b1,int b2,int c1,int c2,int c){
    ax=a1;ay=a2;
    bx=b1;by=b2;
    cx=c1;cy=c2;
    cost=c;
  }
};

int h,w,n,gx[3],gy[3],sx[3],sy[3];
string s[14],iranai;
bool used[14][14][14][14][14][14];
int dx[]={1,0,-1,0,0};
int dy[]={0,1,0,-1,0};

int BFS(){
  memset(used,0,sizeof(used));
  queue<P>q;
  q.push(P(sx[0],sy[0],sx[1],sy[1],sx[2],sy[2],0));
  used[sy[0]][sx[0]][sy[1]][sx[1]][sy[2]][sx[2]]=1;
  while(!q.empty()){
    P p=q.front();q.pop();
    int ay=p.ay;
    int ax=p.ax;
    int by=p.by;
    int bx=p.bx;
    int cy=p.cy;
    int cx=p.cx;
    int cost=p.cost;
    if(ay==gy[0]&&ax==gx[0]&&by==gy[1]&&bx==gx[1]&&cy==gy[2]&&cx==gx[2])return cost;
    for(int i=0;i<5;i++){
      int Ay=ay+dy[i];
      int Ax=ax+dx[i];
      if(Ay<0||Ax<0||Ay>=h||Ax>=w)continue;
      if(s[Ay][Ax]=='#')continue;
      if(n>=2){

        for(int j=0;j<5;j++){
          int By=by+dy[j];
          int Bx=bx+dx[j];
          if(By<0||Bx<0||By>=h||Bx>=w)continue;
          if(s[By][Bx]=='#')continue;
          if(n>=3){

            for(int j=0;j<5;j++){
              int Cy=cy+dy[j];
              int Cx=cx+dx[j];
              if(Cy<0||Cx<0||Cy>=h||Cx>=w)continue;
              if(s[Cy][Cx]=='#')continue;
              if(Ax==bx&&Ay==by&&Bx==ax&&By==ay)continue;
              if(Cx==bx&&Cy==by&&Bx==cx&&By==cy)continue;
              if(Ax==cx&&Ay==cy&&Cx==ax&&Cy==ay)continue;
              if(Ax==Bx&&Ay==By)continue;
              if(Ax==Cx&&Ay==Cy)continue;
              if(Cx==Bx&&Cy==By)continue;
              if(!used[Ay][Ax][By][Bx][Cy][Cx]){
                used[Ay][Ax][By][Bx][Cy][Cx]=1;
                q.push(P(Ax,Ay,Bx,By,Cx,Cy,cost+1));
              }
            }

          }
          else{
            if(Ax==bx&&Ay==by&&Bx==ax&&By==ay)continue;
            if(Ax==Bx&&Ay==By)continue;
            if(!used[Ay][Ax][By][Bx][cy][cx]){
              used[Ay][Ax][By][Bx][cy][cx]=1;
              q.push(P(Ax,Ay,Bx,By,cx,cy,cost+1));
            }
          }
        }

      }
      else{
        if(!used[Ay][Ax][by][bx][cy][cx]){
          used[Ay][Ax][by][bx][cy][cx]=1;
          q.push(P(Ax,Ay,bx,by,cx,cy,cost+1));
        }
      }
    }
  }
  return -1;
}

int main(){
  while(cin>>w>>h>>n,n){
    memset(sy,0,sizeof(sy));
    memset(sx,0,sizeof(sx));
    memset(gx,0,sizeof(gx));
    memset(gy,0,sizeof(gy));
    getline(cin,s[0]);
    getline(cin,s[0]);
    r(i,h-2)getline(cin,s[i]);
    getline(cin,iranai);
    h-=2;w-=2;
    r(i,h)s[i].erase(s[i].begin(),s[i].begin()+1);
    r(i,h)s[i].erase(s[i].begin()+w);
    r(i,h)r(j,w){
      if(s[i][j]=='a')sy[0]=i,sx[0]=j;
      if(s[i][j]=='b')sy[1]=i,sx[1]=j;
      if(s[i][j]=='c')sy[2]=i,sx[2]=j;
      if(s[i][j]=='A')gy[0]=i,gx[0]=j;
      if(s[i][j]=='B')gy[1]=i,gx[1]=j;
      if(s[i][j]=='C')gy[2]=i,gx[2]=j;
    }
    cout<<BFS()<<endl;
  }
}


Compiler output:
a.cc:3:19: error: '::main' must return 'int'
    3 | #define int short int
      |                   ^~~
a.cc:91:1: note: in expansion of macro 'int'
   91 | int main(){
      | ^~~