Cod sursa(job #1631439)

Utilizator ajeccAjechiloae Eugen ajecc Data 5 martie 2016 16:05:57
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.38 kb
#include <fstream>
#include <queue>
#include <string>
#include <cstring>
#define for0(i,n) for(int i=0; i<n; i++)
#define for1(i,n) for(int i=1; i<=n; i++)
#define pb push_back
#define mp make_pair
#define ALL(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A,x) memset(A, x, sizeof(A))
#define cpy(A,B) memcpy(A, B, sizeof(B))
#define g(s) getline(fin, s)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MAX=100005;
const ull MOD=1000000007;
ifstream fin("rj.in");
ofstream fout("rj.out");

struct punct{
    int x, y, sol;
}p, pnou, ro, ju;

int s=10000000, sx, sy;
int n, m, matro[103][103], matju[103][103];
void citire(), retusare(), lee(punct u, int mat[103][103]), solve();

int dx[]={-1, 0, 1, 0, -1, 1, 1, -1};
int dy[]={0, 1, 0, -1, 1, 1, -1, -1};

queue<punct> coada;

int main()
{
    citire();
    retusare();
    lee(ro, matro);
    lee(ju, matju);
    solve();

    return 0;
}


void citire()
{
    fin>>n>>m;
    string s; g(s);
    for1(i, n)
    {
        g(s);
        for0(j, s.size())
        {
            char t=s[j];
            if(t=='X')
                matro[i][j+1]=-1;
            else if(t=='R')
                ro.x=i, ro.y=j+1;
            else if(t=='J')
                ju.x=i, ju.y=j+1;
        }
    }
    fin.close();
}

void retusare()
{
    for0(i, m+2)
        matro[0][i]=matro[n+1][i]=-1;
    for0(i, n+2)
        matro[i][0]=matro[i][m+1]=-1;
    cpy(matju, matro);
    matro[ro.x][ro.y]=1;
    matju[ju.x][ju.y]=1;
}

void lee(punct u, int mat[103][103])
{
    coada.push(u);
    while(!coada.empty())
    {
        p=coada.front();
        coada.pop();
        for0(i, 8)
        {
            if(mat[p.x+dx[i]][p.y+dy[i]]==0)
            {
                pnou.x=p.x+dx[i]; pnou.y=p.y+dy[i];
                mat[p.x+dx[i]][p.y+dy[i]]=mat[p.x][p.y]+1;
                coada.push(pnou);
            }
        }
    }
}

void solve()
{
    int sol=105*105, solx=-1, soly=-1;

    for1(i, n)
        for1(j, m)
        {
            if(matro[i][j]==matju[i][j] && matro[i][j]!=-1 && matro[i][j]<sol && matro[i][j])
            {
                sol=matro[i][j];
                solx=i;
                soly=j;
            }
        }

    fout<<sol<<' '<<solx<<' '<<soly;


    fout.close();
}