結果
| 問題 |
No.3199 Key-Door Grid
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-07-21 16:53:43 |
| 言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 154 ms / 3,000 ms |
| コード長 | 2,942 bytes |
| コンパイル時間 | 5,768 ms |
| コンパイル使用メモリ | 334,076 KB |
| 実行使用メモリ | 33,408 KB |
| 最終ジャッジ日時 | 2025-07-21 16:53:53 |
| 合計ジャッジ時間 | 9,921 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:78:18: warning: ‘gx’ may be used uninitialized [-Wmaybe-uninitialized]
78 | if (dp[gx][gy][i] < INF) {
| ^
main.cpp:40:16: note: ‘gx’ was declared here
40 | ll sx, sy, gx, gy = -1;
| ^~
main.cpp:49:10: warning: ‘sx’ may be used uninitialized [-Wmaybe-uninitialized]
49 | dp[sx][sy][0] = 0;
| ^
main.cpp:40:8: note: ‘sx’ was declared here
40 | ll sx, sy, gx, gy = -1;
| ^~
main.cpp:49:14: warning: ‘sy’ may be used uninitialized [-Wmaybe-uninitialized]
49 | dp[sx][sy][0] = 0;
| ^
main.cpp:40:12: note: ‘sy’ was declared here
40 | ll sx, sy, gx, gy = -1;
| ^~
ソースコード
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#ifndef ONLINE_JUDGE
#define _GLIBCXX_DEBUG
#endif
using ll = long long;
using ld = long double;
using P = pair<ll,ll>;
template<class T> using pq = priority_queue<T>;
template<class T> using pq_g = priority_queue<T, vector<T>, greater<T>>;
#define out(x) cout<<x<<'\n'
#define dump(x) cerr<<#x<<" = "<<x<<'\n'
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define OVERLOAD_REP(_1,_2,_3,name,...) name
#define REP1(i,n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i, l, r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
template<class T> inline bool chmin(T& a, T b) {if(a > b){a = b; return true;} else {return false;}};
template<class T> inline bool chmax(T& a, T b) {if(a < b){a = b; return true;} else {return false;}};
const ll INF=(1LL<<60);
const ll mod=998244353;
using Graph = vector<vector<ll>>;
using Network = vector<vector<pair<ll,ll>>>;
using Grid = vector<string>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int dx2[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy2[8] = {1, 0, -1, 1, -1, 1, 0, -1};
void solve() {
ll H, W, M;
cin>>H>>W>>M;
vector<string> S(H);
rep(i, H) cin >> S[i];
ll sx, sy, gx, gy = -1;
rep(i, H) rep(j, W) {
if (S[i][j] == 'S') {
sx = i; sy = j;
} else if (S[i][j] == 'G') {
gx = i; gy = j;
}
}
vector<vector<vector<ll>>> dp(H, vector<vector<ll>>(W, vector<ll>(M+1, INF)));
dp[sx][sy][0] = 0;
queue<tuple<ll, ll, ll>> q;
q.push({sx, sy, 0});
while (!q.empty()) {
auto [x, y, m] = q.front(); q.pop();
rep(i, 4) {
ll nx = x + dx[i], ny = y + dy[i];
if (nx < 0 || nx >= H || ny < 0 || ny >= W || S[nx][ny] == '#') continue;
if (S[nx][ny] >= '1' && S[nx][ny] <= '9') {
ll nm = (S[nx][ny] - '0');
if (chmin(dp[nx][ny][nm], dp[x][y][m] + 1)) {
q.push({nx, ny, nm});
}
}
else if (S[nx][ny] >= 'a' && S[nx][ny] <= 'i') {
ll num = (S[nx][ny] - 'a' + 1);
if (m == num && chmin(dp[nx][ny][m], dp[x][y][m] + 1)) {
q.push({nx, ny, m});
}
}
else {
if (chmin(dp[nx][ny][m], dp[x][y][m] + 1)) {
q.push({nx, ny, m});
}
}
}
}
ll ans = INF;
rep(i, M+1) {
if (dp[gx][gy][i] < INF) {
chmin(ans, dp[gx][gy][i]);
}
}
if (ans == INF) {
out(-1);
} else {
out(ans);
}
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
solve();
return 0;
}