Cod sursa(job #954005)

Utilizator varga13VarGaz13 varga13 Data 27 mai 2013 22:59:36
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.7 kb
#include <fstream>
#include <string.h>
#include <istream>
#include <utility>
#include <deque>
#include <algorithm>
#include <vector>
using namespace std;
string aux;
ifstream f("rj.in");
ofstream g("rj.out");
int m,n,mat[101][101];
int linmin,colmin;
deque<pair<int,int> > drum;
deque<int> drumb;
int matb[101][101];
int ii[]={-1,-1,-1,0,1,1,1,0};
int jj[]={-1,0,1,1,1,0,-1,-1};

struct rezultat{int tmin, col, lin;};
vector<rezultat> rez;
void Lee(){
    bool ok=true,ook=true;
    while(!drum.empty()&&ok)
    {
        for(int i=0;i<8&&ook;i++)
        {
            int tempi=drum.back().first+ii[i];
            int tempj=drum.back().second+jj[i];
            if(tempi>=0&&tempj>=0&&tempi<n&&tempj<m)
            {
                if(mat[tempi][tempj]==0)
                {
                    drum.push_front(make_pair(tempi,tempj));
                    matb[tempi][tempj]=matb[drum.back().first][drum.back().second];
                    mat[tempi][tempj]=mat[drum.back().first][drum.back().second]+1;
                }
                 if(mat[tempi][tempj]==0)
                {
                    drum.push_front(make_pair(tempi,tempj));
                    matb[tempi][tempj]=matb[drum.back().first][drum.back().second];
                    mat[tempi][tempj]=mat[drum.back().first][drum.back().second]+1;
                }

                if(mat[tempi][tempj]==mat[drum.back().first][drum.back().second]-1&&matb[tempi][tempj]+matb[drum.back().first][drum.back().second]==0)
                {
                    /*g<<mat[tempi][tempj]<<' '<<tempi+1<<' '<<tempj+1;ok=ook=false;*/
                    rezultat auxi;
                    auxi.tmin=(mat[tempi][tempj]==mat[drum.back().first][drum.back().second]) ? mat[tempi][tempj]:mat[tempi][tempj]+1;
                    auxi.col=tempj+1;
                    auxi.lin=tempi+1;
                    rez.push_back(auxi);
                    ok=false;
                }
                if(mat[tempi][tempj]==mat[drum.back().first][drum.back().second]&&matb[tempi][tempj]+matb[drum.back().first][drum.back().second]==0)
                {
                    /*g<<mat[tempi][tempj]+1<<' '<<tempi+1<<' '<<tempj+1;ok=ook=false;*/
                }
                }
    }
    drum.pop_back();}
}
void Print()
{
    for(int i=0;i<n;i++)
        {for(int j=0;j<m;j++)
            g<<mat[i][j]<<'\t';g<<'\n';
            }
}

bool compC (rezultat i,rezultat j)
{
  return (int(i.col)<int(j.col));
}

bool compL (rezultat i,rezultat j)
{
  return (int(i.lin)<int(j.lin));
}

bool compT (rezultat i,rezultat j)
{
  return (int(i.tmin)<int(j.tmin));
}

void Print2()
{   for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
            g<<matb[i][j]<<'\t';g<<'\n';
    }
}

void Search()
{
stable_sort(rez.begin(),rez.end(),compT);
stable_sort(rez.begin(),rez.end(),compL);
stable_sort(rez.begin(),rez.end(),compC);
g<<rez.front().tmin<<' '<<rez.front().lin<<' '<<rez.front().col;

}





int main()
{
    f>>n>>m;
    getline(f,aux,'\n');
    for(int i=0;i<n;i++)
    {getline(f,aux,'\n');
    for(int j=0;j<m;j++)
     {
         if(aux[j]=='J')
            drum.push_front(make_pair(i,j)),matb[i][j]=-1;
               else if(aux[j]=='R')

                  drum.push_front(make_pair(i,j)),matb[i][j]=1;
                             else{
                                  matb[i][j]=0;
                            if(aux[j]==' ')
                                mat[i][j]=0;
                            if(aux[j]=='X')
                               mat[i][j]=1;
                                 }         }}
Lee();
Search();

g<<'\n';
Print();
g<<'\n';
Print2();

f.close();g.close();return 0; }