Cod sursa(job #2665233)

Utilizator sefusefeanuSefeanu Sefu sefusefeanu Data 30 octombrie 2020 13:45:28
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.08 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream f ("rj.in");
ofstream g ("rj.out");

struct coordinate{
    int x;
    int y;
};

int a[101][101];

void lee(int sx, int sy, int ex, int ey, int n, int m)
{
    int dx[] = {-1, 0, 1,-1, 1,-1, 0, 1};
    int dy[] = {-1,-1,-1, 0, 0, 1, 1, 1};

    int i,j;
    coordinate v[10001];

    v[0].x = sx;
    v[0].y = sy;
    int c = 0, l = 0;

    while(c<=l)
    {
        for(i=0;i<8;i++)
            if((v[c].y + dy[i] >= 0 && v[c].y + dy[i] < n) && (v[c].x + dx[i] >= 0 && v[c].x + dx[i] < m))
                if(a[v[c].y + dy[i]][v[c].x + dx[i]] == 0)
                {
                    l++;
                    v[l].x = v[c].x + dx[i];
                    v[l].y = v[c].y + dy[i];
                    a[v[l].y][v[l].x] = a[v[c].y][v[c].x] + 1;

                    if(v[c].y + dy[i] == ey && v[c].x + dx[i] == ex)
                    {
                        c = l+1;
                        break;
                    }
                }
        c++;
    }

    l = (a[ey][ex]+1)/2;

    while(a[ey][ex]!=l)
    {
        for(i=0;i<8;i++)
            if(a[ey+dy[i]][ex+dx[i]] == a[ey][ex] - 1)
            {
                ey+=dy[i];
                ex+=dx[i];
                i = 9;
            }
    }

    g<<a[ey][ex]<<" "<<ey+1<<" "<<ex+1;
}

int main()
{
    int n, m, i, j, xr, yr, xj, yj;
    char s[101];

    f>>n>>m;
    f.get();

    for(i=0;i<n;i++)
    {
        f.getline(s,100);
        for(j=0;j<m;j++)
            switch (s[j])
            {
                case 'X':
                    a[i][j]=-1;
                    break;
                case 'R':
                    a[i][j]= 1;
                    xr = j;
                    yr = i;
                    break;
                case 'J':
                    a[i][j]= 0;
                    xj = j;
                    yj = i;
                    break;
                default:
                    a[i][j]= 0;
            }
    }

    lee(xr, yr, xj, yj, n, m);

    return 0;
}