Pagini recente » Cod sursa (job #3277746) | Cod sursa (job #1203764) | Cod sursa (job #2579078)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
ifstream f ("barbar.in");
ofstream g ("barbar.out");
int matrice[1000][1000];
int n,m;
int startx,starty,stopx,stopy;
int di[]={1,0,-1,0};
int dj[]={0,1,0,-1};
char x;
queue < pair < int, int > > coada;
bool testare(int x, int y)
{
if(x<1||y<1||x>n||y>m)
{
return false;
}
if(matrice[x][y]==-1)
{
return false;
}
return true;
}
void lee()
{
int i,j,k,nou_i,nou_j;
matrice[startx][starty]=1;
coada.push(make_pair(startx,starty));
while(!coada.empty())
{
i=coada.front().first;
j=coada.front().second;
coada.pop();
for(k=0; k<=3; k++)
{
nou_i=i+di[k];
nou_j=j+dj[k];
if(testare(nou_i,nou_j)==true&&matrice[nou_i][nou_j]<1)
{
matrice[nou_i][nou_j]=matrice[i][j]+1;
coada.push(make_pair(nou_i,nou_j));
}
}
}
}
void algcit()
{
//dragoni - D
//pereti - *
//libere - .
//punct de plecare - I
//punct de iesire - 0
f>>n>>m;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
f>>x;
if(x=='.')
{
matrice[i][j]=0;
}
if(x=='D')
{
matrice[i][j]=-1;
}
if(x=='*')
{
matrice[i][j]=-1;
}
if(x=='I')
{
matrice[i][j]=1;
startx=i;
starty=j;
}
if(x=='O')
{
matrice[i][j]=2;
stopx=i;
stopy=j;
}
}
}
}
int main()
{
algcit();
lee();
g<<matrice[stopx][stopy];
return 0;
}