Language: C++

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

typedef pair<int, int> Pair;
const int INF = 100000000;

int H, W, N;
const int MAX_H = 1000, MAX_W = 1000;
char plant[MAX_H][MAX_W];
int sx, sy;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
int d[MAX_H][MAX_W];
int time;

void bfs() {
    queue<Pair> que;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            d[i][j] = INF;
        }
    }
    char muscle = '1';
    que.push(Pair(sx,sy));
    d[sx][sy] = 0;
    while (que.size()) {
        Pair p  = que.front(); que.pop();
        if (int(muscle)-49 == N) break;
        for (int i = 0; i < 4; i++) {
            int nx = p.first + dx[i], ny = p.second + dy[i];
            if (0 <= nx && nx < H && 0 <= ny && ny < W && plant[nx][ny] != 'X' && d[nx][ny] == INF) {
                que.push(Pair(nx, ny));
                d[nx][ny] = d[p.first][p.second] + 1;
            }
            if (muscle == plant[nx][ny]) {
                while(que.size()) {que.pop();}
                que.push(Pair(nx, ny));
                muscle = muscle + 1;
                time += d[nx][ny];
                for (int i = 0; i < H; i++) {
                    for (int j = 0; j < W; j++) {
                        d[i][j] = INF;
                    }
                }
                d[nx][ny] = 0;
                break;
            }
        }
    }
}

// void solve() {
//     time = 0;
//     bfs();
//     cout << time << endl;
// }

// int main () {
//     cin >> H >> W >> N;
//     for (int i = 0; i < H; i++) {
//         for (int j = 0; j < W; j++) {
//             cin >> plant[i][j];
//             if(plant[i][j] == 'S') {sx = i; sy = j;}
//         }
//     }
//     solve();
// }

void solve() {
    time = 0;
    bfs();
    printf("%d",time);
}

int main () {
    scanf("%d%d%d",&H,&W,&N);
    for (int i = 0; i < H; i++) {
        scanf("%s", plant[i]);
        for (int j = 0; j < W; j++) {
            if(plant[i][j] == 'S') {sx = i; sy = j;}
        }
    }
    solve();
}


Compiler output:
a.cc:16:5: error: 'int time' redeclared as different kind of entity
   16 | int time;
      |     ^~~~
In file included from /usr/include/pthread.h:23,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr-default.h:35,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/gthr.h:157,
                 from /usr/include/c++/14/ext/atomicity.h:35,
                 from /usr/include/c++/14/bits/ios_base.h:39,
                 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/time.h:76:15: note: previous declaration 'time_t time(time_t*)'
   76 | extern time_t time (time_t *__timer) __THROW;
      |               ^~~~
a.cc: In function 'void bfs()':
a.cc:41:22: warning: pointer to a function used in arithmetic [-Wpointer-arith]
   41 |                 time += d[nx][ny];
      |                 ~~~~~^~~~~~~~~~~~
a.cc:41:22: error: assignment of read-only location 'time'
a.cc: In function 'void solve()':
a.cc:72:10: error: assignment of function 'time_t time(time_t*)'
   72 |     time = 0;
      |     ~~~~~^~~