Cod sursa(job #2579080)

Utilizator AlexAkaJustinDiaconescu Andrei Alexandru AlexAkaJustin Data 11 martie 2020 22:03:24
Problema Barbar Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.69 kb
#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<0||y<0||x>n-1||y>m-1)
    {
        return false;
    }
    if(matrice[x][y]==-1)
    {
        return false;
    }
    return true;
}
void lee()
{
    int i,j,k,nou_i,nou_j;
    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=0; i<=n-1; i++)
{
    for(int j=0; j<=m-1; 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;
}