Cod sursa(job #2554462)

Utilizator Cosmin3105Cosmin Colceru Cosmin3105 Data 22 februarie 2020 22:06:54
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.16 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[4] = {-1, 0, 1, 0};
int dy[4] = { 0, 1, 0,-1};

bool ok(int x, int y)
{
    if(x < 0 || y < 0 || x >= n || y >= m)
        return false;
    if(mat[x][y] == 0)
        return true;
    if(mat[x][y] == -3)
        return true;
    return false;
}

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

        for(int i = 0; i < 4; 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] == -3){
                    lg_drum = mat[x][y];
                    x_fin = x;
                    y_fin = y;
                    break;
                }
                else{
                    mat[x_nou][y_nou] = mat[x][y] + 1;
                    coada.push(make_pair(x_nou,y_nou));
                }
            }
        }
    }
}

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

    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 < 4; 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+1 << " " << j+1 << "\n";

    return 0;
}