Pagini recente » Cod sursa (job #1266010) | Cod sursa (job #2204291)
#include <fstream>
#include <queue>
#define mp make_pair
#define ZID -1
#define DRAGON 2
using namespace std;
ifstream fin("barbar.in");
ofstream fout("barbar.out");
void lee();
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
int n, m, minim, dist[1005][1005], a[1005][1005];
pair<int,int> start, stop;
queue<pair<int,int> > Q;
class mycomparison
{
public:
bool operator() (const pair<int,int>& lhs, const pair<int,int>&rhs) const
{
return (dist[lhs.first][lhs.second]<dist[rhs.first][rhs.second]);
}
};
priority_queue<pair<int,int>,vector<pair<int,int> >, mycomparison> H;
int main()
{
char c;
int i, j, l9, c9;
pair<int,int> aux;
fin >> n >> m;
for (i=1;i<=n;i++)
for (j=1;j<=m;j++)
{
fin >> c;
if (c == '*')
a[i][j] = ZID;
else if (c == 'D')
{
a[i][j] = DRAGON;
Q.push(mp(i,j));
}
else if (c == 'I')
start = mp(i,j);
else if (c == 'O')
stop = mp(i,j);
}
lee();
H.push(start);
minim = 5000;
while (1 && !H.empty())
{
aux = H.top();
minim = min(minim,dist[aux.first][aux.second]);
if (aux == stop)
break;
H.pop();
for (i=0;i<4;i++)
{
l9 = aux.first+dx[i];
c9 = aux.second+dy[i];
if (!a[l9][c9])
{
H.push(mp(l9,c9));
a[l9][c9] = 1;
}
}
}
if (aux == stop)
fout << minim << '\n';
else fout << -1 << '\n';
return 0;
}
void lee()
{
int i, l9, c9;
pair<int,int> aux;
for (i=0;i<=n+1;i++)
a[i][0] = a[i][m+1] = ZID;
for (i=0;i<=m+1;i++)
a[0][i] = a[n+1][i] = ZID;
while (!Q.empty())
{
aux = Q.front();
Q.pop();
for (i=0;i<4;i++)
{
l9 = aux.first+dx[i];
c9 = aux.second+dy[i];
if (!dist[l9][c9] && !a[l9][c9])
{
dist[l9][c9] = 1+dist[aux.first][aux.second];
Q.push(mp(l9,c9));
}
}
}
}