이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define task "C"
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;
const int MAXN = 1e3 + 5;
const ll INF = 1e18 + 5;
int m, n;
string s[MAXN];
void Input()
{
cin >> m >> n;
for (int i = 1; i <= m; i++)
{
cin >> s[i];
s[i] = ',' + s[i];
}
}
int lft[MAXN][MAXN], up[MAXN][MAXN], down[MAXN][MAXN], rght[MAXN][MAXN];
int d[MAXN][MAXN];
struct TState
{
int x, y;
int dlab;
bool operator < (const TState& other) const
{
return dlab > other.dlab;
}
bool Valid() const
{
return d[x][y] == dlab;
}
};
bool Chk(pair<int, int> p)
{
return p.fi > 0 && p.fi <= m && p.se > 0 && p.se <= n;
}
int Ox[4] = {1, -1, 0, 0};
int Oy[4] = {0, 0, 1, -1};
void Solve()
{
pair<int, int> S, C;
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
if (s[i][j] == '#')
{
lft[i][j] = 0;
up[i][j] = 0;
} else
{
if (s[i][j] == 'S')
S = {i, j};
if (s[i][j] == 'C')
C = {i, j};
up[i][j] = up[i - 1][j] + 1;
lft[i][j] = lft[i][j - 1] + 1;
}
}
}
for (int i = m; i >= 1; i--)
{
for (int j = n; j >= 1; j--)
{
if (s[i][j] == '#')
{
rght[i][j] = 0;
down[i][j] = 0;
} else
{
rght[i][j] = rght[i][j + 1] + 1;
down[i][j] = down[i + 1][j] + 1;
}
}
}
priority_queue<TState> PQ;
memset(d, 0x3f, sizeof(d));
d[S.fi][S.se] = 0;
PQ.push({S.fi, S.se, 0});
while (!PQ.empty())
{
auto u = PQ.top();
PQ.pop();
if (!u.Valid())
continue;
//cout << u.x << ' ' << u.y << ' ' << u.dlab << '\n';
if (u.x == C.fi && u.y == C.se)
{
cout << u.dlab;
return;
}
pair<int, int> v = {u.x, u.y - lft[u.x][u.y] + 1};
int cnt = 0;
for (int c = 0; c < 4; c++)
{
pair<int, int> v = {u.x + Ox[c], u.y + Oy[c]};
if (!Chk(v) || s[v.fi][v.se] == '#')
{
cnt++;
}
if (Chk(v) && d[v.fi][v.se] > u.dlab + 1 && s[v.fi][v.se] != '#')
{
d[v.fi][v.se] = u.dlab + 1;
PQ.push({v.fi, v.se, d[v.fi][v.se]});
}
}
if (cnt > 0)
{
if (Chk(v) && d[v.fi][v.se] > u.dlab + 1)
{
d[v.fi][v.se] = u.dlab + 1;
PQ.push({v.fi, v.se, d[v.fi][v.se]});
}
v = {u.x, u.y + rght[u.x][u.y] - 1};
if (Chk(v) && d[v.fi][v.se] > u.dlab + 1)
{
d[v.fi][v.se] = u.dlab + 1;
PQ.push({v.fi, v.se, d[v.fi][v.se]});
}
v = {u.x - up[u.x][u.y] + 1, u.y};
if (Chk(v) && d[v.fi][v.se] > u.dlab + 1)
{
d[v.fi][v.se] = u.dlab + 1;
PQ.push({v.fi, v.se, d[v.fi][v.se]});
}
v = {u.x + down[u.x][u.y] - 1, u.y};
if (Chk(v) && d[v.fi][v.se] > u.dlab + 1)
{
d[v.fi][v.se] = u.dlab + 1;
PQ.push({v.fi, v.se, d[v.fi][v.se]});
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
if (fopen(task".INP","r"))
{
freopen(task".INP","r",stdin);
//freopen(task".OUT","w",stdout);
}
Input();
Solve();
}
컴파일 시 표준 에러 (stderr) 메시지
portals.cpp: In function 'int main()':
portals.cpp:156:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
156 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |