Language: C++

#include "bits/stdc++.h"

using namespace std;

#define int long long

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)

#define pb push_back
#define VDEL(v, n) v.erase(v.begin() + (n))
#define ALL(v) v.begin(), v.end()

#define INTMAX 2147483647
#define INF 200000000
#define TINT(str) atoi(str.c_str())
#define TSTR(num) to_string(num)

using ll = long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VII = vector<vector<int>>;

int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

PII goal[10];
char k[1000][1000];
int x0, y0;
int H,W;

int BFS(int x, int y)
{
    int cost[1000][1000];

    REP(i, 1000)
    {
        REP(h, 1000)
        {
            cost[i][h] = INF;
        }
    }

    cost[x0][y0] = 0;

    queue<PII> q;

    q.push(make_pair(x0, y0));

    while(!q.empty())
    {
        PII now = q.front();
        q.pop();
        int nx = now.first;
        int ny = now.second; 

        REP(i, 4)
        {
            if(now.first + dx[i] >= H ||
            now.first + dx[i] < 0) continue;
            if(now.second + dy[i] >= W || 
            now.second + dy[i] < 0) continue;

            if(nx + dx[i] == x &&
            ny + dy[i] == y)
            {
                return cost[nx][ny] + 1;
            }

            if(k[nx + dx[i]][ny + dy[i]] != 'X')
            {
                if(cost[nx + dx[i]][ny + dy[i]] > 
                cost[nx][ny])
                {
                    cost[nx + dx[i]][ny + dy[i]] =
                        cost[nx][ny] + 1;
                    q.push({nx + dx[i], ny + dy[i]});
                }
            }
            else continue;
        }
    }
}

signed main()
{
    int N;
    cin >> H >> W >> N;

    char str[1001];
    REP(h, H)
    {
        scanf("%s", str);
        REP(w, W)
        {
            k[h][w] = str[w];
            if(k[h][w] == 'S')
            {
                x0 = h;
                y0 = w;
            }
            if(k[h][w] != '.' &&
                k[h][w] != 'X')
            {
                goal[k[h][w] - '1'] = make_pair(h, w);
            }
        }
    }
    int ans = 0;

    REP(i, N)
    {
        ans += BFS(goal[i].first, goal[i].second);
        x0 = goal[i].first;
        y0 = goal[i].second;
    }

    cout << ans << endl;

    return 0;
}



Compiler output:
a.cc:29:9: error: 'long long int y0' redeclared as different kind of entity
   29 | int x0, y0;
      |         ^~
In file included from /usr/include/features.h:523,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/os_defines.h:39,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/c++config.h:683,
                 from /usr/include/c++/14/cassert:43,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:33,
                 from a.cc:1:
/usr/include/x86_64-linux-gnu/bits/mathcalls.h:256:1: note: previous declaration 'double y0(double)'
  256 | __MATHCALL (y0,, (_Mdouble_));
      | ^~~~~~~~~~
a.cc: In function 'long long int BFS(long long int, long long int)':
a.cc:44:13: error: invalid types 'long long int [1000][double(double) noexcept]' for array subscript
   44 |     cost[x0][y0] = 0;
      |             ^
a.cc:48:11: error: no matching function for call to 'std::queue<std::pair<long long int, long long int> >::push(std::pair<long long int, double (*)(double) noexcept>)'
   48 |     q.push(make_pair(x0, y0));
      |     ~~~~~~^~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/queue:66,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:157:
/usr/include/c++/14/bits/stl_queue.h:283:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(const value_type&) [with _Tp = std::pair<long long int, long long int>; _Sequence = std::deque<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >; value_type = std::pair<long long int, long long int>]'
  283 |       push(const value_type& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:283:30: note:   no known conversion for argument 1 from 'std::pair<long long int, double (*)(double) noexcept>' to 'const std::queue<std::pair<long long int, long long int> >::value_type&' {aka 'const std::pair<long long int, long long int>&'}
  283 |       push(const value_type& __x)
      |            ~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_queue.h:288:7: note: candidate: 'void std::queue<_Tp, _Sequence>::push(value_type&&) [with _Tp = std::pair<long long int, long long int>; _Sequence = std::deque<std::pair<long long int, long long int>, std::allocator<std::pair<long long int, long long int> > >; value_type = std::pair<long long int, long long int>]'
  288 |       push(value_type&& __x)
      |       ^~~~
/usr/include/c++/14/bits/stl_queue.h:288:25: note:   no known conversion for argument 1 from 'std::pair<long long int, double (*)(double) noexcept>' to 'std::queue<std::pair<long long int, long long int> >::value_type&&' {aka 'std::pair<long long int, long long int>&&'}
  288 |       push(value_type&& __x)
      |            ~~~~~~~~~~~~~^~~
a.cc: In function 'int main()':
a.cc:100:20: error: assignment of function 'double y0(double)'
  100 |                 y0 = w;
      |                 ~~~^~~
a.cc:115:12: error: assignment of function 'double y0(double)'
  115 |         y0 = goal[i].second;
      |         ~~~^~~~~~~~~~~~~~~~
a.cc: In function 'long long int BFS(long long int, long long int)':
a.cc:83:1: warning: control reaches end of non-void function [-Wreturn-type]
   83 | }
      | ^