結果
| 問題 |
No.2913 二次元距離空間
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-10-04 22:58:18 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 20 ms / 2,000 ms |
| コード長 | 3,752 bytes |
| コンパイル時間 | 1,464 ms |
| コンパイル使用メモリ | 176,708 KB |
| 実行使用メモリ | 17,608 KB |
| 最終ジャッジ日時 | 2024-10-04 22:58:21 |
| 合計ジャッジ時間 | 2,441 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
コンパイルメッセージ
main.cpp: In function 'int32_t main()':
main.cpp:43:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
43 | for(auto [dx,dy]:d)
| ^
main.cpp:82:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17' [-Wc++17-extensions]
82 | for(auto [dx,dy]:d)
| ^
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define int long long
int p;
int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*1LL*u)%p;} else {int u=po(a,b-1);return (a*1LL*u)%p;}}
int inv(int x) {return po(x,p-2);}
mt19937 rnd;
#define app push_back
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define debug(...) [](auto...a){ ((cout << a << ' '), ...) << endl;}(#__VA_ARGS__, ":", __VA_ARGS__)
#define debugv(v) do {cout<< #v <<" : {"; for(int izxc=0;izxc<v.size();++izxc) {cout << v[izxc];if(izxc+1!=v.size()) cout << ","; }cout <<"}"<< endl;} while(0)
#else
#define debug(...)
#define debugv(v)
#endif
#define lob(a,x) lower_bound(all(a),x)
#define upb(a,x) upper_bound(all(a),x)
const int inf=1e18;
const int maxn=1005;
vector<pair<int,int> > cur;
vector<pair<int,int> > nx;
vector<pair<int,int> > d={{-1,0},{0,-1},{1,0},{0,1}};
int ans[maxn][maxn];
int uu[maxn][maxn];
int32_t main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,m;cin>>n>>m;string s[n];for(int i=0;i<n;++i) {cin>>s[i];}
{
cur.app({0,0});
for(int i=0;i<maxn;++i) {for(int j=0;j<maxn;++j) {ans[i][j]=inf;}} ans[0][0]=0;
int zx=0;
while(!cur.empty())
{
nx.clear();
while(!cur.empty())
{
auto h=cur.back();cur.pop_back();
debug(h.first,h.second);
if(ans[h.first][h.second]!=zx) {continue;}
for(auto [dx,dy]:d)
{
if(h.first+dx>=0 && h.first+dx<n && h.second+dy>=0 && h.second+dy<m)
{
if(s[h.first+dx][h.second+dy]!='#')
{
if(dx && ans[h.first+dx][h.second+dy]>zx)
{
ans[h.first+dx][h.second+dy]=zx;cur.app({h.first+dx,h.second+dy});
}
else if(dy && ans[h.first+dx][h.second+dy]>zx+1)
{
debug(h.first+dx,h.second+dy);
ans[h.first+dx][h.second+dy]=zx+1;nx.app({h.first+dx,h.second+dy});
}
}
}
}
}
cur=nx;
++zx;
}
for(int i=0;i<n;++i) for(int j=0;j<m;++j) uu[i][j]=ans[i][j];
if(ans[n-1][m-1]==inf) {cout<<"No";return 0;}
cout<<"Yes"<<'\n';
cout<<ans[n-1][m-1]<<' ';
}
{
cur.app({0,0});
for(int i=0;i<maxn;++i) {for(int j=0;j<maxn;++j) {ans[i][j]=inf;}} ans[0][0]=0;
int zx=0;
while(!cur.empty())
{
nx.clear();
while(!cur.empty())
{
auto h=cur.back();cur.pop_back();
debug(h.first,h.second);
if(ans[h.first][h.second]!=zx) {continue;}
for(auto [dx,dy]:d)
{
if(h.first+dx>=0 && h.first+dx<n && h.second+dy>=0 && h.second+dy<m)
{
if(s[h.first+dx][h.second+dy]!='#' && uu[h.first+dx][h.second+dy]==uu[h.first][h.second]+(!!dy))
{
if(dy && ans[h.first+dx][h.second+dy]>zx)
{
ans[h.first+dx][h.second+dy]=zx;cur.app({h.first+dx,h.second+dy});
}
else if(dx && ans[h.first+dx][h.second+dy]>zx+1)
{
debug(h.first+dx,h.second+dy);
ans[h.first+dx][h.second+dy]=zx+1;nx.app({h.first+dx,h.second+dy});
}
}
}
}
}
cur=nx;
++zx;
}
if(ans[n-1][m-1]==inf) {cout<<"No";return 0;}
cout<<ans[n-1][m-1]<<' ';
}
return 0;
}