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;
FILE *fp;

int main(){
	fp = fopen("answer.txt","w");
	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);
	fprintf(fp,"%d\n",result);
	fclose(fp);
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:30: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'}
   30 |                                 time.push(0);
      |                                      ^~~~
a.cc:48: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'}
   48 |                 if(time.front()<mintime[qy.front()][qx.front()-1]-1  && S[qy.front()][qx.front()-1] !='X'){
      |                         ^~~~~
a.cc:50: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'}
   50 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:56: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'}
   56 |                                 time.pop();
      |                                      ^~~
a.cc:61: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'}
   61 |                         time.push(0);
      |                              ^~~~
a.cc:66: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'}
   66 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:66: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'}
   66 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:67: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'}
   67 |                                 mintime[qy.front()][qx.front()-1]=time.front()+1;
      |                                                                        ^~~~~
a.cc:74: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'}
   74 |                 if(time.front()<mintime[qy.front()][qx.front()+1]-1 && S[qy.front()][qx.front()+1] !='X'){
      |                         ^~~~~
a.cc:77: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'}
   77 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:83: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'}
   83 |                                 time.pop();
      |                                      ^~~
a.cc:88: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'}
   88 |                         time.push(0);
      |                              ^~~~
a.cc:94: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'}
   94 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:94: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'}
   94 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:95: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'}
   95 |                                 mintime[qy.front()][qx.front()+1]=time.front()+1;
      |                                                                        ^~~~~
a.cc:102: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'}
  102 |                 if(time.front()<mintime[qy.front()-1][qx.front()]-1  && S[qy.front()-1][qx.front()] !='X'){
      |                         ^~~~~
a.cc:105: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'}
  105 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:111: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'}
  111 |                                 time.pop();
      |                                      ^~~
a.cc:116: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'}
  116 |                         time.push(0);
      |                              ^~~~
a.cc:122: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'}
  122 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:122: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'}
  122 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:123: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'}
  123 |                                 mintime[qy.front()-1][qx.front()]=time.front()+1;
      |                                                                        ^~~~~
a.cc:130: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'}
  130 |                 if(time.front()<mintime[qy.front()+1][qx.front()]-1  && S[qy.front()+1][qx.front()] !='X'){
      |                         ^~~~~
a.cc:133: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'}
  133 |                         result += time.front()+1;
      |                                        ^~~~~
a.cc:139: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'}
  139 |                                 time.pop();
      |                                      ^~~
a.cc:144: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'}
  144 |                         time.push(0);
      |                              ^~~~
a.cc:150: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'}
  150 |                                 time.push(time.front()+1);
      |                                      ^~~~
a.cc:150: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'}
  150 |                                 time.push(time.front()+1);
      |                                                ^~~~~
a.cc:151: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'}
  151 |                                 mintime[qy.front()+1][qx.front()]=time.front()+1;
      |                                                                        ^~~~~
a.cc:158: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'}
  158 |                 time.pop();
      |                      ^~~