Language: C++

#pragma once

#include <stdio.h>
#include <string>
#include <iostream>
#include <queue>
using namespace std;
string S[1001];
long result=0;
int H,W,N;
long mintime[1000][1000];
queue<int> qx,qy,time;
int sx,sy,gx,gy;
int step = 0;

int main(){
	scanf("%d%d%d",&H,&W,&N);

	for(int i=0;i<H;i++){
		cin>>S[i];
		for(int j=0;j<W;j++){
			mintime[i][j]=1000000;
			if(S[i][j]=='S'){
				sx=j;
				sy=i;
				qx.push(j);
				qy.push(i);
				time.push(0);
			}

			if(S[i][j]-'0'==N){
				gx=j;
				gy=i;
			}

		}
	}



for(int acc=0;acc<N;acc++){
	step++;
	while(step<=N){
		
		if(qx.front()>0){
		if(time.front()<mintime[qy.front()][qx.front()-1]-1  && S[qy.front()][qx.front()-1] !='X'){
			if(S[qy.front()][qx.front()-1]-'0' ==step){
			result += time.front()+1;
			sx=qx.front()-1;
			sy=qy.front();
			while(qx.empty()== false){
				qx.pop();
				qy.pop();
				time.pop();
			}
			
			qx.push(sx);
			qy.push(sy);
			time.push(0);
			break;
		}
				qx.push(qx.front()-1);
				qy.push(qy.front());
				time.push(time.front()+1);
				mintime[qy.front()][qx.front()-1]=time.front()+1;
		}

	}
		//

	if( qx.front()<W-1 ){
		if(time.front()<mintime[qy.front()][qx.front()+1]-1 && S[qy.front()][qx.front()+1] !='X'){

		if(S[qy.front()][qx.front()+1]-'0' ==step){
			result += time.front()+1;
			sx=qx.front()+1;
			sy=qy.front();
			while(qx.empty()== false){
				qx.pop();
				qy.pop();
				time.pop();
			}
			
			qx.push(sx);
			qy.push(sy);
			time.push(0);
			break;
		}

				qx.push(qx.front()+1);
				qy.push(qy.front());
				time.push(time.front()+1);
				mintime[qy.front()][qx.front()+1]=time.front()+1;
		}
	}

		//

	if( qy.front()>0){
		if(time.front()<mintime[qy.front()-1][qx.front()]-1  && S[qy.front()-1][qx.front()] !='X'){

			if(S[qy.front()-1][qx.front()]-'0' ==step){
			result += time.front()+1;
			sx=qx.front();
			sy=qy.front()-1;
			while(qx.empty()== false){
				qx.pop();
				qy.pop();
				time.pop();
			}
			
			qx.push(sx);
			qy.push(sy);
			time.push(0);
			break;
		}

				qx.push(qx.front());
				qy.push(qy.front()-1);
				time.push(time.front()+1);
				mintime[qy.front()-1][qx.front()]=time.front()+1;
		}
	}

		//

	if(qy.front()<H-1){
		if(time.front()<mintime[qy.front()+1][qx.front()]-1  && S[qy.front()+1][qx.front()] !='X'){

				if(S[qy.front()+1][qx.front()]-'0' ==step){
			result += time.front()+1;
			sx=qx.front();
			sy=qy.front()+1;
			while(qx.empty()== false){
				qx.pop();
				qy.pop();
				time.pop();
			}
			
			qx.push(sx);
			qy.push(sy);
			time.push(0);
			break;
		}

				qx.push(qx.front());
				qy.push(qy.front()+1);
				time.push(time.front()+1);
				mintime[qy.front()+1][qx.front()]=time.front()+1;
		}
	}


		qx.pop();
		qy.pop();
		time.pop();
	



	}

	for(int i=0;i<H;i++){
		for(int j=0;j<W;j++){
			mintime[i][j]=1000000;
		}
	}

}

printf("%d\n",result);
return 0;

}

Compiler output:
a.cc:1:9: warning: #pragma once in main file
    1 | #pragma once
      |         ^~~~
a.cc:12:18: error: 'std::queue<int> time' redeclared as different kind of entity
   12 | queue<int> qx,qy,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:5:
/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 'int main()':
a.cc:28:38: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   28 |                                 time.push(0);
      |                                      ^~~~
a.cc:46:25: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   46 |                 if(time.front()<mintime[qy.front()][qx.front()-1]-1  && S[qy.front()][qx.front()-1] !='X'){
      |                         ^~~~~
a.cc:48:40: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   48 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:54:38: error: request for member 'pop' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   54 |                                 time.pop();
      |                                      ^~~
a.cc:59:30: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   59 |                         time.push(0);
      |                              ^~~~
a.cc:64:38: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   64 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:64:48: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   64 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:65:72: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   65 |                                 mintime[qy.front()][qx.front()-1]=time.front()+1;
      |                                                                        ^~~~~
a.cc:72:25: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   72 |                 if(time.front()<mintime[qy.front()][qx.front()+1]-1 && S[qy.front()][qx.front()+1] !='X'){
      |                         ^~~~~
a.cc:75:40: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   75 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:81:38: error: request for member 'pop' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   81 |                                 time.pop();
      |                                      ^~~
a.cc:86:30: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   86 |                         time.push(0);
      |                              ^~~~
a.cc:92:38: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   92 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:92:48: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   92 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:93:72: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
   93 |                                 mintime[qy.front()][qx.front()+1]=time.front()+1;
      |                                                                        ^~~~~
a.cc:100:25: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  100 |                 if(time.front()<mintime[qy.front()-1][qx.front()]-1  && S[qy.front()-1][qx.front()] !='X'){
      |                         ^~~~~
a.cc:103:40: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  103 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:109:38: error: request for member 'pop' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  109 |                                 time.pop();
      |                                      ^~~
a.cc:114:30: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  114 |                         time.push(0);
      |                              ^~~~
a.cc:120:38: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  120 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:120:48: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  120 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:121:72: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  121 |                                 mintime[qy.front()-1][qx.front()]=time.front()+1;
      |                                                                        ^~~~~
a.cc:128:25: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  128 |                 if(time.front()<mintime[qy.front()+1][qx.front()]-1  && S[qy.front()+1][qx.front()] !='X'){
      |                         ^~~~~
a.cc:131:40: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  131 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:137:38: error: request for member 'pop' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  137 |                                 time.pop();
      |                                      ^~~
a.cc:142:30: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  142 |                         time.push(0);
      |                              ^~~~
a.cc:148:38: error: request for member 'push' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  148 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:148:48: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  148 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:149:72: error: request for member 'front' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  149 |                                 mintime[qy.front()+1][qx.front()]=time.front()+1;
      |                                                                        ^~~~~
a.cc:156:22: error: request for member 'pop' in 'time', which is of non-class type 'time_t(time_t*) noexcept' {aka 'long int(long int*) noexcept'}
  156 |                 time.pop();
      |                      ^~~