結果
| 問題 | No.3504 Insert Maze |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-04-19 00:25:31 |
| 言語 | C++23 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,307 bytes |
| 記録 | |
| コンパイル時間 | 2,476 ms |
| コンパイル使用メモリ | 192,928 KB |
| 実行使用メモリ | 449,408 KB |
| 最終ジャッジ日時 | 2026-04-19 00:26:09 |
| 合計ジャッジ時間 | 24,170 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 69 WA * 16 |
ソースコード
#include <string>
#include <iostream>
#include <vector>
#include <stack>
#include <cassert>
#include <queue>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; ++i)
int main()
{
ll h, w;
cin >> h >> w;
vector<string> s(h);
rep(i, h) cin >> s[i];
s[0][0] = '.';
s[h-1][w-1] = '.';
auto itop = [&](ll i) {
return pair{i / w, i % w};
};
auto ptoi = [&](pair<ll, ll> p) {
return p.first * w + p.second;
};
vector<vector<ll>> g1(h * w), g2(h * w);
rep(i, h) {
rep(j, w) {
if(s[i][j] == '.') {
if(i < h-1 && s[i+1][j] == '.') g1[ptoi({i, j})].emplace_back(ptoi({i+1, j}));
if(j < w-1 && s[i][j+1] == '.') g1[ptoi({i, j})].emplace_back(ptoi({i, j+1}));
}
}
}
rep(i, h) {
rep(j, w) {
if(s[i][j] == '.') {
if(i > 0 && s[i-1][j] == '.') g2[ptoi({i, j})].emplace_back(ptoi({i-1, j}));
if(j > 0 && s[i][j-1] == '.') g2[ptoi({i, j})].emplace_back(ptoi({i, j-1}));
}
}
}
vector<bool> dist1(h*w, false), dist2(h * w, false);
dist1[0] = true;
dist2.back() = true;
queue<ll> q1, q2;
q1.push(0);
q2.push((h-1) * (w-1));
while(!q1.empty()) {
auto v = q1.front();
q1.pop();
for(auto c : g1[v]) {
if(dist1[c]) continue;
dist1[c] = true;
q1.emplace(c);
}
}
while(!q2.empty()) {
auto v = q2.front();
q2.pop();
for(auto c : g2[v]) {
if(dist2[c]) continue;
dist2[c] = true;
q2.emplace(c);
}
}
if(dist1.back()) {
cout << h + w - 2 << endl;
return 0;
}
rep(i, h-1) {
ll j = 0;
while(j < w && !dist1[i * w + j]) j++;
for(; j<w; j++) {
if(dist2[(i+1) * w + j]) {
cout << h + w - 1 << endl;
return 0;
}
}
}
rep(j, w) {
ll i = 0;
while(i < h && !dist1[i * w + j]) i++;
for(; i<h; i++) {
if(dist2[i * w + (j+1)]) {
cout << h + w - 1 << endl;
return 0;
}
}
}
cout << h + w << endl;
}