제출 #652753

#제출 시각아이디문제언어결과실행 시간메모리
652753chanhchuong123Mecho (IOI09_mecho)C++14
24 / 100
1093 ms65536 KiB
#include <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
  if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
  if (a < b) {a = b; return true;} return false;
}

const int dx[] = {-1, 0, 0, +1};
const int dy[] = {0, -1, +1, 0};
const int N = 808;
int n, s;
char a[N][N];
pair<int, int> st, en;
pair<int, int> d[N][N];

struct node {
  int du, u, v, cnt;
  bool operator< (const node &other) const {
  	return du > other.du;
  }
};

bool check(int tim) {
  for (int i = 1; i <= n; i++)
  	for (int j = 1; j <= n; j++)
  	  d[i][j] = make_pair(1e9, -1);
  priority_queue<node> q;
  d[st.fi][st.se] = {tim, s};
  q.push({tim, st.fi, st.se, s});
  for (int i = 1; i <= n; i++) {
  	for (int j = 1; j <= n; j++) {
  	  if (a[i][j] == 'H') {
  	  	d[i][j] = {0, 0};
  	  	q.push({0, i, j, 0});
  	  }
  	}
  }
  while (q.size()) {
  	int u = q.top().u;
  	int v = q.top().v;
  	int du = q.top().du;
  	int cnt = q.top().cnt;
  	q.pop();
  	if (d[u][v] != make_pair(du, cnt)) continue;
    for (int i = 0; i < 4; i++) {
      int x = u + dx[i], y = v + dy[i];
      if (u < 1 || u > n) continue;
      if (v < 1 || v > n) continue;
      if (a[x][y] == 'T') continue;
      int w = (cnt == 0 || cnt == s ? 1 : 0);
      int _c = (cnt == 0 ? 0 : cnt == s ? 1 : cnt + 1);

      if (d[x][y].fi > d[u][v].fi + w) {
      	d[x][y].fi = d[u][v].fi + w; d[x][y].se = _c;
      	q.push({d[x][y].fi, x, y, d[x][y].se});
      } else if (d[x][y].fi == d[u][v].fi + w) {
      	if (d[x][y].se == 0) {
      	  if (cnt == s - 1) continue;
      	  d[x][y].fi = d[u][v].fi + w; d[x][y].se = _c;
      	  q.push({d[x][y].fi, x, y, d[x][y].se});
      	} else if (d[x][y].se == s && cnt == 0) {
      	  d[x][y].fi = d[u][v].fi + w; d[x][y].se = 0;
      	  q.push({d[x][y].fi, x, y, d[x][y].se});
      	} else if (cnt != 0) {
      	  if (d[x][y].se > _c) {
      	  	d[x][y].se = _c;
      	  	q.push({d[x][y].fi, x, y, d[x][y].se});
      	  }
      	}
      }
    }
  }
  return d[en.fi][en.se].se > 0;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);

  if (fopen(task".inp","r")) {
    freopen(task".inp","r",stdin);
    freopen(task".out","w",stdout);
  }

  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'M')
      	st = make_pair(i, j);
      if (a[i][j] == 'D')
      	en = make_pair(i, j);
    }
  }
  if (check(0) == false) {
    cout << "-1"; return 0;
  }
  int l = 0, r = n * 2 + 100;
  while (r - l > 1) {
    int mid = l + r >> 1;
    if (check(mid)) l = mid;
    else r = mid;
  }
  cout << l;
}

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

mecho.cpp: In function 'int main()':
mecho.cpp:108:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  108 |     int mid = l + r >> 1;
      |               ~~^~~
mecho.cpp:89:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |     freopen(task".inp","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:90:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |     freopen(task".out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...