Language: C++

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;

const int maxn = 1010;
typedef struct Point
{
    int x, y, t, n;
    Point(int _x, int _y, int _t)
    {
        x = _x, y =_y, t = _t;
    }
};

char a[maxn][maxn];
bool vis[maxn][maxn];
int W, H, N;
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int data[16][2];

int BFS(int sx, int sy, int ex, int ey)
{
    queue<Point> que;
    que.push(Point(sx, sy, 0));
    memset(vis, 0, sizeof(vis));
    vis[sx][sy] = 1;

    while(!que.empty())
    {
        Point p = que.front(); que.pop();
        //cout<<"p.t: "<<p.t<<endl;
        if(p.x == ex && p.y == ey) return p.t;

        for(int i = 0; i < 4; i++)
        {
            int nx = p.x + dx[i];
            int ny = p.y + dy[i];
            //cout<<nx<<"          "<<ny<<" "<<vis[nx][ny]<<endl;
            if(nx >= 0 && nx < H && ny >= 0 && ny < W && a[nx][ny] != 'X' && vis[nx][ny] == 0)
            {
                //cout<<"nx: "<<nx<<" ny: "<<ny<<" nn "<<nn<<" a[nx][ny] "<<a[nx][ny]<<endl;
                que.push(Point(nx, ny, p.t + 1));
                vis[nx][ny] = 1;
            }
        }
    }
  /*  for(int i = 0; i < H; i++)
    {
        for(int j = 0; j  < W; j++)
            cout<<vis[i][j];
        cout<<endl;
    }*/
    return 0;
}

int main()
{
    //FILE *f = fopen("data.txt","rb");
    scanf("%d%d%d", &H, &W, &N)
    
        int sx, sy, ans = 0;

        for(int i = 0; i < H; i++)
            scanf("%s",a[i]);

        for(int i = 0; i < H; i++)
            for(int j = 0; j  < W; j++)
                if(a[i][j] == 'S')  {data[0][0] = i, data[0][1] =j; a[i][j] = '.'; break;}
                else if( isdigit(a[i][j]) )
                {
                    int index = a[i][j] - '0';
                    data[index][0] = i;
                    data[index][1] = j;
                }
        for(int i = 0; i < N; i++)
        {
            ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
        }
        printf("%d\n",ans);
    
    return 0;
}

Compiler output:
a.cc:8:1: warning: 'typedef' was ignored in this declaration
    8 | typedef struct Point
      | ^~~~~~~
a.cc: In function 'int main()':
a.cc:61:32: error: expected ';' before 'int'
   61 |     scanf("%d%d%d", &H, &W, &N)
      |                                ^
      |                                ;
   62 | 
   63 |         int sx, sy, ans = 0;
      |         ~~~                     
a.cc:70:38: error: reference to 'data' is ambiguous
   70 |                 if(a[i][j] == 'S')  {data[0][0] = i, data[0][1] =j; a[i][j] = '.'; break;}
      |                                      ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:70:54: error: reference to 'data' is ambiguous
   70 |                 if(a[i][j] == 'S')  {data[0][0] = i, data[0][1] =j; a[i][j] = '.'; break;}
      |                                                      ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:74:21: error: reference to 'data' is ambiguous
   74 |                     data[index][0] = i;
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:75:21: error: reference to 'data' is ambiguous
   75 |                     data[index][1] = j;
      |                     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:79:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   79 |             ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
      |             ^~~
      |             abs
a.cc:79:24: error: reference to 'data' is ambiguous
   79 |             ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:79:36: error: reference to 'data' is ambiguous
   79 |             ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
      |                                    ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:79:49: error: reference to 'data' is ambiguous
   79 |             ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
      |                                                 ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [16][2]'
   21 | int data[16][2];
      |     ^~~~
a.cc:79:63: error: reference to 'data' is ambiguous
   79 |             ans += BFS(data[i][0], data[i][1],  data[i+1][0], data[i+1][1]);
      |                                                               ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Contain