이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<cstdio>
#include<queue>
#include<memory.h>
#define origin 1061109567
#define MAX_N 1100
using namespace std;
struct data {
int x,y,v;
data() {
}
data(int x_,int y_,int v_) {
x = x_, y = y_, v = v_;
}
};
bool operator <(data X,data Y) {
return X.v > Y.v;
}
bool operator >(data X,data Y) {
return X.v < Y.v;
}
int n,m,ct,dir[5][2] = {{-1,0},{1,0},{0,1},{0,-1}};
char a[MAX_N][MAX_N];
int dist[MAX_N][MAX_N][4], sd[MAX_N][MAX_N], dp[MAX_N][MAX_N];
int x1,x2,y1,y2;
priority_queue<data> Q, Q_;
int main()
{
memset(sd,-1,sizeof(sd));
memset(dp,0x3f,sizeof(dp));
scanf("%d %d",&n,&m);
for(int i=0;i<n;i++) scanf("%s",a[i]);
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
if(a[i][j] == 'S') x1=i, y1=j;
if(a[i][j] == 'C') x2=i, y2=j;
}
}
for(int i=0;i<n;i++) dist[i][0][0] = (a[i][0]!='#' ? 1:0);
for(int i=0;i<n;i++) dist[i][m-1][1] = (a[i][m-1]!='#' ? 1:0);
for(int i=0;i<m;i++) dist[0][i][2] = (a[0][i]!='#' ? 1:0);
for(int i=0;i<m;i++) dist[n-1][i][3] = (a[n-1][i]!='#' ? 1:0);
for(int i=0;i<n;i++)
for(int j=1;j<m;j++) dist[i][j][0] = (a[i][j-1]!='#' ? dist[i][j-1][0]+1:1);
for(int i=0;i<n;i++)
for(int j=m-2;j>=0;j--) dist[i][j][1] = (a[i][j+1]!='#' ? dist[i][j+1][1]+1:1);
for(int i=0;i<m;i++)
for(int j=1;j<n;j++) dist[j][i][2] = (a[j-1][i]!='#' ? dist[j-1][i][2]+1:1);
for(int i=0;i<m;i++)
for(int j=n-1;j>=0;j--) dist[j][i][3] = (a[j+1][i]!='#' ? dist[j+1][i][3]+1:1);
for(int i=0;i<n;i++) {
if(a[i][0]!='#') Q.push(data(i,0,1));
if(a[i][m-1]!='#') Q.push(data(i,m-1,1));
}
for(int i=1;i<m-1;i++) {
if(a[0][i]!='#' ) Q.push(data(0,i,1));
if(a[n-1][i]!='#') Q.push(data(n-1,i,1));
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) if(a[i][j]=='#') {
Q.push(data(i,j,0));
}
}
while(ct < n*m) {
data T = Q.top();
Q.pop();
if(T.x>=0 && T.x<n && T.y>=0 && T.y<m && sd[T.x][T.y]==-1) {
ct++;
sd[T.x][T.y] = T.v;
for(int d=0;d<4;d++) Q.push(data(T.x+dir[d][0],T.y+dir[d][1],T.v+1));
}
}
Q_.push(data(x1,y1,1));
while(dp[x2][y2] == origin) {
data T = Q_.top();
Q_.pop();
if(T.x>=0 && T.x<n && T.y>=0 && T.y<m && a[T.x][T.y]!='#' && dp[T.x][T.y]==origin) {
dp[T.x][T.y] = T.v;
for(int d=0;d<4;d++) Q_.push(data(T.x+dir[d][0],T.y+dir[d][1],T.v+1));
Q_.push(data(T.x,T.y-dist[T.x][T.y][0]+1,T.v+sd[T.x][T.y]));
Q_.push(data(T.x,T.y+dist[T.x][T.y][1]-1,T.v+sd[T.x][T.y]));
Q_.push(data(T.x-dist[T.x][T.y][2]+1,T.y,T.v+sd[T.x][T.y]));
Q_.push(data(T.x+dist[T.x][T.y][3]-1,T.y,T.v+sd[T.x][T.y]));
}
}
printf("%d\n\n",dp[x2][y2]-1);
/*
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) printf("%d ",dp[i][j]);
printf("\n");
}
printf("\n");
for(int d=0;d<3;d++) {
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) printf("%d ",dist[i][j][d]);
printf("\n");
}
printf("\n");
}
*/
return 0;
}
# | 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... |