Cod sursa(job #2292304)

Utilizator andreisontea01Andrei Sontea andreisontea01 Data 29 noiembrie 2018 12:47:58
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.18 kb
#include <bits/stdc++.h>

using namespace std;

const int MAXN = 180;

bool bloc[MAXN][MAXN];
int lee[MAXN][MAXN];
int dx[] = {0, -1, 0, 1}, dy[] = {-1, 0, 1, 0};
int n, m;
queue<pair<int, int> > q;

#define x first
#define y second

bool ok(int x, int y){
    if(x > 0 && y > 0 && x <= n && y <= n){
        if(!bloc[x][y])
            return 1;
        return 0;
    }
    return 0;
}

int main()
{
    ifstream fin("rj.in");
    ofstream fout("rj.out");
    fin >> n >> m;
    int rx, ry, jx, jy;
    string s;
    getline(fin, s);
    for(int i = 1; i <= n; ++i){
        getline(fin, s);
        for(int j = 0; j < m; ++j){
            if(s[j] == 'X')
                bloc[i][j + 1] = 1;
            if(s[j] == 'R'){
                rx = i;
                ry = j + 1;
            }
            if(s[j] == 'J'){
                jx = i;
                jy = j + 1;
            }
        }
    }
    for(int i = 1; i <= n; ++i){
        for(int j = 1; j <= n; ++j)
            lee[i][j] = 1e9;
    }
    lee[rx][ry] = 1;
    q.push(make_pair(rx, ry));
    while(!q.empty()){
        pair<int, int> cel = q.front();
        q.pop();
        for(int d = 0; d < 4; ++d){
            int nextx = cel.x + dx[d];
            int nexty = cel.y + dy[d];
            if(ok(nextx, nexty)){
                int pas = lee[cel.x][cel.y] + 1;
                if(pas < lee[nextx][nexty]){
                    lee[nextx][nexty] = pas;
                    q.push(make_pair(nextx, nexty));
                }
            }
        }
    }
    lee[jx][jy] = 1;
    q.push(make_pair(jx, jy));
    while(!q.empty()){
        pair<int, int> cel = q.front();
        q.pop();
        for(int d = 0; d < 4; ++d){
            int nextx = cel.x + dx[d];
            int nexty = cel.y + dy[d];
            if(ok(nextx, nexty)){
                int pas = lee[cel.x][cel.y] + 1;
                if(pas == lee[nextx][nexty]){
                    fout << nextx << " " << nexty << " " << lee[nextx][nexty] - 2;
                    return 0;
                }
                if(pas < lee[nextx][nexty]){
                    lee[nextx][nexty] = pas;
                    q.push(make_pair(nextx, nexty));
                }
            }
        }
    }
    return 0;
}