Cod sursa(job #1657192)

Utilizator IceBlast13Cristurean Alexander IceBlast13 Data 20 martie 2016 11:38:09
Problema Rj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

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

const int dx[]={0, 1, 1, 1, 0, -1, -1, -1};
const int dy[]={1, 1, 0, -1, -1, -1, 0, 1};
int xr, yr, xj, yj;

void citire (int &n, int &m, int a[][101], int b[][101])
{
    char s[101];
    int j, i;
    f>>n>>m;
    f.get();
    for(i=1;i<=n;i++)
        {
            f.getline(s, 101,'\n');
            for(j=0;j<m;j++)
            {
                a[i][j+1]=0;
                b[i][j+1]=0;
                if(s[j]=='R')
                {
                    xr=i;
                    yr=j+1;
                }
                if(s[j]=='J')
                {
                    xj=i;
                    yj=j+1;
                }
                if(s[j]=='X')
                    a[i][j+1]=b[i][j+1]=1;

            }


        }

}
int bine(int ii, int jj, int n, int m)
{
    return (ii>=1 and ii<=n and jj>=1 and jj<=m);
}

void lee(int n, int m, int a[][101], int i, int j)
{
    int cx[10000], cy[10000], k, ii, jj;
    cx[0]=i;
    cy[0]=j;
    a[i][j]=1;
    int pi=0;
    int ps=0;
    while(pi<=ps)
    {
        for(k=0;k<8;k++)
        {
            ii=cx[pi]+dx[k];
            jj=cy[pi]+dy[k];
            if(bine(ii, jj, n, m)==1)
                if(a[ii][jj]==0)
                {
                    ps=ps+1;
                    cx[ps]=ii;
                    cy[ps]=jj;
                    a[ii][jj]=a[cx[pi]][cy[pi]]+1;
                }
        }
        pi=pi+1;
    }
}


int main()
{
    int n, m, a[101][101], b[101][101];
    int minim=100*100;
    int ir, jr;
    citire(n, m, a, b);

    lee(n, m, a, xr, yr);
     /*
     for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
            cout<<a[i][j]<<" ";
        cout<<endl;
    }
    */
    lee(n, m, b, xj, yj);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            if(a[i][j]==b[i][j] and a[i][j]!=0 and a[i][j]!=1 and a[i][j]<minim)
            {
                ir=i;
                jr=j;
                minim=a[i][j];
            }
    g<<minim<<" "<<ir<<" "<<jr;
    return 0;
}