# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
953194 | tmtxangel | Mecho (IOI09_mecho) | C++17 | 115 ms | 7000 KiB |
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>
#define lli long long
#define ld long double
#define tmt angel
using namespace std;
const int maxn = 802;
int vis[maxn][maxn], n, m;
char a[maxn][maxn];
int tim[maxn][maxn];
int hi, hj, sti, stj;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
deque<pair<pair<int, int>, int>> s;
bool flag = false;
void gocheck(int i, int j, int cost)
{
if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1 || a[i][j] == 'D') return;
vis[i][j] = 1;
tim[i][j] = cost;
s.push_back({{i, j}, cost});
}
void spread(int i, int j, int cost, int step)
{
if(vis[hi][hj] == 1) return;
if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1 || tim[i][j] < cost) return;
vis[i][j] = 1;
// cout << i << " " << j << " " << cost << " " << step << '\n';
if(vis[hi][hj] == 1) return;
if(step == m)
{
//tim[1][i][j] = cost;
s.push_back({{i, j}, cost});
}
else
{
// tim[1][i][j] = cost;
for(int f = 0; f < 4; f++)
{
spread(i + x[f], j + y[f], cost, step + 1);
if(vis[hi][hj] == 1) break;
}
}
}
void Bfs()
{
while(!s.empty())
{
int i = s.front().first.first;
int j = s.front().first.second;
int c = s.front().second;
s.pop_front();
for(int f = 0; f < 4; f++)
{
gocheck(i + x[f], j + y[f], c+1);
}
}
}
bool checked(int st)
{
s.clear();
flag = false;
// if(st == 0) s.push_back({ {sti, stj},{2, 0} });
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
vis[i][j] = a[i][j] == 'G' || a[i][j] == 'D' ? 0 : 1;
}
}
s.push_back({{sti, stj}, st});
while(!s.empty() && vis[hi][hj] == 0)
{
int i = s.front().first.first;
int j = s.front().first.second;
int c = s.front().second;
s.pop_front();
for(int f = 0; f < 4; f++)
{
spread(i + x[f], j + y[f], c+1, 1);
if(vis[hi][hj] == 1) break;
}
}
return vis[hi][hj] == 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
// freopen(".INP", "r" , stdin);
// freopen(".OUT", "w" , stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
cin >> a[i][j];
if(a[i][j] == 'M')
{
vis[i][j] = 1;
sti = i;
stj = j;
}
else vis[i][j] = a[i][j] == 'G' ? 0 : 1;
if(a[i][j] == 'D')
{
hi = i;
hj = j;
}
if(a[i][j] == 'H')
{
s.push_back({{i, j}, 0});
vis[i][j] = 1;
}
}
}
Bfs();
tim[hi][hj] = 1e18;
//cout << checked(1);
int low = 0, high = n, mid;
while(low <= high)
{
mid = (low + high) / 2;
if(checked(mid)) low = mid+1;
else high = mid - 1;
}
cout << high;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |