Cod sursa(job #2555887)

Utilizator Cosmin3105Cosmin Colceru Cosmin3105 Data 24 februarie 2020 14:59:39
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.24 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <queue>

using namespace std;

ifstream fin("rj.in");
ofstream fout("rj.out");

int n, m;
char s[105];
int mat[105][105];
int lg_drum;
int x_fin, y_fin;

queue < pair <int,int> > coada;

int dx[8] = {-1,-1, 0, 1, 1, 1, 0,-1};
int dy[8] = { 0, 1, 1, 1, 0,-1,-1,-1};

void citire()
{
    fin >> n >> m;
    fin.get();
    for(int i = 1; i <= n; i++){
        fin.getline(s, m+1);
        for(int j = 0; j < m; j++){
            if(s[j] == 'X')
                mat[i][j+1] = -1;
            else if(s[j] == 'R'){
                coada.push(make_pair(i,j+1));
                mat[i][j+1] = 1;
            }
            else if(s[j] == 'J')
                mat[i][j+1] = -2;
        }
    }
}

void bordare()
{
    for(int i = 0; i <= n+1; i++){
        mat[i][0] = -1;
        mat[i][m+1] = -1;
    }
    for(int i = 0; i <= m+1; i++){
        mat[0][i] = -1;
        mat[n+1][i] = -1;
    }
}

bool ok(int x, int y)
{
    if(mat[x][y] == -2)
        return true;
    if(mat[x][y] != 0)
        return false;
    return true;
}

void lee()
{
    while(lg_drum == 0){
        int x = coada.front().first;
        int y = coada.front().second;
        coada.pop();

        for(int i = 0; i < 8; i++){
            int x_nou = x + dx[i];
            int y_nou = y + dy[i];

            if(ok(x_nou, y_nou)){
                if(mat[x_nou][y_nou] == -2){
                    lg_drum = mat[x][y] + 1;
                    x_fin = x;
                    y_fin = y;
                }
                mat[x_nou][y_nou] = mat[x][y] + 1;
                coada.push(make_pair(x_nou,y_nou));

            }
        }
    }
}

int main()
{
    citire();
    bordare();
    lee();

    int tmin = lg_drum/2 + 1;
    fout << tmin << " ";

    int i = x_fin;
    int j = y_fin;

    while(mat[i][j] != tmin){
        for(int k = 0; k < 8; k++){
            int i_nou = i + dx[k];
            int j_nou = j + dy[k];

            if(mat[i_nou][j_nou] == mat[i][j] - 1){
                i = i_nou;
                j = j_nou;
                break;
            }
        }
    }

    fout << i << " " << j << "\n";

    return 0;

}