이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 1000;
int R,C, distWall[MX][MX], dist[MX][MX];
pi fst[MX][MX][4];
char g[MX][MX];
int xd[4] = {0,1,0,-1}, yd[4] = {1,0,-1,0};
bool isWall(int x, int y) {
if (x < 0 || x >= R || y < 0 || y >= C) return 1;
return g[x][y] == '#';
}
void genWall() {
queue<pi> q;
F0R(i,R) F0R(j,C) distWall[i][j] = MOD;
F0R(i,R) F0R(j,C) {
if (isWall(i,j)) distWall[i][j] = 0;
else {
F0Rd(z,4) if (isWall(i+xd[z],j+yd[z])) {
distWall[i][j] = 1;
q.push({i,j});
}
}
}
while (sz(q)) {
auto a = q.front(); q.pop();
F0R(i,4) {
pi A = {a.f+xd[i],a.s+yd[i]};
if (isWall(A.f,A.s)) continue;
if (distWall[A.f][A.s] != MOD) continue;
distWall[A.f][A.s] = distWall[a.f][a.s]+1;
q.push(A);
}
}
}
void genEdges() {
F0R(i,R) F0R(j,C) {
if (j == 0 || g[i][j-1] == '#') fst[i][j][0] = {i,j};
else fst[i][j][0] = fst[i][j-1][0];
if (i == 0 || g[i-1][j] == '#') fst[i][j][1] = {i,j};
else fst[i][j][1] = fst[i-1][j][1];
}
F0Rd(i,R) F0Rd(j,C) {
if (j == C-1 || g[i][j+1] == '#') fst[i][j][2] = {i,j};
else fst[i][j][2] = fst[i][j+1][2];
if (i == R-1 || g[i-1][j] == '#') fst[i][j][3] = {i,j};
else fst[i][j][3] = fst[i+1][j][3];
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> R >> C;
F0R(i,R) F0R(j,C) cin >> g[i][j];
genWall();
genEdges();
pi st,en;
F0R(i,R) F0R(j,C) {
if (g[i][j] == 'S') st = {i,j};
if (g[i][j] == 'C') en = {i,j};
}
F0R(i,R) F0R(j,C) dist[i][j] = MOD;
priority_queue<pair<int,pi>,vector<pair<int,pi>>,greater<pair<int,pi>>> p;
p.push({dist[st.f][st.s] = 0,st});
while (sz(p)) {
auto a = p.top(); p.pop();
if (dist[a.s.f][a.s.s] < a.f) continue;
F0R(z,4) {
pi A = {a.s.f+xd[z],a.s.s+yd[z]};
if (isWall(A.f,A.s)) continue;
if (dist[A.f][A.s] > a.f+1)
p.push({dist[A.f][A.s] = a.f+1,A});
}
F0R(z,4) {
pi A = fst[a.s.f][a.s.s][z];
if (dist[A.f][A.s] > a.f+distWall[a.s.f][a.s.s])
p.push({dist[A.f][A.s] = a.f+distWall[a.s.f][a.s.s],A});
}
}
cout << dist[en.f][en.s];
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# | 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... |