답안 #386527

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
386527 2021-04-06T18:24:20 Z tushar_2658 Mecho (IOI09_mecho) C++14
57 / 100
285 ms 8940 KB
#include "bits/stdc++.h"
using namespace std;

const int maxn = 805;

char s[maxn][maxn];
int n, S; 
int vis[maxn][maxn], dis[maxn][maxn], dis1[maxn][maxn];
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int sx, sy, fx, fy;

void precalc(){
  queue<pair<int, int>> q;
  for(int i = 1; i <= n; ++i){
    for(int j = 1; j <= n; ++j){
      dis[i][j] = n * n;
      vis[i][j] = 0;
      if(s[i][j] == 'H'){
        vis[i][j] = 1;
        q.push({i, j});
        dis[i][j] = 0;
      }
    }
  }
  while(!q.empty()){
    pair<int, int> src = q.front();
    q.pop();   
    for(int i = 0; i < 4; ++i){
      int newx = src.first + dx[i], newy = src.second + dy[i];
      if(newx <= n && newx > 0 && newy <= n && newy > 0){
        if(vis[newx][newy] == 0 && s[newx][newy] != 'T'){
          vis[newx][newy] = 1;
          dis[newx][newy] = min(dis[newx][newy], dis[src.first][src.second] + 1);
          q.push({newx, newy});
        }
      }
    }
  }
}

bool can(int x){
  queue<pair<int, int>> q;
  for(int i = 1; i <= n; ++i){
    for(int j = 1; j <= n; ++j){
      dis1[i][j] = n * n;
      if(s[i][j] == 'D'){
        vis[i][j] = 0;
      }else if(s[i][j] == 'T')vis[i][j] = 1;
      else if(s[i][j] == 'H'){
        vis[i][j] = 1;
      }else if(s[i][j] == 'M'){
        vis[i][j] = 1;
        dis1[i][j] = 0;
        q.push({i, j});
      }else {
        vis[i][j] = 0;
      }
    }
  }
  while(!q.empty()){
    pair<int, int> src = q.front();
    q.pop();
    for(int i = 0; i < 4; ++i){
      int newx = src.first + dx[i], newy = src.second + dy[i];
      if(newx <= n && newx > 0 && newy <= n && newy > 0){
        if(s[newx][newy] != 'T' && !vis[newx][newy]){
          int cost = (dis1[src.first][src.second] + 1) / S + x; 
          if(cost < dis[newx][newy]){
            vis[newx][newy] = 1;
            dis1[newx][newy] = dis1[src.first][src.second] + 1;
            q.push({newx, newy});
          }
        }
      }
    }
  }
  if(vis[fx][fy])return 1;
  return 0;
}

