Cod sursa(job #483051)

Utilizator andra23Laura Draghici andra23 Data 6 septembrie 2010 19:10:50
Problema Sate Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.23 kb
#include<cstdio>
#include<fstream>
#define nmax 260

using namespace std;

int a[nmax][nmax];
pair<int, int> c[nmax*nmax];
int dirx[] = {0, -1, 0, 1};
int diry[] = {-1, 0, 1, 0};

int main(){
    freopen("muzeu.in", "r", stdin);
    ofstream g("muzeu.out");
    int n, i, j, p=1, u=0, x, y, x1, y1;
    char s[nmax];
    scanf("%d ", &n);
    for (i = 0; i < n; i++){
        fgets(s, nmax, stdin);
        for (j = 0; j < n; j++) 
            if (s[j] == 'P'){
                a[i][j] = 0;
                u++;
                c[u] = make_pair(i, j);
            }
            else 
                if (s[j] == '#')
                    a[i][j] = -2;
                else 
                    a[i][j] = -1;
    }
    
    while (p <= u){
        x = c[p].first;
        y = c[p].second;
        p++;
        for (i = 0; i <= 3; i++){
            x1 = x+dirx[i];
            y1 = y+diry[i];
            if (a[x1][y1] == -1){
                a[x1][y1] = a[x][y]+1;
                u++;
                c[u] = make_pair(x1, y1);    
            }
        }
    }
    
    for (i = 0; i < n; i++){
        for (j = 0; j < n; j++)
            g<<a[i][j]<<" ";
        g<<'\n';
    }
        
    return 0;
}