結果
| 問題 | No.3199 Key-Door Grid |
| コンテスト | |
| ユーザー |
Kude
|
| 提出日時 | 2025-07-11 21:43:07 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.90.0) |
| 結果 |
AC
|
| 実行時間 | 51 ms / 3,000 ms |
| + 781µs | |
| コード長 | 2,141 bytes |
| 記録 | |
| コンパイル時間 | 2,521 ms |
| コンパイル使用メモリ | 361,072 KB |
| 実行使用メモリ | 13,740 KB |
| 最終ジャッジ日時 | 2026-07-13 03:21:35 |
| 合計ジャッジ時間 | 5,204 ms |
|
ジャッジサーバーID (参考情報) |
judge2_0 / judge3_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 37 |
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:34:15: warning: 'ti' may be used uninitialized [-Wmaybe-uninitialized]
34 | int si, sj, ti, tj;
| ^~
main.cpp:34:19: warning: 'tj' may be used uninitialized [-Wmaybe-uninitialized]
34 | int si, sj, ti, tj;
| ^~
main.cpp:34:7: warning: 'si' may be used uninitialized [-Wmaybe-uninitialized]
34 | int si, sj, ti, tj;
| ^~
main.cpp:34:11: warning: 'sj' may be used uninitialized [-Wmaybe-uninitialized]
34 | int si, sj, ti, tj;
| ^~
ソースコード
#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
const int di[] = {1, 0, -1, 0};
const int dj[] = {0, 1, 0, -1};
int dist[500][500][10];
} int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w, m;
cin >> h >> w >> m;
constexpr int INF = 1001001001;
rep(i, h) rep(j, w) rep(k, m + 1) dist[i][j][k] = INF;
vector<string> s(h);
rep(i, h) cin >> s[i];
int si, sj, ti, tj;
rep(i, h) rep(j, w) {
if (s[i][j] == 'S') {
si = i, sj = j;
s[i][j] = '.';
} else if (s[i][j] == 'G') {
ti = i, tj = j;
s[i][j] = '.';
}
}
deque<tuple<int, int, int, int>> q;
dist[si][sj][0] = 0;
q.emplace_back(si, sj, 0, 0);
auto idxchk = [&](int i, int j) { return 0 <= i && i < h && 0 <= j && j < w; };
while (q.size()) {
auto [i, j, k, d] = q.front(); q.pop_front();
if (dist[i][j][k] != d) continue;
if ('1' <= s[i][j] && s[i][j] <= '9' && k != s[i][j] - '0') {
int nk = s[i][j] - '0';
if (chmin(dist[i][j][nk], d)) q.emplace_front(i, j, nk, d);
continue;
} else if ('a' <= s[i][j] && s[i][j] <= 'i' && k != s[i][j] - 'a' + 1) {
continue;
}
rep(dir, 4) {
int ni = i + di[dir], nj = j + dj[dir];
if (!idxchk(ni, nj) || s[ni][nj] == '#') continue;
if (chmin(dist[ni][nj][k], d + 1)) q.emplace_back(ni, nj, k, d + 1);
}
}
int ans = INF;
rep(k, m + 1) chmin(ans, dist[ti][tj][k]);
if (ans == INF) ans = -1;
cout << ans << '\n';
}
Kude