Cod sursa(job #867500)

Utilizator Alexghita96Ghita Alexandru Alexghita96 Data 29 ianuarie 2013 19:22:27
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.44 kb
#include <stdio.h>

struct coord
{
    int x, y;
};

void citire(int &n, int &m, int a[101][101], int b[101][101], int &xr, int &yr, int &xj, int &yj)
{
    FILE *F = fopen("rj.in", "r");
    int i, j;
    char c;
    fscanf(F, "%d %d\n", &n, &m);
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= m; j++)
        {
            fscanf(F, "%c", &c);
            if (c == 'X')
                a[i][j] = b[i][j] = -1;
            else
            {
                a[i][j] = b[i][j] = 0;
                if (c == 'R')
                {
                    xr = i;
                    yr = j;
                    a[i][j] = 1;
                    b[i][j] = 0;
                }
                else if (c == 'J')
                {
                    xj = i;
                    yj = j;
                    a[i][j] = 0;
                    b[i][j] = 1;
                }
                else if (c == 10)
                    j--;
            }
        }
    }
    fclose(F);
}

void lee(int n, int m, int a[101][101], int xi, int yi)
{
    int i, tc, tf,
        depx[8] = {-1, -1, 0, 1, 1, 1, 0, -1},
        depy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
    coord queue[10001], crt, test;
    queue[tc = tf = 1].x = xi;
    queue[tf].y = yi;
    while (tc <= tf)
    {
        crt.x = queue[tc].x;
        crt.y = queue[tc].y;
        for (i = 0; i <= 7; i++)
        {
            test.x = crt.x + depx[i];
            test.y = crt.y + depy[i];
            if (test.x >= 1 && test.x <= n && test.y >= 1 && test.y <= m && !a[test.x][test.y])
            {
                a[test.x][test.y] = a[crt.x][crt.y] + 1;
                queue[++tf].x = test.x;
                queue[tf].y = test.y;
            }
        }
        tc++;
    }
}

void match(int n, int m, int a[101][101], int b[101][101])
{
    int i, j, tmin = 10001;
    coord pct;
    for (i = 1; i <= n; i++)
        for (j = 1; j <= m; j++)
        {
            if (a[i][j] == b[i][j] && a[i][j] >= 1 && tmin > a[i][j])
            {
                tmin = a[i][j];
                pct.x = i;
                pct.y = j;
            }
        }
    FILE *F = fopen("rj.out", "w");
    fprintf(F, "%d %d %d", tmin, pct.x, pct.y);
}

int main()
{
    int n, m, a[101][101], b[101][101];
    coord r, j;
    citire(n, m, a, b, r.x, r.y, j.x, j.y);
    lee(n, m, a, r.x, r.y);
    lee(n, m, b, j.x, j.y);
    match(n, m, a, b);
    return 0;
}