Cod sursa(job #1117574)

Utilizator IonSebastianIon Sebastian IonSebastian Data 23 februarie 2014 17:41:27
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.88 kb
#include <fstream>
#include <queue>

using namespace std;

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

const int MAX_N = 100, MAX_M = 100;
const int dlin[] = {-1,-1,-1,0,0,1,1,1}, dcol[] = {-1,0,1,-1,1,-1,0,1};

struct poz {
    int lin;
    int col;
};

int n, m, ifin, jfin, fin, a[MAX_N+2][MAX_M+2];

bool gasit;

queue <poz> q;

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

void citire(){
    char c;
    int i, j;
    poz p;
    for(i = 1; i <= n; i++){
        for(j = 1; j <= m; j++){
            in.get(c);
            if(c == '\n' ){
                if(j != m){
                    in.get(c);
                }
            }
            if(c == 'X'){
                a[i][j] = -1;
            } else {
                if(c == 'R' || c == 'J'){
                    a[i][j] = 1;
                    p.lin = i;
                    p.col = j;
                    q.push(p);
                }
            }
        }
    }
}

void lee(){
    poz x,y;
    int i;
    while(!q.empty()){
        x = q.front();
        q.pop();
        for(i = 0; i < 8; i++){
            y.lin = x.lin + dlin[i];
            y.col = x.col + dcol[i];
            if(a[y.lin][y.col] == 0){
                q.push(y);
                a[y.lin][y.col] = 1 + a[x.lin][x.col];
            } else {
                if(a[y.lin][y.col] == 1 + a[x.lin][x.col] && !gasit){
                    ifin = y.lin;
                    jfin = y.col;
                    fin = a[y.lin][y.col];
                    gasit = true;
                }
            }
        }
    }
}

int main()
{
    in >> n >> m;
    bordare();
    citire();
    lee();
    out << fin << " " << ifin << " " << jfin;
    return 0;
}