제출 #394062

#제출 시각아이디문제언어결과실행 시간메모리
394062Valera_GrinenkoMecho (IOI09_mecho)C++17
100 / 100
221 ms6200 KiB
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 803;
char d[N][N];
int b[N][N];
int sx = 0, sy = 0, fx = 0, fy = 0;
int n, s;
int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
char check(int tt) {
  if(b[sx][sy] <= tt * s) return false;
  vector<vector<int> > u(n, vector<int>(n, -1));
  u[sx][sy] = tt * s;
  queue<pair<int, pair<int, int> > > q;
  q.push(mp(tt * s, mp(sx, sy)));
  while(!q.empty()) {
    auto x = q.front(); q.pop();
    int xx = x.se.fi, yy = x.se.se, cd = x.fi;
    if(xx == fx && yy == fy) return true;
    for(int mv = 0; mv < 4; mv++) {
      int i = xx + dx[mv], j = yy + dy[mv];
      if(i >= 0 && i < n && j >= 0 && j < n && u[i][j] == -1 && d[i][j] != 'T') {
        if(i == fx && j == fy) return true;
        if(cd + 1 < b[i][j]) {
          u[i][j] = cd + 1;
          q.push(mp(cd + 1, mp(i, j)));
        }
      }
    }
  }
  return false;
}
int main() {

  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

  cin >> n >> s;

  queue<pair<int, int> > bee;

  for(int i = 0; i < n; i++)
    for(int j = 0; j < n; j++) {
      b[i][j] = -1;
      cin >> d[i][j];
      if(d[i][j] == 'H') { bee.push(mp(i, j)); b[i][j] = 0; }
    }

  while(!bee.empty()) {
    auto x = bee.front(); bee.pop();
    for(int mv = 0; mv < 4; mv++) {
      int i = x.fi + dx[mv], j = x.se + dy[mv];
      if(i >= 0 && i < n && j >= 0 && j < n && b[i][j] == -1 && d[i][j] != 'T' && d[i][j] != 'D') { b[i][j] = b[x.fi][x.se] + s; bee.push(mp(i,  j)); }
    }
  }

  for(int i = 0; i < n; i++) {
    for(int j = 0; j < n; j++) {
      if(d[i][j] == 'D') fx = i, fy = j;
      if(d[i][j] == 'M') sx = i, sy = j;
    }
  }

  if(!check(0)) { cout << -1; return 0; }

  int l = 0, r = n * n;

  while(l < r) {
    int m = (l + r + 1) / 2;
    if(check(m)) l = m;
    else r = m - 1;
  }

  cout << l;

  return 0;
}
/*

*/

컴파일 시 표준 에러 (stderr) 메시지

mecho.cpp:3: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
    3 | #pragma GCC optimization ("unroll-loops")
      |
#Verdict Execution timeMemoryGrader output
Fetching results...