Cod sursa(job #3150282)

Utilizator Laurentiu_BTarabic Laurentiu Gabriel Laurentiu_B Data 15 septembrie 2023 21:46:05
Problema Rj Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.62 kb
#include<iostream>
#include<fstream>
#include<queue>
using namespace std;
int di[10]={1,-1,0,0,1,-1,1,-1};
int dj[10]={0,0,1,-1,1,-1,-1,1};
queue<pair<int,int> > q;
int n,m;
char s;
int a[105][105];
int b[105][105];
int c[105][105];
int main()
{
    ifstream f("rj.in");
    ofstream g("rj.out");
  f>>n>>m;
  f.get(s);
  int ri,rj,ji,jj;
  for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
  {
    f.get(s);
//cout<<s;
    if(s=='R')
    {
        ri=i;
        rj=j;
    }
    if(s=='J')
    {
        ji=i;
        jj=j;
    }
    if(s=='X')
        a[i][j]=1;
  }
  f.get(s);
  //cout<<endl;
  }
  q.push({ri,rj});
  b[ri][rj]=1;
  while(!q.empty())
  {
      int x=q.front().first,y=q.front().second;
      q.pop();
    for(int i=0;i<8;i++)
    {
      if(x+di[i]<=n&&x+di[i]>=1&&y+dj[i]<=m&&y+dj[i]>=1&&a[x+di[i]][y+dj[i]]==0&&b[x+di[i]][y+dj[i]]==0)
      {
        b[x+di[i]][y+dj[i]]=b[x][y]+1;
        q.push({x+di[i],y+dj[i]});
      }
    }
  }
  q.push({ji,jj});
  c[ji][jj]=1;
  while(!q.empty())
  {
      int x=q.front().first,y=q.front().second;
      q.pop();
    for(int i=0;i<8;i++)
    {
      if(x+di[i]<=n&&x+di[i]>=1&&y+dj[i]<=m&&y+dj[i]>=1&&a[x+di[i]][y+dj[i]]==0&&c[x+di[i]][y+dj[i]]==0)
      {
        c[x+di[i]][y+dj[i]]=c[x][y]+1;
        q.push({x+di[i],y+dj[i]});
      }
    }
  }
  int mi=n*m+1;
  int zi,zj;
  for(int i=1;i<=n;i++){
    for(int j=1;j<=m;j++)
    {
        if(b[i][j]==c[i][j]&&mi>b[i][j]&&a[i][j]==0)
            {mi=b[i][j];
            zi=i;
            zj=j;
            }
    }

  }
    g<<zi<<" "<<zj<<" "<<mi;
}