Language: C++

#include <cstdio>
#include <iostream>
using namespace std;
int ans = 0;
int gh=0, gw=0;
const int max = 500000;
char c[1000][1000];
int d[1000][1000];
int mh[4] = { 1,0,-1,0 };
int mw[4] = { 0,1,0,-1 };
char hijou[9] = { '1','2','3','4','5','6','7','8','9' };
int  judge(int h, int w,int g) {
	if (c[h][w] ==hijou[g])
	{ 	gh = h;
	gw = w; 
	return d[h][w];
	}
	for (int i = 0; i < 4; i++) {
		int nh = h + mh[i];
		int nw = w + mh[i];
		if (nh >= 0 && nh < 1000 && nw>=0 && nh < 1000 && d[nh][nw] == max&&c[nh][nw] != 'X') {
			d[nh][nw] = d[h][w] + 1;
			judge(nh, nw,g);
		}
	}
}
int main() {
	int h, w, n,sh,sw;
	for (int i = 0; i < 1000;i++){
		for (int j = 0; j < 1000; j++)
		{
			c[i][j] = 'X'; 
		}
	}
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w;j++){
			cin >> c[i][j];
			if (c[i][j] == 'S') { sh = i; sw = j; }
		}
	}cout << sh << sw;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 1000; j++) {
			for (int k = 0; k < 1000; k++)
				d[j][k] = max;
		}
	
		d[sh][sw] = 0;
		ans+=judge(sh, sw, i);
		sh = gh; sw = gw;
}
	cout << ans << endl;
	
}

Compiler output:
a.cc: In function 'int judge(int, int, int)':
a.cc:21:80: error: reference to 'max' is ambiguous
   21 |                 if (nh >= 0 && nh < 1000 && nw>=0 && nh < 1000 && d[nh][nw] == max&&c[nh][nw] != 'X') {
      |                                                                                ^~~
In file included from /usr/include/c++/14/string:51,
                 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:2:
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:                 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
a.cc:6:11: note:                 'const int max'
    6 | const int max = 500000;
      |           ^~~
a.cc: In function 'int main()':
a.cc:45:43: error: reference to 'max' is ambiguous
   45 |                                 d[j][k] = max;
      |                                           ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidates are: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:                 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
a.cc:6:11: note:                 'const int max'
    6 | const int max = 500000;
      |           ^~~
a.cc: In function 'int judge(int, int, int)':
a.cc:26:1: warning: control reaches end of non-void function [-Wreturn-type]
   26 | }
      | ^