This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 50001
#define M ll(1e9 + 7)
#define inf 1e9 + 1e9
using namespace std;
//using namespace __gnu_pbds;
typedef long double ld;
typedef long long ll;
typedef short int si;
typedef array <int, 2> a2;
//typedef tree <int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
bool mk[501][501];
queue <a2> qr;
int dst[501][501], l[501][501], r[501][501], u[501][501], d[501][501];
int a[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int rst(int x, int y, int sx, int sy) {return abs(sx - x) + abs(sy - y);}
int main()
{
//freopen("input.txt", "r", stdin); //freopen("output4.txt", "w", stdout);
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
int sx, sy, fx, fy;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == 'C') {sx = i; sy = j;}
else if (s[i][j] == 'F') {fx = i; fy = j;}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (j != 0) l[i][j] = l[i][j - 1];
if (s[i][j] == '#') l[i][j] = j;
}
for (int j = m - 1; j >= 0; j--)
{
if (j + 1 != m) r[i][j] = r[i][j + 1];
if (s[i][j] == '#') r[i][j] = j;
}
}
for (int j = 0; j < m; j++)
{
for (int i = 0; i < n; i++)
{
if (i != 0) u[i][j] = u[i - 1][j];
if (s[i][j] == '#') u[i][j] = i;
}
for (int i = n - 1; i >= 0; i--)
{
if (i + 1 != n) d[i][j] = d[i + 1][j];
if (s[i][j] == '#') d[i][j] = i;
}
}
qr.push({sx, sy});
mk[sx][sy] = 1;
dst[sx][sy] = 0;
while (sz(qr) > 0)
{
int x = qr.front()[0], y = qr.front()[1];
qr.pop();
for (int i = 0; i < 4; i++)
{
int cx = x + a[i][0];
int cy = y + a[i][1];
if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + 1 || !mk[cx][cy]) && s[cx][cy] != '#')
{
mk[cx][cy] = 1;
dst[cx][cy] = dst[x][y] + 1;
qr.push({cx, cy});
}
}
int mn = min(rst(x, y, x, l[x][y]), min(rst(x, y, x, r[x][y]), min(rst(x, y, u[x][y], y), rst(x, y, d[x][y], y))));
int cx = x, cy = l[x][y] + 1;
if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
{
mk[cx][cy] = 1;
dst[cx][cy] = dst[x][y] + mn;
qr.push({cx, cy});
}
cy = r[x][y] - 1;
if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
{
mk[cx][cy] = 1;
dst[cx][cy] = dst[x][y] + mn;
qr.push({cx, cy});
}
cy = y; cx = u[x][y] + 1;
if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
{
mk[cx][cy] = 1;
dst[cx][cy] = dst[x][y] + mn;
qr.push({cx, cy});
}
cx = d[x][y] - 1;
if (cx < n && cy < m && cx >= 0 && cy >= 0 && (dst[cx][cy] > dst[x][y] + mn || !mk[cx][cy]) && s[cx][cy] != '#')
{
mk[cx][cy] = 1;
dst[cx][cy] = dst[x][y] + mn;
qr.push({cx, cy});
}
}
if (!mk[fx][fy]) cout << "nemoguce" << endl;
else cout << dst[fx][fy];
}
Compilation message (stderr)
portal.cpp: In function 'int main()':
portal.cpp:100:17: warning: 'sx' may be used uninitialized in this function [-Wmaybe-uninitialized]
dst[sx][sy] = 0;
~~~~~~~~~~~~^~~
portal.cpp:171:30: warning: 'fy' may be used uninitialized in this function [-Wmaybe-uninitialized]
else cout << dst[fx][fy];
^
portal.cpp:100:17: warning: 'sy' may be used uninitialized in this function [-Wmaybe-uninitialized]
dst[sx][sy] = 0;
~~~~~~~~~~~~^~~
portal.cpp:171:30: warning: 'fx' may be used uninitialized in this function [-Wmaybe-uninitialized]
else cout << dst[fx][fy];
^
# | 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... |