제출 #954225

#제출 시각아이디문제언어결과실행 시간메모리
954225tmtxangelMecho (IOI09_mecho)C++17
52 / 100
164 ms11600 KiB
#include <bits/stdc++.h>


#define lli long long
#define ld long double
#define tmt angel

using namespace std;
const int maxn = 802;
int vis[maxn][maxn], n, m;
char a[maxn][maxn];
int tim[maxn][maxn];
int mtim[maxn][maxn];
int hi, hj, sti, stj;
int x[4] = {1, -1, 0, 0};
int y[4] = {0, 0, 1, -1};
deque<pair<pair<int, int>, int>> s;
bool flag = false;
int bvis[maxn][maxn];

bool valid_sq(int nx, int ny) {
	return nx >= 0 && nx < n && ny >= 0 && ny < n &&
	       (a[nx][ny] == 'G' || a[nx][ny] == 'M');
}

void gocheck(int i, int j, int cost)
{
    if(i < 1 || j < 1 || i > n || j > n || bvis[i][j] == 1 || a[i][j] == 'D' || a[i][j] == 'T') return;
    bvis[i][j] = 1;
    tim[i][j] = cost;
    s.push_back({{i, j}, cost});
}

void spread(int i, int j, int cost, int st)
{
    //if(vis[hi][hj] == 1) return;
    if(i < 1 || j < 1 || i > n || j > n || vis[i][j] == 1) return;
    if(tim[i][j] - st > cost / m)
    {
        s.push_back({{i, j}, cost});
        vis[i][j] = 1;
        mtim[i][j] = cost;
    }
}

void Bfs()
{
    while(!s.empty())
    {
        int i = s.front().first.first;
        int j = s.front().first.second;
        int c = s.front().second;
        s.pop_front();
        for(int f = 0; f < 4; f++)
        {
            gocheck(i + x[f], j + y[f], c+1);
        }
    }
}

bool checked(int st)
{
     for(int i = 1; i <= n; i++)
    {
      for(int j = 1; j <= n; j++)
      {
        vis[i][j] = a[i][j] == 'G' || a[i][j] == 'D' ? 0 : 1;
        mtim[i][j] = 0;
      }
    }
     s.clear();
      s.push_back({{sti, stj}, 0});
    while(!s.empty())
    {
         int i = s.front().first.first;
        int j = s.front().first.second;
        int c = s.front().second;
        s.pop_front();
        for(int f = 0; f < 4; f++)
        {
            spread(i + x[f], j + y[f], c+1, st);
            //if(vis[hi][hj] == 1) break;
        }
    }
   // if(st == 0) s.push_back({ {sti, stj},{2, 0} });
    for(int f = 0; f < 4; f++)
    {
        int nx = hi + x[f], ny = hj + y[f];
        if(valid_sq(nx, ny) && vis[nx][ny] == 1 && mtim[nx][ny] / m < tim[nx][ny] - st) return true;
    }
    return false;

}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
//   freopen(".INP", "r" , stdin);
//   freopen(".OUT", "w" , stdout);
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
    for(int j = 1; j <= n; j++)
    {
        cin >> a[i][j];
        if(a[i][j] == 'M' )
        {
            vis[i][j] = 1;
            sti = i;
            stj = j;
        }
        else vis[i][j] = a[i][j] == 'G' || a[i][j] == 'D' ? 0 : 1;
        if(a[i][j] == 'D')
        {
            hi = i;
            hj = j;
        }
        if(a[i][j] == 'H')
        {
            s.push_back({{i, j}, 0});
            bvis[i][j] = 1;
        }
    }
}
Bfs();
//s.clear();

 //cout << checked(1);
int low = 0, high = n*n, mid;
while(low <= high)
{
    mid = (low + high) / 2;
    if(tim[sti][stj] <= mid) high = mid-1;
    if(checked(mid)) low = mid +1;
    else high = mid - 1;
}
cout << low-1;
/*for(int i = 1; i <= n; i++)
{
    for(int j = 1; j <= n; j++)
    {
        cout << mtim[i][j] << " ";
    }
    cout << '\n';
}
cout << '\n';

for(int i = 1; i <= n; i++)
{
    for(int j = 1; j <= n; j++)
    {
        cout << tim[i][j] << " ";
    }
    cout << '\n';
}*/



return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...