# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
675211 |
2022-12-27T02:59:18 Z |
vjudge1 |
Portals (BOI14_portals) |
C++17 |
|
7 ms |
7508 KB |
#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();
}
Compilation message
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 |
1 |
Correct |
2 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4436 KB |
Output is correct |
7 |
Correct |
2 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
2 ms |
4308 KB |
Output is correct |
10 |
Incorrect |
2 ms |
4308 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
3 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
3 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4436 KB |
Output is correct |
7 |
Correct |
2 ms |
4436 KB |
Output is correct |
8 |
Correct |
3 ms |
4436 KB |
Output is correct |
9 |
Correct |
2 ms |
5076 KB |
Output is correct |
10 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4308 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4464 KB |
Output is correct |
4 |
Correct |
2 ms |
4360 KB |
Output is correct |
5 |
Correct |
6 ms |
7508 KB |
Output is correct |
6 |
Correct |
7 ms |
7508 KB |
Output is correct |
7 |
Correct |
7 ms |
7508 KB |
Output is correct |
8 |
Correct |
5 ms |
7508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4308 KB |
Output is correct |
2 |
Correct |
3 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4436 KB |
Output is correct |
4 |
Correct |
2 ms |
4308 KB |
Output is correct |
5 |
Correct |
2 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4436 KB |
Output is correct |
7 |
Correct |
3 ms |
4452 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4260 KB |
Output is correct |
2 |
Correct |
2 ms |
4436 KB |
Output is correct |
3 |
Correct |
2 ms |
4436 KB |
Output is correct |
4 |
Correct |
3 ms |
4332 KB |
Output is correct |
5 |
Correct |
3 ms |
4436 KB |
Output is correct |
6 |
Correct |
2 ms |
4436 KB |
Output is correct |
7 |
Correct |
2 ms |
4436 KB |
Output is correct |
8 |
Correct |
2 ms |
4436 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |