#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#define F first
#define S second
#define pb push_back
#define N +100500
#define M ll(1e9 + 7)
#define sz(x) (int)x.size()
#define re return
#define oo ll(1e9)
#define el '\n'
#define pii pair <int, int>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree <int, null_type, less_equal <int> , rb_tree_tag, tree_order_statistics_node_update> ordered_set;
typedef long long ll;
typedef long double ld;
set <pair <int, pair <int, int> > > q;
int mrk[550][550];
char c[550][550];
int go[4][550][550], i, j, n, m, ans, x, y, sx, sy, ex, ey;
void get(int &x, int &y, int &st)
{
x = (*q.begin()).S.F;
y = (*q.begin()).S.S;
st = (*q.begin()).F;
q.erase(q.begin());
}
void put(int x, int y, int st)
{
if (mrk[x][y] < st) return;
q.erase({mrk[x][y], {x, y}});
q.insert({st, {x, y}});
mrk[x][y] = st;
}
void Portal(int x, int y, int st)
{
for (int fst = 0; fst < 4; fst++)
{
for (int sd = 0; sd < 4; sd++)
{
if (fst == sd) continue;
int cx = x;
int cy = y;
int len = 1;
if (fst < 2) cy = go[fst][x][y], len += abs(y - cy);
else cx = go[fst][x][y], len += abs(x - cx);
if (sd < 2) cy = go[sd][x][y], cx = x;
else cx = go[sd][x][y], cy = y;
// cerr << fst << " " << sd << " " << x << " " << y << " " << cx << " " << cy << el;
put(cx, cy, st + len);
}
}
}
void putall(int x, int y, int st)
{
int steps[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
for (int i = 0; i < 4; i++)
{
int cx = x + steps[i][0];
int cy = y + steps[i][1];
if (cx < 1 || cy < 1 || cx > n || cy > m || c[cx][cy] == '#') continue;
put(cx, cy, st + 1);
}
Portal(x, y, st);
}
int main()
{
srand(time(0));
ios_base::sync_with_stdio(0);
iostream::sync_with_stdio(0);
ios::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
// in("input.txt");
// out("output.txt");
cin >> n >> m;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
cin >> c[i][j];
if (c[i][j] == 'C') sx = i, sy = j;
if (c[i][j] == 'F') ex = i, ey = j;
mrk[i][j] = 1e9;
}
}
int lst;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= m; j++)
{
if (c[i][j] == '#') lst = j;
go[0][i][j] = lst + 1;
}
for (j = m; j >= 1; j--)
{
if (c[i][j] == '#') lst = j;
go[1][i][j] = lst - 1;
}
}
for (j = 1; j <= m; j++)
{
for (i = 1; i <= n; i++)
{
if (c[i][j] == '#') lst = i;
go[2][i][j] = lst + 1;
}
for (i = n; i >= 1; i--)
{
if (c[i][j] == '#') lst = i;
go[3][i][j] = lst - 1;
}
}
put(sx, sy, 0);
while (sz(q))
{
int st;
get(x, y, st);
putall(x, y, st);
}
if (mrk[ex][ey] == oo) cout << "nemoguce";
else cout << mrk[ex][ey] << el;
}
//
//110000
//1100
Compilation message
portal.cpp: In function 'int main()':
portal.cpp:139:31: warning: 'lst' may be used uninitialized in this function [-Wmaybe-uninitialized]
go[2][i][j] = lst + 1;
~~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
512 KB |
Output is correct |
2 |
Correct |
5 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
1536 KB |
Output is correct |
3 |
Correct |
18 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
2048 KB |
Output is correct |
2 |
Correct |
7 ms |
2560 KB |
Output is correct |
3 |
Correct |
17 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
4856 KB |
Output is correct |
2 |
Correct |
56 ms |
3200 KB |
Output is correct |
3 |
Correct |
34 ms |
3584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
5504 KB |
Output is correct |
2 |
Correct |
15 ms |
6016 KB |
Output is correct |
3 |
Correct |
65 ms |
3812 KB |
Output is correct |
4 |
Correct |
50 ms |
4864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
6264 KB |
Output is correct |
2 |
Correct |
17 ms |
6016 KB |
Output is correct |
3 |
Correct |
89 ms |
6016 KB |
Output is correct |
4 |
Correct |
92 ms |
6136 KB |
Output is correct |