Cod sursa(job #887240)

Utilizator lucianRRuscanu Lucian lucianR Data 23 februarie 2013 17:19:23
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.96 kb
#include <iostream>
#include <fstream>

using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");


string line;
short int n, m, x1, y1, x2, y2, mat[100][100], xc, yc, qx[10000], qy[10000], s, f, xx, yy;
bool visited[100][100];
const short int dx[]={0, -1, -1, -1, 0, 1, 1, 1};
const short int dy[]={-1, -1, 0, 1, 1, 1, 0, -1};

void read() {
    in>>n>>m; getline(in,line);
    for(short int i=0; i<n; i++) {
        getline(in,line);
        for(short int j=0; j<m; j++) {
            if(line[j]=='R') {
                x1=i; y1=j;
                mat[i][j]=1;
            }
            if(line[j]=='J') {
                x2=i; y2=j;
                mat[i][j]=1;
            }
            if(line[j]==' ') {
                mat[i][j]=0;
            }
            if(line[j]=='X') {
                mat[i][j]=-1;
            }
        }
    }
}

bool good(short int row, short int col) {
    return (row>=0) && (row<n) && (col>=0) && (col<m) && (mat[row][col]!=-1);
}

short int road() {
    while(s<f) {
        xc=qx[s];
        yc=qy[s];
        visited[xc][yc]=1;
        for(short int i=0; i<8; i++) {
            if(good(xc+dx[i],yc+dy[i]) && visited[xc+dx[i]][yc+dy[i]]==0) {
                qx[f]=xc+dx[i];
                qy[f]=yc+dy[i];
                mat[xc+dx[i]][yc+dy[i]]=mat[xc][yc]+1;
                f++;
            }
            else
                if(good(xc+dx[i],yc+dy[i]) && visited[xc+dx[i]][yc+dy[i]] && mat[xc+dx[i]][yc+dy[i]]==mat[xc][yc]+1) {
                    xx=xc+dx[i];
                    yy=yc+dy[i];
                }
        }
        s++;
    }
}

int main()
{
    read();
    qx[0]=x1;
    qy[0]=y1;
    qx[1]=x2;
    qy[1]=y2;
    f=2;
    road();
    /*for(short int i=0; i<n; i++) {
        for(short int j=0; j<m; j++)
            out<<mat[i][j];
        out<<"\n";
    }*/
    out<<mat[xx][yy]-1<<" "<<xx+1<<" "<<yy+1;
    in.close();
    out.close();
    return 0;
}