Cod sursa(job #1925623)

Utilizator EduardLatcanEduard Latcan EduardLatcan Data 13 martie 2017 14:43:02
Problema Rj Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 1.93 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define max 110
#define max2 30000
#define poz 8

FILE *fin,*fout;
char init[max][max];
int l, sx[max2], sy[max2], romeo[max][max], julieta[max][max];
int gps1[poz]= {0,1,1,1,0,-1,-1,-1};
int gps2[poz]= {-1,-1,0,1,1,1,0,-1};

void BF(int x, int y, int romeo[][max], int n, int m)
{
    int i, j, cx, cy;
    for (i = 1; i <= n; i++)
        for (j = 0; j < m; j++)
            if(init[i][j] == 'X')
                romeo[i][j] = -1;
            else romeo[i][j] = max2;
    l = 1;
    sx[l] = x;
    sy[l] = y;
    romeo[x][y] = 1;
    for (i = 1; i <= l; i++)
        for (j = 0; j < poz; j++)
        {
            cx = sx[i] + gps1[j];
            cy = sy[i] + gps2[j];
            if ((cx > 0) && (cx <= n) && (cy >= 0) && (cy <= m) && (romeo[sx[i]][sy[i]] + 1 < romeo[cx][cy]))
            {
                l++;
                sx[l] = cx;
                sy[l] = cy;
                romeo[cx][cy] = romeo[sx[i]][sy[i]] + 1;
            }
        }
}

int main()
{
    fin = fopen("rj.in", "r");
    fout = fopen("rj.out", "w");
    int n, m, pozx=0, pozy=0;
    fscanf(fin,"%d %d ", &n, &m);
    int i, j;
    for (i = 1; i <= n; i++)
        fgets(init[i], max, fin);
    for (i = 1; i <= n; i++)
        for (j = 0; j < m; j++)
            if (init[i][j] == 'R')
                BF(i, j, romeo, n, m);
    for (i = 1; i <= n; i++)
        for (j = 0; j < m; j++)
            if (init[i][j] == 'J')
                BF(i, j, julieta, n, m);
    romeo[pozx][pozy]=max2;
    for (i = 1; i <= n; i++)
        for (j = 0; j < m; j++)
            if ((romeo[i][j] != -1) && (romeo[i][j] == julieta[i][j]) && (romeo[i][j] < romeo[pozx][pozy]))
            {
                pozx = i;
                pozy = j;
            }
    fprintf(fout,"%d %d %d\n", romeo[pozx][pozy], pozx, pozy + 1);
    fclose (fin);
    fclose (fout);
    return 0;
}