#ifndef INCLUDED_MAIN #define INCLUDED_MAIN #include __FILE__ int main(void) { ll h,w,m; cin>>h>>w>>m; vector g(h,vc(w)); pair s; pair goal; rep(i,h) rep(j,w) { cin>>g[i][j]; if(g[i][j]=='S') s=make_pair(i,j); if(g[i][j]=='G') goal=make_pair(i,j); } struct node { int i; int j; int keyty; }; queue que; vector> dist(h,vector(w,1e9)); dist[s.first][s.second]=0; que.push({s.first,s.second,-1}); while(!que.empty()) { node nd=que.front(); que.pop(); rep(i,4) { int ni=nd.i+dx[i],nj=nd.j+dy[i]; int kty=nd.keyty; if(!isin(ni,nj,h,w)) continue; if(g[ni][nj]=='#') continue; if(g[ni][nj]>='a' && g[ni][nj]<='i' && g[ni][nj]!=char('a'+nd.keyty-1)) continue; if(dist[ni][nj]<=dist[nd.i][nd.j]) continue; dist[ni][nj]=dist[nd.i][nd.j]+1; if(g[ni][nj]=='G') { cout<='1' && g[ni][nj]<='9') kty=g[ni][nj]-'0'; que.push({ni,nj,kty}); } } cout<<-1< using namespace std; #define rep(i,n) for(ll i=0;i=l;i--) using ll = long long; using ld = long double; const ll mod=998244353; #define vout(v) for(auto i :v) cout< #define vc vector #define pb push_back vector dx={-1,1,0,0},dy={0,0,-1,1}; bool isin(ll i,ll j,ll h,ll w) { return (i>=0 && i=0 && j bool chmin(T& a, T b){if(a > b){a = b; return true;} return false;} template bool chmax(T& a, T b){if(a < b){a = b; return true;} return false;} void no() { cout<<"No"<