Submission #713718

# Submission time Handle Problem Language Result Execution time Memory
713718 2023-03-22T22:56:29 Z vjudge1 Mecho (IOI09_mecho) C++17
100 / 100
410 ms 2684 KB
#include "bits/stdc++.h"
using namespace std;

#define ii pair<int,int>
#define iii tuple<int,int,int>

const int ms = 8e2 + 5;

char grid[ms][ms];
bool bees[ms][ms], mecho[ms][ms];
int n, s;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
ii start, fim;
vector<ii> hives;

void step(queue<ii> &q){
  queue<ii> nq;
  while(!q.empty()){
    int x, y;
    tie(x, y) = q.front();
    q.pop();
    if(bees[x][y]) continue;

    for(int i=0; i<4; i++){
        int a = x +dx[i], b = y + dy[i];
        if(a < 0 || a>= n || b < 0 || b>=n || bees[a][b] || grid[a][b] == 'T' ) continue;
        
        if(!mecho[a][b]){
          mecho[a][b] = true;
          nq.push({a, b});
        }
    }
  }
  nq.swap(q);
}

void step_bees(queue<ii> &h){
  queue<ii> new_h;

  while(!h.empty()){
    int x, y;
    tie(x, y) = h.front();
    h.pop();

    for(int i=0; i<4; i++){
      int a = x+dx[i], b = y+dy[i];

      if(a < 0 || a >= n || b <0 || b >=n || grid[a][b] == 'T' || grid[a][b] == 'D') continue;

      if(!bees[a][b]){
        bees[a][b] = true;
        new_h.push({a, b});
      }
    }

  }
  new_h.swap(h);
}


bool test(int mid){
  memset(bees, 0, sizeof bees);
  memset(mecho, 0, sizeof mecho);

  queue<ii> h, m;

  mecho[start.first][start.second] = true;
  m.push(start);

  for(int i=0; i<hives.size(); i++) {
    bees[hives[i].first][hives[i].second] = true;
    h.push(hives[i]);
  }
  
  for(int i=0; i<mid; i++) step_bees(h);

  while(!m.empty()){

    for(int i=0; i<s; i++)step(m);
    
    step_bees(h);
  }

  return mecho[fim.first][fim.second];
}

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

  
  cin >> n >> s;
  

  for(int i=0; i<n; i++){
    for(int j=0; j<n; j++){
      cin >> grid[i][j];
      if(grid[i][j] == 'M') start ={i, j};
      if(grid[i][j] == 'D') fim = {i, j};
      if(grid[i][j] == 'H') hives.push_back({i, j});
    }
  }


  int l=0, r = n*n;
  int ans = -1;
  while(l <= r){
    int mid = (l+r)/2;
    
    if(test(mid)){
      ans = mid;
      l = mid+1;
    }
    else r = mid -1;

  }
  // test(2);

  cout << ans << '\n';


	return 0;
}

Compilation message

mecho.cpp: In function 'bool test(int)':
mecho.cpp:71:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |   for(int i=0; i<hives.size(); i++) {
      |                ~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 296 ms 2464 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 2 ms 1620 KB Output is correct
13 Correct 2 ms 1620 KB Output is correct
14 Correct 2 ms 1620 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 1 ms 1492 KB Output is correct
20 Correct 2 ms 1492 KB Output is correct
21 Correct 2 ms 1620 KB Output is correct
22 Correct 1 ms 1624 KB Output is correct
23 Correct 4 ms 1620 KB Output is correct
24 Correct 2 ms 1620 KB Output is correct
25 Correct 3 ms 1620 KB Output is correct
26 Correct 2 ms 1620 KB Output is correct
27 Correct 3 ms 1620 KB Output is correct
28 Correct 2 ms 1620 KB Output is correct
29 Correct 3 ms 1648 KB Output is correct
30 Correct 3 ms 1620 KB Output is correct
31 Correct 4 ms 1620 KB Output is correct
32 Correct 3 ms 1620 KB Output is correct
33 Correct 70 ms 1844 KB Output is correct
34 Correct 43 ms 1748 KB Output is correct
35 Correct 39 ms 1748 KB Output is correct
36 Correct 85 ms 1876 KB Output is correct
37 Correct 84 ms 1876 KB Output is correct
38 Correct 49 ms 1876 KB Output is correct
39 Correct 116 ms 1928 KB Output is correct
40 Correct 77 ms 1932 KB Output is correct
41 Correct 60 ms 1876 KB Output is correct
42 Correct 148 ms 1876 KB Output is correct
43 Correct 93 ms 1964 KB Output is correct
44 Correct 75 ms 1972 KB Output is correct
45 Correct 175 ms 2008 KB Output is correct
46 Correct 115 ms 2004 KB Output is correct
47 Correct 88 ms 2004 KB Output is correct
48 Correct 194 ms 2044 KB Output is correct
49 Correct 137 ms 2004 KB Output is correct
50 Correct 103 ms 2044 KB Output is correct
51 Correct 277 ms 2084 KB Output is correct
52 Correct 170 ms 2084 KB Output is correct
53 Correct 133 ms 2096 KB Output is correct
54 Correct 321 ms 2132 KB Output is correct
55 Correct 189 ms 2132 KB Output is correct
56 Correct 164 ms 2132 KB Output is correct
57 Correct 331 ms 2132 KB Output is correct
58 Correct 217 ms 2164 KB Output is correct
59 Correct 181 ms 2176 KB Output is correct
60 Correct 410 ms 2200 KB Output is correct
61 Correct 252 ms 2200 KB Output is correct
62 Correct 199 ms 2296 KB Output is correct
63 Correct 278 ms 2212 KB Output is correct
64 Correct 299 ms 2212 KB Output is correct
65 Correct 291 ms 2216 KB Output is correct
66 Correct 294 ms 2128 KB Output is correct
67 Correct 303 ms 2216 KB Output is correct
68 Correct 300 ms 2376 KB Output is correct
69 Correct 272 ms 2260 KB Output is correct
70 Correct 286 ms 2244 KB Output is correct
71 Correct 309 ms 2172 KB Output is correct
72 Correct 327 ms 2264 KB Output is correct
73 Correct 251 ms 2516 KB Output is correct
74 Correct 292 ms 2532 KB Output is correct
75 Correct 327 ms 2684 KB Output is correct
76 Correct 301 ms 2552 KB Output is correct
77 Correct 327 ms 2680 KB Output is correct
78 Correct 276 ms 2516 KB Output is correct
79 Correct 271 ms 2516 KB Output is correct
80 Correct 290 ms 2508 KB Output is correct
81 Correct 279 ms 2516 KB Output is correct
82 Correct 285 ms 2516 KB Output is correct
83 Correct 300 ms 2508 KB Output is correct
84 Correct 277 ms 2444 KB Output is correct
85 Correct 301 ms 2456 KB Output is correct
86 Correct 280 ms 2456 KB Output is correct
87 Correct 290 ms 2456 KB Output is correct
88 Correct 272 ms 2388 KB Output is correct
89 Correct 280 ms 2400 KB Output is correct
90 Correct 278 ms 2392 KB Output is correct
91 Correct 297 ms 2396 KB Output is correct
92 Correct 292 ms 2404 KB Output is correct