Cod sursa(job #2234395)

Utilizator DandeacDan Deac Dandeac Data 25 august 2018 17:05:05
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.49 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#include <string>
#define MAX_MAT 100
using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");

vector<string> rj;
short dist[MAX_MAT][MAX_MAT];

int N, M, x1, y1, x2, y2;
int x,y,CX,CY;
queue <int> qx,qy;
const int dx[] = {-1, -1, -1,  0, 0,  1, 1, 1};
const int dy[] = {-1,  0,  1, -1, 1, -1, 0, 1};
bool le2 = false;

bool onmap(int x, int y)
{
    return (0 <= x && x < N &&
            0 <= y && y < M);
}

void lee(int p1, int p2){

    qx.push(p1);
    qy.push(p2);
    dist[p1][p2] = 1;

    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 (!onmap(CX, CY)) continue;

            if (le2)
            {
                if (rj[CX][CY] != 'X' && dist[CX][CY] == dist[x][y] + 1)
                {
                    g<<dist[CX][CY] <<' '<<CX+1<<' '<<CY+1;
                    return;
                }
                if(rj[CX][CY] != 'X' && dist[CX][CY] >= dist[x][y] + 1)
                {
                    qx.push(CX);
                    qy.push(CY);
                    dist[CX][CY] = dist[x][y] + 1;
                }
            }
            else // lee 1
            {
                if(rj[CX][CY] != 'X' && dist[CX][CY] == 0)
                {
                    qx.push(CX);
                    qy.push(CY);
                    dist[CX][CY] = dist[x][y] + 1;
                }
            }

        }
    }
}
char punct;

int main()
{
    f>>N>>M;
    rj.resize(N);

    string trash;
    getline(f, trash);

    for(int i=0;i<N;i++)
    {
        getline(f, rj[i]);
    }

    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            if (rj[i][j] == 'R')
            {
                x1 = i;
                y1 = j;
            }
            if (rj[i][j] == 'J')
            {
                x2 = i;
                y2 = j;
            }
            cout << rj[i][j];
        }
        cout << '\n';
    }
    cout << x1 << ' ' << y1 << '\n' << x2 << ' ' << y2 << endl << endl;
    lee(x1,y1);le2=true;
    lee(x2,y2);
    cout << endl << endl;
    for (int i = 0; i < N; ++i)
    {
        for (int j = 0; j < M; ++j)
        {
            cout << dist[i][j];
        }
        cout << '\n';
    }

    return 0;
}