This documentation is automatically generated by online-judge-tools/verification-helper
#define PROBLEM "https://yukicoder.me/problems/no/3199"
#include "../../ds/Deque.hpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
const int INF = 2100000000;
template<class T> bool chmin(T &a, T b){return (a > b ? a = b, 1 : 0);}
void solve();
// POP'N ROLL MUSIC / TOMOO
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
rep(i, 0, t) solve();
}
void solve(){
int H, W, M;
cin >> H >> W >> M;
vector<string> S(H);
rep(i, 0, H) cin >> S[i];
int sx, sy, gx, gy;
rep(i, 0, H) rep(j, 0, W){
if (S[i][j] == 'S') sx = i, sy = j;
if (S[i][j] == 'G') gx = i, gy = j;
}
vector dp(M + 1, vector(H, vector<int>(W, INF)));
po167::Deque<tuple<int, int, int>> q;
q.push_back({0, sx, sy});
dp[0][sx][sy] = 0;
vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
while (!q.empty()){
auto [c, x, y] = q.front();
q.pop_front();
if ('1' <= S[x][y] && S[x][y] <= '9' && S[x][y] != (char)('0' + c)){
int d = S[x][y] - '0';
if (dp[d][x][y] == INF){
dp[d][x][y] = dp[c][x][y];
q.push_front({d, x, y});
}
continue;
}
if ('a' <= S[x][y] && S[x][y] <= 'z' && S[x][y] != (char)('a' + c - 1)) continue;
rep(k, 0, 4){
int s = dx[k] + x;
int t = dy[k] + y;
if (s < 0 || H <= s || t < 0 || W <= t) continue;
if (S[s][t] == '#') continue;
if (chmin(dp[c][s][t], dp[c][x][y] + 1)){
q.push_back({c, s, t});
continue;
}
}
}
int ans = INF;
rep(i, 0, M + 1) chmin(ans, dp[i][gx][gy]);
if (ans == INF) ans = -1;
cout << ans << "\n";
}
#line 1 "test/ds/deque.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/3199"
#line 2 "ds/Deque.hpp"
#include <vector>
#include <cassert>
namespace po167{
// GPT 作
template<typename T>
struct Deque {
Deque()
: _cap(8), _size(0), _head(0)
{
_data = static_cast<T*>(operator new[](sizeof(T) * _cap));
}
~Deque() {
clear();
operator delete[](_data);
}
// 要素数
int size() const { return _size; }
bool empty() const { return _size == 0; }
// 前後に要素を追加
void push_back(const T& v) { emplace_back(v); }
void push_back(T&& v) { emplace_back(std::move(v)); }
void push_front(const T& v) { emplace_front(v); }
void push_front(T&& v) { emplace_front(std::move(v)); }
// 前後から取り出し
void pop_back() {
assert(!empty());
int idx = (_head + _size - 1) & (_cap - 1);
_data[idx].~T();
--_size;
}
void pop_front() {
assert(!empty());
_data[_head].~T();
_head = ( _head + 1 ) & (_cap - 1);
--_size;
}
// 前後の参照
T& front() {
assert(!empty());
return _data[_head];
}
T& back() {
assert(!empty());
int idx = (_head + _size - 1) & (_cap - 1);
return _data[idx];
}
// 添字アクセス (0 ≤ i < size())
T& operator[](int i) {
assert(i >= 0 && i < _size);
return _data[( _head + i ) & (_cap - 1)];
}
const T& operator[](int i) const {
assert(i >= 0 && i < _size);
return _data[( _head + i ) & (_cap - 1)];
}
// 全要素をクリア
void clear() {
for(int i = 0; i < _size; i++){
int idx = (_head + i) & (_cap - 1);
_data[idx].~T();
}
_size = 0;
_head = 0;
}
private:
T* _data;
int _cap; // 必ず2のべき乗
int _size;
int _head; // 先頭要素のインデックス
// 空きがなければ拡張
void ensure_capacity() {
if (_size < _cap) return;
int new_cap = _cap << 1;
T* new_data = static_cast<T*>(operator new[](sizeof(T) * new_cap));
// 要素を head から順にコピー
for(int i = 0; i < _size; i++){
new (&new_data[i]) T(std::move((*this)[i]));
(*this)[i].~T();
}
operator delete[](_data);
_data = new_data;
_cap = new_cap;
_head = 0;
}
// 前後に emplace
template<typename... Args>
void emplace_back(Args&&... args) {
ensure_capacity();
int idx = (_head + _size) & (_cap - 1);
new(&_data[idx]) T(std::forward<Args>(args)...);
++_size;
}
template<typename... Args>
void emplace_front(Args&&... args) {
ensure_capacity();
_head = (_head - 1) & (_cap - 1);
new(&_data[_head]) T(std::forward<Args>(args)...);
++_size;
}
};
}
#line 5 "test/ds/deque.test.cpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
const int INF = 2100000000;
template<class T> bool chmin(T &a, T b){return (a > b ? a = b, 1 : 0);}
void solve();
// POP'N ROLL MUSIC / TOMOO
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
rep(i, 0, t) solve();
}
void solve(){
int H, W, M;
cin >> H >> W >> M;
vector<string> S(H);
rep(i, 0, H) cin >> S[i];
int sx, sy, gx, gy;
rep(i, 0, H) rep(j, 0, W){
if (S[i][j] == 'S') sx = i, sy = j;
if (S[i][j] == 'G') gx = i, gy = j;
}
vector dp(M + 1, vector(H, vector<int>(W, INF)));
po167::Deque<tuple<int, int, int>> q;
q.push_back({0, sx, sy});
dp[0][sx][sy] = 0;
vector<int> dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
while (!q.empty()){
auto [c, x, y] = q.front();
q.pop_front();
if ('1' <= S[x][y] && S[x][y] <= '9' && S[x][y] != (char)('0' + c)){
int d = S[x][y] - '0';
if (dp[d][x][y] == INF){
dp[d][x][y] = dp[c][x][y];
q.push_front({d, x, y});
}
continue;
}
if ('a' <= S[x][y] && S[x][y] <= 'z' && S[x][y] != (char)('a' + c - 1)) continue;
rep(k, 0, 4){
int s = dx[k] + x;
int t = dy[k] + y;
if (s < 0 || H <= s || t < 0 || W <= t) continue;
if (S[s][t] == '#') continue;
if (chmin(dp[c][s][t], dp[c][x][y] + 1)){
q.push_back({c, s, t});
continue;
}
}
}
int ans = INF;
rep(i, 0, M + 1) chmin(ans, dp[i][gx][gy]);
if (ans == INF) ans = -1;
cout << ans << "\n";
}