Cod sursa(job #2232892)

Utilizator DandeacDan Deac Dandeac Data 21 august 2018 16:11:14
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <iostream>
#include <fstream>
#include <queue>
#define MAX_MAT 110
using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");

int rj[MAX_MAT][MAX_MAT], N, M, x1, y1, x2, y2;
int x,y,CX,CY;
queue <int> qx,qy;
const int dx[] = {1,-1,0,0,1,-1,-1,1}, dy[] = {0,0,1,-1,1,-1,1,-1};
bool le2 = false;
void lee(int p1, int p2, int u1, int u2){

    qx.push(p1);
    qy.push(p1);

    while(!qx.empty())
    {
        x=qx.front();
        y=qy.front();

        qx.pop();
        qy.pop();

        for(int i=0;i<8;i++)
        {
            CX = x + dx[i];
            CY = y + dy[i];

            if(CX>0 && CY>0  && CX<=N && CY<=N )
            {
                if(le2 && rj[CX][CY] >= rj[x][y] + 1  )
                {
                    qx.push(CX);
                    qy.push(CY);
                    rj[CX][CY] = rj[x][y] + 1;
                }
                else if(rj[CX][CY] == 0)
                {
                    qx.push(CX);
                    qy.push(CY);
                    rj[CX][CY] = rj[x][y] + 1;
                }

            }
        }
    }
    if(le2) g<<rj[x][y] <<' '<<x<<' '<<y;
}
char punct;

int main()
{

 f>>N>>M;
    f.get(punct);
    for(int i=1;i<=N;i++)
    {
         for(int j=1;j<=M;j++)
        {
            f.get(punct);
            if(punct == 'X')
                rj[i][j] = -1;
            else if(punct == 'R')
            {
                 rj[i][j] = 1;
                 x1=i;y1=j;

            }else if(punct == 'J')
            {
                rj[i][j] = 0;
                x2=i;y2=j;
            }
            else  rj[i][j] = 0;

        }if(punct == 'R' || punct == 'J' || punct == 'X')
        f.get(punct);
    }
    lee(x1,y1,x2,y2);le2=true;
    lee(x2,y2,x1,y1);

    return 0;
}