#include <bits/stdc++.h>
#define all(v) begin(v), end(v)
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
const int MAXN = 805, MAXS = 1006, infINT = 1e9 + 234;
const int dx[4] = {-1, 0, 0, +1};
const int dy[4] = {0, -1, +1, 0};
template<class X, class Y> bool minimize(X &x, const Y &y){return x > y ? x = y, 1: 0;}
int numSize, maxRange, bee[MAXN][MAXN], mecho[MAXN][MAXN];
char board[MAXN][MAXN];
void input(){
cin >> numSize >> maxRange;
for(int row = 1; row <= numSize; row++)
for(int col = 1; col <= numSize; col++)
cin >> board[row][col];
}
bool isValid(int row, int col){
return 1 <= min(row, col) && max(row, col) <= numSize;
}
void bfsBee(){
#define PUSH(row, col, d) if (minimize(bee[row][col], d)) q.emplace(row, col)
memset(bee, 0x3f, sizeof bee);
queue<ii> q;
for(int row = 1; row <= numSize; row++)
for(int col = 1; col <= numSize; col++) if (board[row][col] == 'H')
PUSH(row, col, 0);
while(q.size()){
int row = q.front().fi, col = q.front().se;
q.pop();
for(int k = 0; k < 4; k++){
int nrow = row + dx[k], ncol = col + dy[k];
if (isValid(nrow, ncol) && (board[nrow][ncol] == 'G' || board[nrow][ncol] == 'M')){
PUSH(nrow, ncol, bee[row][col] + 1);
}
}
}
}
bool bfsMecho(const int &mid){
#define PUSH(row, col, d) if (minimize(mecho[row][col], d)) q.emplace(row, col)
memset(mecho, 0x3f, sizeof mecho);
queue<ii> q;
for(int row = 1; row <= numSize; row++)
for(int col = 1; col <= numSize; col++) if (board[row][col] == 'M' && bee[row][col] > mid)
PUSH(row, col, 0);
while(q.size()){
int row = q.front().fi, col = q.front().se;
q.pop();
for(int k = 0; k < 4; k++){
int nrow = row + dx[k], ncol = col + dy[k];
if (isValid(nrow, ncol) && (board[nrow][ncol] == 'G' || board[nrow][ncol] == 'M' || board[nrow][ncol] == 'D')){
if ((mecho[row][col] + 1) / maxRange < bee[nrow][ncol] - mid)
PUSH(nrow, ncol, mecho[row][col] + 1);
}
}
}
for(int row = 1; row <= numSize; row++)
for(int col = 1; col <= numSize; col++) if (board[row][col] == 'D' && mecho[row][col] < infINT)
return 1;
return 0;
}
bool check(const int &mid){
return bfsMecho(mid);
}
void solve(){
bfsBee();
int l = 0, r = numSize * numSize, m, res = -1;
while(l <= r){
m = (l + r) >> 1;
if (check(m)) res = m, l = m + 1;
else r = m - 1;
}
cout << res << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
input();
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
mecho.cpp:52: warning: "PUSH" redefined
52 | #define PUSH(row, col, d) if (minimize(mecho[row][col], d)) q.emplace(row, col)
|
mecho.cpp:31: note: this is the location of the previous definition
31 | #define PUSH(row, col, d) if (minimize(bee[row][col], d)) q.emplace(row, col)
|
mecho.cpp: In function 'int main()':
mecho.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:99:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
99 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |