#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
using std::mt19937_64;
using std::random_device;
using std::uniform_int_distribution;
#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define pqb priority_queue<int>
#define pqs priority_queue<int,vi,greater<int> >
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x,y) fixed<<setprecision(y)<<x
#define mk(arr,n,type) type *arr=new type[n];
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> pbds;
void cpc()
{
#ifndef ONLINE_JUDGE
freopen("atlarge.in", "r", stdin);
freopen("atlarge.out", "w", stdout);
#endif
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int exponent(int x, int n, int m) {
if(x == 0 and n == 0) return 1;
x %= m;
int res = 1;
while (n > 0) {
if (n % 2 == 1) { res = res * x % m; }
x = x * x % m;
n /= 2;
}
return res;
}
int gcd(int a,int b){
if(a == 0) return b;
return gcd(b%a,a);
}
bool check(int x,int y,int n,int m){
if(x < 0 or y < 0 or x >= n or y >= m) return false;
return true;
}
bool find(vector<vector<char>> &v,int n,int mid,int &steps){
queue<pii> q;
vector<vector<bool>> visited(n,vector<bool>(n,false));
pii st,en;
for(int i = 0 ; i < n; i++){
for(int j = 0 ; j < n; j++){
if(v[i][j] == 'H') q.push({i,j});
if(v[i][j] == 'D') en = {i,j};
if(v[i][j] == 'M') st = {i,j};
}
}
int ct = 0;
while(mid--){
int si = q.size();
for(int i = 0 ; i < si; i++){
int x = q.front().ff;
int y = q.front().ss;
visited[x][y] = true;
q.pop();
if(visited[en.ff][en.ss]) return false;
for(int i = 0 ; i < 4; i++){
int l = x+dx[i];
int m = y+dy[i];
if(check(l,m,n,n) and !visited[l][m] and (v[l][m] == 'G' or v[l][m] == 'M' or v[l][m] == 'D')){
q.push({l,m});
if(visited[en.ff][en.ss]) return false;
visited[l][m] = true;
}
}
}
}
queue<pii> q2;
q2.push(st);
vector<vector<bool>> visited2(n,vector<bool>(n,false));
while(true){
int tim = steps;
while(tim--){
int si = q2.size();
for(int i = 0 ; i < si; i++){
int x = q2.front().ff;
int y = q2.front().ss;
q2.pop();
if(visited[x][y]) continue;
visited2[x][y] = true;
if(visited2[en.ff][en.ss]) return true;
for(int i = 0 ; i < 4; i++){
int l = x+dx[i];
int m = y+dy[i];
if(check(l,m,n,n) and !visited[l][m] and !visited2[l][m] and (v[l][m] == 'G' or v[l][m] == 'D')){
q2.push({l,m});
visited2[l][m] = true;
if(visited2[en.ff][en.ss]) return true;
}
}
}
}
int si = q.size();
for(int i = 0 ; i < si; i++){
int x = q.front().ff;
int y = q.front().ss;
visited[x][y] = true;
q.pop();
if(visited[en.ff][en.ss]) return false;
for(int i = 0 ; i < 4; i++){
int l = x+dx[i];
int m = y+dy[i];
if(check(l,m,n,n) and !visited[l][m] and (v[l][m] == 'G' or v[l][m] == 'M' or v[l][m] == 'D')){
q.push({l,m});
if(visited[en.ff][en.ss]) return false;
visited[l][m] = true;
}
}
}
}
return false;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
// cpc();
int t=1;
// cin >> t;
while(t--){
int n,s;
cin >> n >> s;
vector<vector<char>> v(n,vector<char>(n));
for(int i = 0 ; i < n; i++){
for(int j = 0 ;j < n; j++){
cin >> v[i][j];
}
}
int l = 0,r = n*n;
int ans = -1;
while(l <= r){
int mid = (l+r)/2;
if(find(v,n,mid,s)){
ans = mid;
l = mid+1;
}
else r = mid-1;
}
cout<<ans<<endl;
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
mecho.cpp: In function 'void cpc()':
mecho.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("atlarge.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen("atlarge.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |