# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
841189 | daoda | Mecho (IOI09_mecho) | C++17 | 299 ms | 3280 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>
using namespace std;
#define FOR(a, b) for(ll i = a; i < b; i++)
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
typedef long long int ll;
typedef pair<ll, ll> pll; // comments that are mixed
typedef vector<ll> vll; // in with code are placed
typedef vector<pll> vpll; // on the right side
typedef vector<bool> vb;
struct Object {
ll row,col;
char type='H';
};
ll n,s;
vector<vector<char>> grid, stat;
vll dr={0,0,-1,1}, dc={-1, 1, 0, 0};
vector<Object> hives;
Object bear, home;
void bfs(queue<Object>&bfs_list,function<bool(char&,char&)> cond,char rep,ll iters = 1){
FOR(0, iters) {
ll sz=bfs_list.size();
FOR(0, sz) {
Object cur=bfs_list.front();
bfs_list.pop();
FOR(0, 4) {
char& cell = stat[cur.row + dr[i]][cur.col + dc[i]];
if(cond(stat[cur.row][cur.col], cell)) {
cell=rep;
bfs_list.push((Object){.row=cur.row+dr[i],.col=cur.col+dc[i]});
}
}
}
}
}
bool cond1(char& a,char& b) { return (b == 'G' || b == 'M'); }
bool cond2(char& a,char& b) { return (a == 'M' && (b == 'G' || b== 'D')); }
bool f(ll max_t) {
stat=grid;
queue<Object> bfs_list1, bfs_list2;
FOR(0, hives.size()) bfs_list1.push(hives[i]);
bfs_list2.push(bear);
bfs(bfs_list1,cond1,'H',max_t);
while(bfs_list2.size()) {
bfs(bfs_list2,cond2,'M', s);
if(stat[home.row][home.col] == 'M') return true;
bfs(bfs_list1,cond1,'H', 1);
}
return false;
}
int main() {
fast
cin >> n >> s;
grid.resize(n+2, vector<char>(n+2, 'T'));
FOR(1, n+1) {
for(ll i2=1; i2 < n+1; i2++) {
cin >> grid[i][i2];
if(grid[i][i2] == 'H') hives.push_back((Object){.row=i,.col=i2});
if(grid[i][i2] == 'M') bear = (Object){.row=i,.col=i2,.type='M'};
if(grid[i][i2] == 'D') home = (Object){.row=i,.col=i2,.type='D'};
}
}
ll lb=-1, upb=n*n;
while(lb < upb) {
ll mid=(lb + upb + 1) /2 ;
if(f(mid)) lb=mid;
else upb = mid - 1 ;
}
cout << lb;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |