結果

問題 No.3199 Key-Door Grid
ユーザー amentorimaru
提出日時 2025-07-11 22:16:15
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 151 ms / 3,000 ms
コード長 4,051 bytes
コンパイル時間 5,011 ms
コンパイル使用メモリ 271,216 KB
実行使用メモリ 25,472 KB
最終ジャッジ日時 2025-07-11 22:16:24
合計ジャッジ時間 7,554 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 37
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:141:24: warning: ‘gx’ may be used uninitialized [-Wmaybe-uninitialized]
  141 |     chmin(ans, dp[i][gx][gy]);
      |                        ^
main.cpp:100:14: note: ‘gx’ was declared here
  100 |   ll sx, sy, gx, gy;
      |              ^~
main.cpp:141:28: warning: ‘gy’ may be used uninitialized [-Wmaybe-uninitialized]
  141 |     chmin(ans, dp[i][gx][gy]);
      |                            ^
main.cpp:100:18: note: ‘gy’ was declared here
  100 |   ll sx, sy, gx, gy;
      |                  ^~
In file included from /usr/include/c++/13/bits/uses_allocator_args.h:38,
                 from /usr/include/c++/13/bits/memory_resource.h:41,
                 from /usr/include/c++/13/string:58,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/ostream:40,
                 from /usr/include/c++/13/iostream:41,
                 from main.cpp:4:
In constructor ‘constexpr std::_Head_base<_Idx, _Head, false>::_Head_base(_UHead&&) [with _UHead = long long int&; long unsigned int _Idx = 1; _Head = long long int]’,
    inlined from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = long long int&; _UTail = {long long int&}; <template-parameter-2-3> = void; long unsigned int _Idx = 1; _Head = long long int; _Tail = {long long int}]’ at /usr/include/c++/13/tuple:293:38,
    inlined from ‘constexpr std::_Tuple_impl<_Idx, _Head, _Tail ...>::_Tuple_impl(_UHead&&, _UTail&& ...) [with _UHead = int; _UTail = {long long int&, long long int&}; <template-parameter-2-3> = void; long unsigned int _Idx = 0; _Head = long long int; _Tail = {long long int, long long int}]’ at /usr/include/c++/13/tuple:293:38,
    inlined from ‘constexpr std::tuple< <template-parameter-1-1> >::tuple(_UElements&& .

ソースコード

diff #

#define ATCODER
#include <bit>
#include <cstdint>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
#include <math.h>
#include <climits>
#include <set>
#include <map>
#include <list>
#include <iterator>
#include <bitset>
#include <chrono>
#include <type_traits>
using namespace std;

using ll = long long;

#define FOR(i, a, b) for(ll i=(a); i<(b);i++)
#define REP(i, n) for(ll i=0; i<(n);i++)
#define ROF(i, a, b) for(ll i=(b-1); i>=(a);i--)
#define PER(i, n) for(ll i=n-1; i>=0;i--)
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VP vector< pair<ll,ll> >
#define VVP vector<vector<pair<ll,ll>>>
#define all(i) begin(i),end(i)
#define SORT(i) sort(all(i))
#define EXISTBIT(x,i) (((x>>i) & 1) != 0)
#define MP(a,b) make_pair(a,b)
#ifdef ATCODER
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using mint2 = modint998244353;
#endif
template<typename T = ll>
vector<T> read(size_t n) {
  vector<T> ts(n);
  for (size_t i = 0; i < n; i++) cin >> ts[i];
  return ts;
}

template<typename TV, const ll N> void read_tuple_impl(TV&) {}
template<typename TV, const ll N, typename Head, typename... Tail>
void read_tuple_impl(TV& ts) {
  get<N>(ts).emplace_back(*(istream_iterator<Head>(cin)));
  read_tuple_impl<TV, N + 1, Tail...>(ts);
}
template<typename... Ts> decltype(auto) read_tuple(size_t n) {
  tuple<vector<Ts>...> ts;
  for (size_t i = 0; i < n; i++) read_tuple_impl<decltype(ts), 0, Ts...>(ts);
  return ts;
}

template<typename T> T det2(array<T, 4> ar) { return ar[0] * ar[3] - ar[1] * ar[2]; }
template<typename T> T det3(array<T, 9> ar) { return ar[0] * ar[4] * ar[8] + ar[1] * ar[5] * ar[6] + ar[2] * ar[3] * ar[7] - ar[0] * ar[5] * ar[7] - ar[1] * ar[3] * ar[8] - ar[2] * ar[4] * ar[6]; }
template<typename T> bool chmax(T& tar, T src) { return tar < src ? tar = src, true : false; }
template<typename T> bool chmin(T& tar, T src) { return tar > src ? tar = src, true : false; }
template<typename T> void inc(vector<T>& ar) { for (auto& v : ar) v++; }
template<typename T> void dec(vector<T>& ar) { for (auto& v : ar) v--; }
template<typename T> vector<pair<T, int>> id_sort(vector<T>& a) {
  vector<T, int> res(a.size());
  for (int i = 0; i < a.size(); i++)res[i] = MP(a[i], i);
  SORT(res);
  return res;
}

using val = ll; using func = ll;

val op(val a, val b) { return max(a, b); }
val e() { return -1e18; }
//val mp(func f, val a) { return MP(a.first + f * a.second, a.second); }
//func comp(func f, func g) { return f + g; }
//func id() { return 0; }


// Rook
ll dxr[4] = { 1,0,-1,0 };
ll dyr[4] = { 0,1,0,-1 };
// Bishop
ll dxb[4] = { -1,-1,1,1 };
ll djb[4] = { -1,1,-1,1 };
// qween
ll dxq[8] = { 0,-1,-1,-1,0,1,1,1 };
ll dyq[8] = { -1,-1,0,1,1,1,0,-1 };


void solve() {
  ll h, w, m;
  cin >> h >> w >> m;
  auto s = read<string>(h);
  vector dp(m + 1, vector(h, vector<ll>(w, 1e18)));
  ll sx, sy, gx, gy;
  REP(i, h) {
    REP(j, w) {
      if (s[i][j] == 'S') {
        sx = i;
        sy = j;
      }
      if (s[i][j] == 'G') {
        gx = i;
        gy = j;
      }
    }
  }

  queue<tuple<ll, ll, ll>> que;
  dp[0][sx][sy] = 0;
  que.emplace(0, sx, sy);
  while (!que.empty()) {
    auto [b, x, y] = que.front();
    que.pop();
    REP(d, 4) {
      ll nx = x + dxr[d];
      ll ny = y + dyr[d];
      if (nx < 0 || ny < 0 || nx >= h || ny >= w)continue;
      if (s[nx][ny] == '#')continue;
      ll nb = b;
      if ('1' <= s[nx][ny] && s[nx][ny] <= '9') {
        nb = s[nx][ny] - '0';
      }
      if ('a' <= s[nx][ny] && s[nx][ny] <= 'i') {
        if (b != s[nx][ny] - 'a' + 1)continue;
      }
      if (dp[nb][nx][ny] <= dp[b][x][y] + 1) {
        continue;
      }
      dp[nb][nx][ny] = dp[b][x][y] + 1;
      que.emplace(nb, nx, ny);
    }
  }
  ll ans = 1e18;
  REP(i, m + 1) {
    chmin(ans, dp[i][gx][gy]);
  }
  if (ans > 1e17)ans = -1;
  cout << ans;
}


int main() {
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


0