#include <bits/stdc++.h>
using namespace std;
#define task "main"
#define F first
#define S second
#define ii pair<int, int>
#define il pair<int, long long>
#define li pair<long long, int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define valid(x, y) ((x) >= 1 && (x) <= n && (y) >= 1 && (y) <= n)
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
const int MAX_N = (int)803;
const int INF = (int)1e9 + 1408;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
int n, speed;
string forest[MAX_N];
int sX, sY, tX, tY;
int distBee[MAX_N][MAX_N], distBear[MAX_N][MAX_N];
void bfs(int dist[MAX_N][MAX_N], int val) {
queue<ii> q;
FOR(i, 1, n) FOR(j, 1, n) if (dist[i][j] == 0)
q.push({i, j});
while (!q.empty()) {
int x = q.front().F, y = q.front().S;
q.pop();
if (val >= 0 && dist[x][y] / speed + val >= distBee[x][y])
continue;
FOR(i, 0, 3) {
int newX = x + dx[i], newY = y + dy[i];
if (!valid(newX, newY)) continue;
if ((val >= 0 || forest[newX][newY] != 'D') && forest[newX][newY] != 'T' && dist[newX][newY] >= INF) {
dist[newX][newY] = dist[x][y] + 1;
q.push({newX, newY});
}
}
}
}
void solve() {
cin >> n >> speed;
FOR(i, 1, n) {
cin >> forest[i];
forest[i] = "@" + forest[i];
}
FOR(i, 1, n) FOR(j, 1, n) {
if (forest[i][j] == 'M') sX = i, sY = j;
else if (forest[i][j] == 'D') tX = i, tY = j;
}
memset(distBee, 0x3f, sizeof(distBee));
FOR(i, 1, n) FOR(j, 1, n) if (forest[i][j] == 'H')
distBee[i][j] = 0;
bfs(distBee, -1);
int l = 0, r = n * n, res = -1;
while (l <= r) {
int mid = (l + r) >> 1;
memset(distBear, 0x3f, sizeof(distBear));
distBear[sX][sY] = 0;
bfs(distBear, mid);
if (distBear[tX][tY] < INF) l = (res = mid) + 1;
else r = mid - 1;
}
cout << res;
}
int32_t main() {
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
bool multitest = 0;
int numTest = 1;
if (multitest) cin >> numTest;
while (numTest--) {
solve();
}
return 0;
}
/* Lak lu theo dieu nhac!!!! */
컴파일 시 표준 에러 (stderr) 메시지
mecho.cpp: In function 'int32_t main()':
mecho.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
101 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:102:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |