Pagini recente » Cod sursa (job #2348798) | Cod sursa (job #2377671) | Cod sursa (job #249138) | Cod sursa (job #1656815) | Cod sursa (job #3233800)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin ("rj.in");
ofstream fout ("rj.out");
int dir_i[]={-1,-1,0,1,1,1,0,-1};
int dir_j[]={0,1,1,1,0,-1,-1,-1};
vector <vector<pair<int,int>> > plan,plan1;
vector<pair<int,int>> linie;
queue <pair<int,int>> q;
void lee(int i, int j)
{
int k;
pair<int,int> x,inlocuitor;
q.push({i,j});
plan[i][j].first=1;
while(!q.empty())
{
x=q.front();
q.pop();
if(plan[x.first][x.second].first == 0)
for(k=0;k<8;k++)
{
inlocuitor = plan[x.first+dir_i[k]][x.second+dir_j[k]];
if(inlocuitor.first==0 || inlocuitor.first> plan[x.first][x.second].second +1 )
{
inlocuitor.first = plan[x.first][x.second].second +1;
q.push({x.first+dir_i[k] , x.second+dir_j[k]});
}
}
else if(plan[x.first][x.second].second == 0)
{
for(k=0;k<8;k++)
{
inlocuitor = plan[x.first+dir_i[k]][x.second+dir_j[k]];
if(inlocuitor.second == 0 || inlocuitor.second > plan[x.first][x.second].first +1)
{
inlocuitor.second = plan[x.first][x.second].first +1;
q.push({x.first+dir_i[k] , x.second+dir_j[k]});
}
}
}
else
{
for(k=0;k<8;k++)
{
inlocuitor = plan[x.first+dir_i[k]][x.second+dir_j[k]];
if(inlocuitor.first==0 || inlocuitor.first> plan[x.first][x.second].second +1 )
{
inlocuitor.first = plan[x.first][x.second].second +1;
q.push({x.first+dir_i[k] , x.second+dir_j[k]});
}
if(inlocuitor.second == 0 || inlocuitor.second > plan[x.first][x.second].first +1)
{
inlocuitor.second = plan[x.first][x.second].first +1;
q.push({x.first+dir_i[k] , x.second+dir_j[k]});
}
}
}
}
}
int main()
{
int n,m,i,j,k,ry,rx,jx,jy;
char c;
fin>>n>>m;
fin.get(c);
for(i=0;i<m+2;i++)
linie.push_back({0,0});
for(i=0;i<n+2;i++)
{
plan.push_back(linie);
plan1.push_back(linie);
}
for(i=0;i<n+2;i++)
{
plan[i][0]=plan[i][m]=plan1[i][0]=plan1[i][m]={1,1};
}
for(i=0;i<m+2;i++)
{
plan[0][i]=plan[n][i]=plan1[0][i]=plan1[n][i]={1,1};
}
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
fin.get(c);
if(c=='J')
{
jx=j;
jy=i;
}
else if(c=='R')
{
rx=j;
ry=i;
}
else if(c=='X')
{
plan[i][j]={-1,-1};
plan1[i][j]={-1,-1};
}
}
fin.get(c);
}
lee(ry,rx);
swap(plan,plan1);
lee(jy,jx);
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
if(plan[i][j].first == plan1[i][j].first && plan[i][j].first>0)
{
fout<<plan[i][j].first<<" "<<i<<" "<<j<<'\n';
break;
}
else if(plan[i][j].second == plan1[i][j].second && plan[i][j].second>0)
{
fout<<plan[i][j].second<<" "<<i<<" "<<j<<'\n';
break;
}
}
}
return 0;
}