99 lines
2.6 KiB
C++
99 lines
2.6 KiB
C++
#include <iostream>
|
|
#include <list>
|
|
#include <fstream>
|
|
#include <list>
|
|
#include <utility>
|
|
#include <queue>
|
|
|
|
using namespace std;
|
|
|
|
//ifstream input_stream;
|
|
|
|
int shiftX[] = {-1, 1, 0, 0};
|
|
int shiftY[] = {0, 0, -1, 1};
|
|
const int LIMIT = 1000;
|
|
uint16_t cellDistances[LIMIT][LIMIT];
|
|
uint16_t guardDistances[LIMIT][LIMIT];
|
|
char mainMap[LIMIT][LIMIT];
|
|
|
|
int main() {
|
|
// input_stream.open("input.txt");
|
|
|
|
int32_t N, M;
|
|
// input_stream >> N;
|
|
// input_stream >> M;
|
|
cin >> N;
|
|
cin >> M;
|
|
|
|
list<pair<int, int>> guardPositions;
|
|
|
|
for (int y = 0; y < N; y++) {
|
|
for (int x = 0; x < M; x++) {
|
|
//input_stream >> mainMap[y][x];
|
|
cin >> mainMap[y][x];
|
|
if (mainMap[y][x] == 'p') {
|
|
guardPositions.push_back(make_pair(x, y));
|
|
}
|
|
cellDistances[y][x] = UINT16_MAX;
|
|
}
|
|
}
|
|
|
|
|
|
bool visited[N][M];
|
|
for (auto it = guardPositions.begin(); it != guardPositions.end(); it++) {
|
|
pair<int, int> start = *it;
|
|
|
|
for (int y = 0; y < N; y++) {
|
|
for (int x = 0; x < M; x++) {
|
|
visited[y][x] = false;
|
|
guardDistances[y][x] = UINT16_MAX;
|
|
}
|
|
}
|
|
|
|
queue<pair<int, int>> q;
|
|
q.push(start);
|
|
guardDistances[start.second][start.first] = 0;
|
|
cellDistances[start.second][start.first] = 0;
|
|
|
|
while (!q.empty()) {
|
|
pair<int, int> node = q.front();
|
|
|
|
q.pop();
|
|
visited[node.second][node.first] = true;
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
int x = node.first + shiftX[i];
|
|
int y = node.second + shiftY[i];
|
|
|
|
if (x < 0 || x >= M || y < 0 || y >= N || visited[y][x] || mainMap[y][x] == 'b') {
|
|
continue;
|
|
}
|
|
|
|
uint16_t new_distance = guardDistances[node.second][node.first] + 1;
|
|
guardDistances[y][x] = new_distance;
|
|
|
|
if (new_distance < cellDistances[y][x]) {
|
|
cellDistances[y][x] = new_distance;
|
|
}
|
|
q.push(make_pair(x, y));
|
|
}
|
|
}
|
|
}
|
|
|
|
int max_distance = 0;
|
|
for (int y = 0; y < N; y++) {
|
|
for (int x = 0; x < M; x++) {
|
|
// cout << cellDistances[y][x] << " ";
|
|
if (cellDistances[y][x] > max_distance && mainMap[y][x] == 's') {
|
|
max_distance = cellDistances[y][x];
|
|
}
|
|
}
|
|
// cout << endl;
|
|
}
|
|
|
|
cout << max_distance << endl;
|
|
|
|
// input_stream.close();
|
|
return 0;
|
|
}
|