| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1356151 | nathako9n | Portals (BOI14_portals) | C++20 | 6 ms | 4980 KiB |
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
using namespace std;
const int N = 1005;
char ar[N+3][N+3];
int n,m;
ll dp[N+3][N+3];
int px[]={-1,0,1,0},py[]={0,1,0,-1};
int up[N+3][N+3],dow[N+3][N+3],lef[N+3][N+3],rig[N+3][N+3];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
int sx,sy,ex,ey;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>ar[i][j];
if(ar[i][j]=='S'){
sx=i,sy=j;
}
else if(ar[i][j]=='C'){
ex=i, ey=j;
}
dp[i][j]=1e18;
}
}
for(int i=1;i<=n;i++){
int last=0;
for(int j=1;j<=m;j++){
if(ar[i][j]=='#') last=j;
lef[i][j]=last+1;
}
last=m+1;
for(int j=m;j>=1;j--){
if(ar[i][j]=='#') last=j;
rig[i][j]=last-1;
}
}
for(int j=1;j<=m;j++){
int last=0;
for(int i=1;i<=n;i++){
if(ar[i][j]=='#') last=i;
up[i][j]=last+1;
}
last=n+1;
for(int i=n;i>=1;i--){
if(ar[i][j]=='#') last=i;
dow[i][j]=last-1;
}
}
dp[sx][sy]=0;
priority_queue<tuple<ll,int,int>,vector<tuple<ll,int,int>>,greater<tuple<ll,int,int>>> pq;
pq.push({0,sx,sy});
while(!pq.empty()){
auto[w,cx,cy]=pq.top(); pq.pop();
if(w>dp[cx][cy])continue;
for(int i=0;i<4;i++){
int nx=px[i]+cx;
int ny=py[i]+cy;
//if(nx<1||ny<1||nx>n||ny>m)continue;
if(ar[nx][ny]=='#'||nx<1||ny<1||nx>n||ny>m){
int nxx=dow[cx][cy];
int nyy=cy;
if(dp[nxx][nyy]>w+1){
dp[nxx][nyy]=w+1;
pq.push({w+1,nxx,nyy});
}
nxx=cx;
nyy=lef[cx][cy];
if(dp[nxx][nyy]>w+1){
dp[nxx][nyy]=w+1;
pq.push({w+1,nxx,nyy});
}
nxx=up[cx][cy];
nyy=cy;
if(dp[nxx][nyy]>w+1){
dp[nxx][nyy]=w+1;
pq.push({w+1,nxx,nyy});
}
nxx=cx;
nyy=rig[cx][cy];
if(dp[nxx][nyy]>w+1){
dp[nxx][nyy]=w+1;
pq.push({w+1,nxx,nyy});
}
}
else{
if(dp[nx][ny]>w+1){
dp[nx][ny]=w+1;
pq.push({w+1,nx,ny});
}
}
//if(cx==4&&cy==3)cout<<i<<" "<<cx<<" "<<cy<<" "<<nx<<" "<<ny<<endl;
}
}
cout<<dp[ex][ey]<<endl;
return 0;
}
/*
4 4
.#.C
.#.#
....
S...
*/
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
| # | Verdict | Execution time | Memory | Grader output |
|---|---|---|---|---|
| Fetching results... | ||||
