Cod sursa(job #1514962)

Utilizator istrate.cristianIstrate Cristian istrate.cristian Data 31 octombrie 2015 21:12:29
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <fstream>
#include <queue>

using namespace std;

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

const short dx[] = {-1,-1,-1,0,1,1,1,0}, dy[] = {-1,0,1,1,1,0,-1,-1};
int mat[105][105],mat1[105][105],n,m,x1,y1,x2,y2,Min=300;
char character[101];
queue < pair<int,int> > c;

void citire()
{
    int i,j;
    f >> n >> m;
    f.get();
    for(i=1; i<=n; i++){
             f.get(character,101);
        for(j=1; j<=m; j++){
            if(character[j-1]=='X')
                mat[i][j] = mat1[i][j] = -1;
            else if(character[j-1] =='R'){
                mat[i][j] = 1;
                x1 = i;
                y1 = j;
            }
            else if(character[j-1] =='J'){
                mat1[i][j] = 1;
                x2 = i;
                y2 = j;
            }
        }
        f.get();
    }
}

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

void lee()
{
    int nl,nc,nx,ny,i;
    while(!c.empty()){
        nl = c.front().first;
        nc = c.front().second;
        c.pop();
            for(i=0; i<8; i++){
                nx = nl + dx[i];
                ny = nc + dy[i];
                if(mat[nx][ny] == 0){
                    mat[nx][ny] = mat[nl][nc] + 1;
                    c.push(make_pair(nx,ny));
                }
            }
    }
}

void lee1()
{
    int nl,nc,nx,ny,i;
    while(!c.empty()){
        nl = c.front().first;
        nc = c.front().second;
        c.pop();
            for(i=0; i<8; i++){
                nx = nl + dx[i];
                ny = nc + dy[i];
                if(mat1[nx][ny] == 0){
                    mat1[nx][ny] = mat1[nl][nc] + 1;
                    c.push(make_pair(nx,ny));
                }
            }
    }
}

void afisare()
{
    int i,j;
    for(i=1; i<=n; i++)
        for(j=1; j<=m; j++){
            if(mat[i][j] == mat1[i][j] && mat[i][j] >0)
                if(mat[i][j] < Min){
                    Min = mat[i][j];
                    x1=i;
                    y1=j;
                }
        }
    g << Min << " " << x1 << " " << y1;
}

int main()
{
    citire();
    bordare();
    c.push(make_pair(x1,y1));
    lee();
    c.push(make_pair(x2,y2));
    lee1();
    afisare();
    return 0;
}