Pagini recente » Cod sursa (job #2266625) | Cod sursa (job #1949254) | Cod sursa (job #3257491) | Cod sursa (job #1031537) | Cod sursa (job #2498057)
#include <fstream>
#include <queue>
using namespace std;
ifstream in("barbar.in");
ofstream out("barbar.out");
int mat[1005][1005];
int r, c, nr_drag;
int dx[] = { 0,0,1,-1 };
int dy[] = { -1,1,0,0 };
pair<int, int> coord_drag[1005], coord_start, coord_final;
queue<pair<int, int> > coada;
void captusire()
{
for (int i = 0; i <= r + 1; i++)
mat[i][0] = mat[i][c + 1] = -1;
for (int j = 0; j <= c + 1; j++)
mat[0][j] = mat[r + 1][j] = -1;
}
void alg_Lee()
{
while (!coada.empty())
{
int okx = coada.front().first;
int oky = coada.front().second;
coada.pop();
for (int i = 0; i < 4; i++)
{
int x2 = okx + dx[i];
int y2 = oky + dy[i];
if (mat[x2][y2] > mat[okx][oky] + 1 || mat[x2][y2] == 0)
{
mat[x2][y2] = mat[okx][oky] + 1;
coada.push(make_pair(x2, y2));
}
}
}
}
int main()
{
in >> r >> c;
captusire();
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
{
char x;
in >> x;
if (x == '.')
mat[i][j] = 0;
else if (x == '*')
mat[i][j] = -1;
else if (x == 'D')
{
mat[i][j] = 0;
coord_drag[++nr_drag] = make_pair(i, j);
}
else if (x == 'I')
{
mat[i][j] = 0;
coord_start = make_pair(i, j);
}
else if (x == 'O')
{
mat[i][j] = 0;
coord_final = make_pair(i, j);
}
}
for (int i = 1; i <= nr_drag; i++)
{
coada.push(make_pair(coord_drag[i].first, coord_drag[i].second));
}
alg_Lee();
for (int i = 1; i <= nr_drag; i++)
{
mat[coord_drag[i].first][coord_drag[i].second] = 0;
}
out<<-1;
in.close();
out.close();
return 0;
}