Pagini recente » Cod sursa (job #2737409) | Cod sursa (job #2182588) | Cod sursa (job #128726) | Cod sursa (job #2559415) | Cod sursa (job #2664209)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
ifstream f ("rj.in");
ofstream g ("rj.out");
int n, m, a[105][105], b[105][105], c[105][105];
deque <pair<int, int>> q1, q2;
int romeo_x, romeo_y, juliet_x, juliet_y;
int dl[]={-1, 0, 1, 0, -1, 1, -1, 1};
int dc[]={0, 1, 0, -1, -1, 1, 1, -1};
bool check(int x, int y)
{
if(x>=1 && y>=1 && x<=n && y<=m && a[x][y]==0)
return 1;
return 0;
}
int main()
{
f>>n>>m;
char x;
f.get(x);
for(int i=1; i<=n; i++)
{
int ok=0;
for(int j=1; j<=m; j++)
{
f.get(x);
b[i][j]=1000;
c[i][j]=1000;
if(x=='X')
{
a[i][j]=-1;
}
else if (x=='R')
{
romeo_x=i;
romeo_y=j;
a[i][j]=0;
b[i][j]=0;
c[i][j]=0;
}
else if (x=='J')
{
juliet_x=i;
juliet_y=j;
a[i][j]=0;
b[i][j]=0;
c[i][j]=0;
}
else if (x==' ')
{
a[i][j]=0;
}
else if (x=='\n')
{
while(j<=m)
{
ok=1;
a[i][j]=0;
j++;
}
}
}
if(ok==0) f.get(x);
}
q1.push_back(make_pair(romeo_x, romeo_y));
q2.push_back(make_pair(juliet_x, juliet_y));
pair <int, int> curr, next;
while(!q1.empty())
{
curr=q1.front();
q1.pop_front();
for(int i=0; i<8; i++)
{
next.first=curr.first+dl[i];
next.second=curr.second+dc[i];
if(check(next.first, next.second))
{
if(b[curr.first][curr.second]+1<b[next.first][next.second])
{
b[next.first][next.second]=b[curr.first][curr.second]+1;
q1.push_back(make_pair(next.first, next.second));
}
}
}
}
while(!q2.empty())
{
curr=q2.front();
q2.pop_front();
for(int i=0; i<8; i++)
{
next.first=curr.first+dl[i];
next.second=curr.second+dc[i];
if(check(next.first, next.second))
{
if(c[curr.first][curr.second]+1<c[next.first][next.second])
{
c[next.first][next.second]=c[curr.first][curr.second]+1;
if(b[next.first][next.second]==c[next.first][next.second])
{
g<<b[next.first][next.second]+1<<" "<<next.first<<" "<<next.second;
return 0;
}
q2.push_back(make_pair(next.first, next.second));
}
}
}
}
return 0;
}