int main(int argc, char const *argv[])
{
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> S;
  for(int i = 1; i <= n; ++i){
    for(int j = 1; j <= n; ++j){
      cin >> s[i][j];
      if(s[i][j] == 'M'){
        sx = i, sy = j;
      }else if(s[i][j] == 'D'){
        fx = i, fy = j;
      }
    }
  }
  precalc();
  int lo = 1, hi = n * n, ans = 0;
  while(lo <= hi){
    int mid = (lo + hi) >> 1;
    if(can(mid)){
      ans = mid;
      lo = mid + 1;
    }else {
      hi = mid - 1;
    }
  }
  cout << ans << '\n';

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Incorrect 1 ms 364 KB Output isn't correct
3 Correct 1 ms 364 KB Output is correct
4 Incorrect 1 ms 364 KB Output isn't correct
5 Correct 1 ms 492 KB Output is correct
6 Incorrect 1 ms 492 KB Output isn't correct
7 Incorrect 126 ms 8820 KB Output isn't correct
8 Incorrect 1 ms 492 KB Output isn't correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 492 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Incorrect 1 ms 1004 KB Output isn't correct
13 Incorrect 2 ms 876 KB Output isn't correct
14 Incorrect 2 ms 1004 KB Output isn't correct
15 Correct 1 ms 620 KB Output is correct
16 Correct 1 ms 620 KB Output is correct
17 Correct 1 ms 620 KB Output is correct
18 Correct 1 ms 620 KB Output is correct
19 Correct 1 ms 620 KB Output is correct
20 Correct 2 ms 620 KB Output is correct
21 Correct 1 ms 748 KB Output is correct
22 Correct 1 ms 748 KB Output is correct
23 Correct 1 ms 876 KB Output is correct
24 Correct 2 ms 876 KB Output is correct
25 Correct 2 ms 1004 KB Output is correct
26 Correct 2 ms 1004 KB Output is correct
27 Correct 1 ms 1004 KB Output is correct
28 Correct 2 ms 1004 KB Output is correct
29 Correct 1 ms 1132 KB Output is correct
30 Correct 2 ms 1132 KB Output is correct
31 Correct 2 ms 1132 KB Output is correct
32 Correct 2 ms 1132 KB Output is correct
33 Correct 10 ms 3948 KB Output is correct
34 Correct 10 ms 3948 KB Output is correct
35 Correct 31 ms 4076 KB Output is correct
36 Correct 12 ms 4460 KB Output is correct
37 Correct 13 ms 4460 KB Output is correct
38 Correct 39 ms 4460 KB Output is correct
39 Correct 15 ms 4972 KB Output is correct
40 Correct 16 ms 4972 KB Output is correct
41 Correct 53 ms 4972 KB Output is correct
42 Correct 18 ms 5484 KB Output is correct
43 Correct 22 ms 5484 KB Output is correct
44 Correct 65 ms 5484 KB Output is correct
45 Correct 22 ms 5996 KB Output is correct
46 Correct 23 ms 5996 KB Output is correct
47 Correct 75 ms 5996 KB Output is correct
48 Correct 25 ms 6508 KB Output is correct
49 Correct 27 ms 6528 KB Output is correct
50 Correct 98 ms 6764 KB Output is correct
51 Correct 32 ms 7040 KB Output is correct
52 Correct 32 ms 7020 KB Output is correct
53 Correct 120 ms 7080 KB Output is correct
54 Correct 34 ms 7532 KB Output is correct
55 Correct 36 ms 7532 KB Output is correct
56 Correct 140 ms 7660 KB Output is correct
57 Correct 38 ms 8044 KB Output is correct
58 Correct 41 ms 8044 KB Output is correct
59 Correct 156 ms 8044 KB Output is correct
60 Correct 43 ms 8556 KB Output is correct
61 Correct 48 ms 8556 KB Output is correct
62 Correct 188 ms 8576 KB Output is correct
63 Correct 179 ms 8684 KB Output is correct
64 Correct 285 ms 8684 KB Output is correct
65 Correct 231 ms 8612 KB Output is correct
66 Correct 214 ms 8556 KB Output is correct
67 Incorrect 201 ms 8556 KB Output isn't correct
68 Correct 128 ms 8556 KB Output is correct
69 Correct 122 ms 8556 KB Output is correct
70 Correct 126 ms 8684 KB Output is correct
71 Correct 119 ms 8684 KB Output is correct
72 Incorrect 110 ms 8556 KB Output isn't correct
73 Incorrect 65 ms 8812 KB Output isn't correct
74 Correct 96 ms 8940 KB Output is correct
75 Correct 120 ms 8812 KB Output is correct
76 Correct 107 ms 8812 KB Output is correct
77 Correct 113 ms 8812 KB Output is correct
78 Incorrect 119 ms 8812 KB Output isn't correct
79 Correct 95 ms 8812 KB Output is correct
80 Correct 98 ms 8812 KB Output is correct
81 Correct 109 ms 8812 KB Output is correct
82 Correct 98 ms 8812 KB Output is correct
83 Correct 122 ms 8940 KB Output is correct
84 Correct 118 ms 8812 KB Output is correct
85 Correct 121 ms 8812 KB Output is correct
86 Correct 120 ms 8812 KB Output is correct
87 Correct 114 ms 8812 KB Output is correct
88 Correct 120 ms 8684 KB Output is correct
89 Correct 139 ms 8684 KB Output is correct
90 Correct 138 ms 8812 KB Output is correct
91 Correct 122 ms 8684 KB Output is correct
92 Correct 126 ms 8684 KB Output is correct