이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define MEM(a, b) memset(a, (b), sizeof(a))
#define ALL(c) (c).begin(),(c).end()
#define LINF (int)1e18
#define INF (int)1e9
#define ll long long
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define IO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define MAX 510
using namespace std;
typedef pair<int,int> pii;
int n, m, si, sj, ei, ej, nx, ny, mm;
string mat[MAX];
int path[4][MAX][MAX], v[MAX][MAX];
int dy[] = {1, -1, 0, 0};
int dx[] = {0, 0, 1, -1};
struct point{
int y, x, d;
point(int y, int x, int d) {
this->y = y;
this->x = x;
this->d = d;
}
bool operator<(const point &pn) const{
if(d==pn.d) {
if(y==pn.y) return x<pn.x;
return y<pn.y;
}
return d<pn.d;
}
};
set<point> s;
inline bool check(int y, int x) {return (y>=0 && y<n && x>=0 && x<m && mat[y][x]!='#');}
inline void preprocess_paths() {
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) {
if(mat[i][j]=='#') continue;
if(mat[i][j-1]=='#')
for(int k=j;k<m;k++) {
if(mat[i][k]=='#') break;
path[0][i][k] = k-j;
}
if(mat[i][j+1]=='#')
for(int k=j;k>=0;k--) {
if(mat[i][k]=='#') break;
path[1][i][k] = j-k;
}
if(mat[i-1][j]=='#')
for(int k=i;k<n;k++) {
if(mat[k][j]=='#') break;
path[2][k][j] = k-i;
}
if(mat[i+1][j]=='#')
for(int k=i;k>=0;k--) {
if(mat[k][j]=='#') break;
path[3][k][j] = i-k;
}
}
}
inline void solve() {
for(int i=0;i<n;i++) for(int j=0;j<m;j++) v[i][j] = INF;
s.insert(point(si,sj,0));
v[si][sj] = 0;
while(!s.empty()) {
point c = *s.begin();
s.erase(s.begin());
for(int k=0;k<4;k++) {
ny = c.y + dy[k];
nx = c.x + dx[k];
if(check(ny,nx))
if(v[ny][nx]>c.d+1) {
s.erase(point(ny,nx,v[ny][nx]));
s.insert(point(ny,nx,c.d+1));
v[ny][nx] = c.d+1;
}
}
mm = min(path[0][c.y][c.x], min(path[1][c.y][c.x], min(path[2][c.y][c.x], path[3][c.y][c.x]))); mm++;
ny = c.y; nx = c.x;
nx = c.x - path[0][c.y][c.x];
if(check(ny,nx))
if(v[ny][nx]>mm+c.d)
s.erase(point(ny,nx,v[ny][nx])), s.insert(point(ny,nx,mm+c.d)), v[ny][nx] = mm+c.d;
ny = c.y; nx = c.x;
nx = c.x + path[1][c.y][c.x];
if(check(ny,nx))
if(v[ny][nx]>mm+c.d)
s.erase(point(ny,nx,v[ny][nx])), s.insert(point(ny,nx,mm+c.d)), v[ny][nx] = mm+c.d;
ny = c.y; nx = c.x;
ny = c.y - path[2][c.y][c.x];
if(check(ny,nx))
if(v[ny][nx]>mm+c.d)
s.erase(point(ny,nx,v[ny][nx])), s.insert(point(ny,nx,mm+c.d)), v[ny][nx] = mm+c.d;
ny = c.y; nx = c.x;
ny = c.y + path[3][c.y][c.x];
if(check(ny,nx))
if(v[ny][nx]>mm+c.d)
s.erase(point(ny,nx,v[ny][nx])), s.insert(point(ny,nx,mm+c.d)), v[ny][nx] = mm+c.d;
}
}
int main() {
IO;
cin >> n >> m;
for(int i=0;i<n;i++) cin >> mat[i];
for(int i=0;i<n;i++)
for(int j=0;j<m;j++) {
if(mat[i][j]=='C') si = i, sj = j;
if(mat[i][j]=='F') ei = i, ej = j;
}
preprocess_paths();
solve();
if(v[ei][ej]==INF) cout << "nemoguce";
else cout << v[ei][ej];
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... |
# | 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... |