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>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 403;
const int xx[9] = {0, 1, 0, -1, 1, -1, 1, -1, 0};
const int yy[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int n, m;
char a[N][N];
int rec(vector<pair<int, int> > s, vector<vector<bool> > cc, vector<vector<bool> > tt)
{
vector<vector<bool> > c(2 * N, vector<bool>(2 * N, false));
queue<pair<int, int> > q;
q.push(m_p(0, 0));
tt[N + 0][N + 0] = true;
cc[N + 0][N + 0] = true;
int ans = 0;
while (!q.empty())
{
int dx = q.front().fi;
int dy = q.front().se;
q.pop();
if (cc[N + dx][N + dy] == false)
{
cc[N + dx][N + dy] = true;
map<string, vector<pair<int, int> > > mp;
for (int i = 0; i < sz(s); ++i)
{
string ss = "";
for (int j = 0; j < 9; ++j)
{
int x = s[i].fi + dx + xx[j];
int y = s[i].se + dy + yy[j];
if (a[x][y] == 'X')
ss += '.';
else
ss += a[x][y];
}
mp[ss].push_back(s[i]);
}
if (sz(mp) > 1)
{
ans = N * N;
for (auto it = mp.begin(); it != mp.end(); ++it)
{
ans = min(ans, rec(it->se, cc, tt));
}
return ans;
}
}
{
int x = s[0].fi + dx;
int y = s[0].se + dy;
if (a[x][y] == 'o')
++ans;
}
for (int i = 0; i < 4; ++i)
{
int ndx = dx + xx[i];
int ndy = dy + yy[i];
bool z = true;
if (tt[N + ndx][N + ndy] == false)
{
for (int i = 0; i < sz(s); ++i)
{
int x = s[i].fi + ndx;
int y = s[i].se + ndy;
if (a[x][y] == 'X' || a[x][y] == '#')
{
z = false;
break;
}
}
}
if (z)
{
if (c[N + ndx][N + ndy] == false)
{
q.push(m_p(ndx, ndy));
tt[N + ndx][N + ndy] = true;
c[N + ndx][N + ndy] = true;
}
}
}
}
return ans;
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
cin >> (a[i] + 1);
}
vector<pair<int, int> > s;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if (a[i][j] == 'S')
{
s.push_back(m_p(i, j));
a[i][j] = '.';
}
}
}
vector<vector<bool> > gg(2 * N, vector<bool>(2 * N, false));
cout << rec(s, gg, gg) << "\n";
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
return 0;
}
# | 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... |