Pagini recente » Cod sursa (job #604381) | Cod sursa (job #920406) | Cod sursa (job #1823022) | Cod sursa (job #3177605) | Cod sursa (job #3278415)
#include <fstream>
#include <iostream>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("date.in");
ofstream fout("date.out");
int n, m, t;
int tx, ty;
vector<pair<int, int>> p;
queue<pair<int, int>> q;
char mat[501][501];
int price[501][501];
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
priority_queue<int, vector<int>, greater<int>()> pq;
inline bool ok(int &nx, int &ny)
{
return nx >= 0 && ny >= 0 && nx < n && ny < m && mat[nx][ny] != '#' && price[nx][ny] == 0;
}
int lee()
{
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (ok(nx, ny))
{
price[nx][ny] = max(price[nx][ny], price[x][y] + 1);
if (mat[nx][ny] == 'P')
{
for (int i = 0; i < p.size(); i++)
{
if (p[i].first != nx && p[i].second != ny)
{
q.push({p[i].first, p[i].second});
price[p[i].first][p[i].second] = price[nx][ny];
}
}
}
// if (nx == tx && ny == ty)
// {
// return price[nx][ny] - 1;
// }
q.push({nx, ny});
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (price[i][j] != INT_MAX)
cout << price[i][j] << " ";
else
cout << 'x' << " ";
}
cout << '\n';
}
cout << '\n';
cout << "*" << q.front().first << " " << q.front().second << '\n';
}
return -1;
}
int main()
{
fin >> n >> m >> t;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
price[i][j] = 0;
fin >> mat[i][j];
if (mat[i][j] == 'W')
{
price[i][j] = 1;
q.push({i, j});
}
if (mat[i][j] == 'E')
{
tx = i;
ty = j;
}
if (mat[i][j] == 'P')
{
p.push_back({i, j});
}
}
}
fout << lee() << '\n';
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (price[i][j] != INT_MAX)
fout << price[i][j] << " ";
else
fout << 'x' << " ";
}
fout << '\n';
}
return 0;
}