結果
| 問題 | No.3504 Insert Maze |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-18 02:37:39 |
| 言語 | C++23(gnu拡張) (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
AC
|
| 実行時間 | 742 ms / 2,000 ms |
| コード長 | 4,178 bytes |
| 記録 | |
| コンパイル時間 | 4,440 ms |
| コンパイル使用メモリ | 384,596 KB |
| 実行使用メモリ | 132,736 KB |
| 最終ジャッジ日時 | 2026-04-18 02:37:59 |
| 合計ジャッジ時間 | 17,713 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge1_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 85 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
namespace cho {
struct linear_index {
int bg, ed;
linear_index() {};
linear_index(int x, int y) : bg(x), ed(y) {};
int operator()(int i) const {
assert(bg <= i && i < ed);
return i - bg;
}
int inverse(int i) const {
assert(0 <= i && i < ed - bg);
return i + bg;
}
int size() const {
return ed - bg;
}
};
template <int dim>
struct md_index {
std::array<linear_index, dim> ar;
template <typename... Args>
md_index(Args... args) {
static_assert(sizeof...(args) == dim * 2, "Args mismatch");
const std::array<int, dim * 2> lr = {(int)args...};
for (int i = 0; i < dim; i++) {
ar[i] = linear_index(lr[i * 2], lr[i * 2 + 1]);
}
}
template <typename... Args>
int operator()(Args... args) const {
static_assert(sizeof...(args) == dim, "Args mismatch");
const std::array<int, dim> ind = {(int)args...};
int res = 0;
for (int i = 0; i < dim; i++) {
res *= ar[i].size();
res += ar[i](ind[i]);
}
return res;
}
std::array<int, dim> inverse(int x) const {
std::array<int, dim> res;
for (int i = dim - 1; i >= 0; i--) {
res[i] = ar[i].inverse(x % ar[i].size());
x /= ar[i].size();
}
return res;
}
int size() const {
int res = 1;
for (int i = 0; i < dim; i++) res *= ar[i].size();
return res;
}
};
} // namespace cho
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define all(x) begin(x), end(x)
template <class T> bool chmin(T& x, T y) {
return x > y ? (x = y, true) : false;
}
template <class T> bool chmax(T& x, T y) {
return x < y ? (x = y, true) : false;
}
using mint = atcoder::modint998244353;
void solve() {
int h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, 0, h) cin >> s[i];
cho::md_index<3> ind(0, h, 0, w, 0, 2);
cho::md_index<2> id_h(0, h, 0, w - 1), id_w(0, h - 1, 0, w);
int sz = ind.size(), szh = id_h.size(), szw = id_w.size();
auto isin = [&](int x, int y) {
return 0 <= x && x < h && 0 <= y && y < w;
};
vector<int> dd = {0, 1, 0, -1, 0};
ll ans = h + w;
vector<ll> dist(sz + szh + szw, 1e18);
dist[0] = 0;
queue<int> q;
q.push(0);
while (!q.empty()) {
int nw = q.front();
q.pop();
if (nw < sz) {
auto [x, y, f] = ind.inverse(nw);
rep(k, 0, 4) {
int nx = x + dd[k], ny = y + dd[k + 1];
if (!isin(nx, ny) || s[nx][ny] == '#') continue;
int nn = ind(nx, ny, f);
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (f == 0) {
if (y < w - 1) {
int nn = id_h(x, y) + sz;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (y > 0) {
int nn = id_h(x, y - 1) + sz;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (x < h - 1) {
int nn = id_w(x, y) + sz + szh;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (x > 0) {
int nn = id_w(x - 1, y) + sz + szh;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
}
} else if (nw < sz + szh) {
auto [x, y] = id_h.inverse(nw - sz);
if (x > 0) {
int nn = id_h(x - 1, y) + sz;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (x < h - 1) {
int nn = id_h(x + 1, y) + sz;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
{
int nn = ind(x, y, 1);
if (s[x][y] != '#' && chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
{
int nn = ind(x, y + 1, 1);
if (s[x][y + 1] != '#' && chmin(dist[nn], dist[nw] + 1))
q.push(nn);
}
} else {
auto [x, y] = id_w.inverse(nw - sz - szh);
if (y > 0) {
int nn = id_w(x, y - 1) + sz + szh;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
if (y < w - 1) {
int nn = id_w(x, y + 1) + sz + szh;
if (chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
{
int nn = ind(x, y, 1);
if (s[x][y] != '#' && chmin(dist[nn], dist[nw] + 1)) q.push(nn);
}
{
int nn = ind(x + 1, y, 1);
if (s[x + 1][y] != '#' && chmin(dist[nn], dist[nw] + 1))
q.push(nn);
}
}
}
rep(f, 0, 2) chmin(ans, dist[ind(h - 1, w - 1, f)]);
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
int t = 1;
// cin >> t;
while (t--) solve();
}