#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define DIM 40
#define TRUE 1
#define FALSE 0

void ProveraUnosa(int *x, unsigned z);
void UnesiMatricu(char a[][DIM], int n, int m);
void StampajMatricu(char a[][DIM], int n, int m);
void GdeJeSteva();
void ProsiriVatru();
void PomeriStevu();
void Korak();
void SlobodnihMesta();


char a[DIM][DIM];
int b[DIM][DIM], c[DIM][DIM];
int n = 0, m = 0;
int Steva_i = 0, Steva_j = 0;
int Novo_i = 0, Novo_j = 0;
unsigned broj = 1;
int nastavi;

int main()
{
    printf("Unesite n (broj vrsta matrice [1, %d]): ", DIM);
    ProveraUnosa(&n, DIM);
    printf("Unesite m (broj kolona matrice [1, %d]): ", DIM);
    ProveraUnosa(&m, DIM);

    printf("Unesite matricu (karakteri: '.', 'S', 'X'):\n");
    UnesiMatricu(a, n, m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if(a[i][j]=='S'){
                Steva_i=Novo_i;
                Steva_j=Novo_j;
                break;
            }
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if(a[i][j]=='*') b[i][j] = -1;
            if(a[i][j]=='X') b[i][j] = 0;
            if(a[i][j]=='S') b[i][j] = 0;
            for (int l=0; l<n*m; l++){
                if(a[Novo_i-n][Novo_j]=='.') b[i][j] = n;
                if(a[Novo_i+n][Novo_j]=='.') b[i][j] = n;
                if(a[Novo_i][Novo_j-n]=='.') b[i][j] = n;
                if(a[Novo_i][Novo_j+n]=='.') b[i][j] = n;
            }

        }
    }
    StampajMatricu2(b, n, m);

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if(a[i][j]=='*') c[i][j] = -1;
            if(a[i][j]=='X') c[i][j] = 0;
            if(a[i][j]=='S') c[i][j] = 0;
            for (int l=0; l<n*m; l++){
                if(a[i-n][j]=='.') b[i][j] = n;
                if(a[i+n][j]=='.') b[i][j] = n;
                if(a[i][j-n]=='.') b[i][j] = n;
                if(a[i][j+n]=='.') b[i][j] = n;
            }

        }
    }
    StampajMatricu2(b, n, m);

    /*Nisam zavrsio, ali mi je pristup bio sledeci: napravim dve matrice, jednu za koliko koraka je Stevi potrebno da dodje do nekog slobodnog polja (na to
    mesto napise broj koraka),
    a drugu koliko je vatri potrebno da dodje to tog polja (na isti nacin). Na zidove stavi npr. -1   Zatim uporedim te dve matrice i ako ce Steva
    stici na odredjeno polje pre vatre brojac se inkrementuje*/

    return 0;
}

void ProveraUnosa(int *x, unsigned z)
{
    do
    {
        printf("Unos>> ");
        scanf("%d", x);
        if (*x < 1 || *x > z)
            printf("Doslo je do greske, pokusajte ponovo...\n");
    } while (*x < 1 || *x > z);
}

void UnesiMatricu(char a[][DIM], int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            scanf(" %c", &a[i][j]);
        }
    }
}

void StampajMatricu(char a[][DIM], int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%c", a[i][j]);
        }
        printf("\n");
    }
}

void StampajMatricu2(int a[][DIM], int n, int m)
{
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            printf("%3d", a[i][j]);
        }
        printf("\n");
    }
}