Pagini recente » Cod sursa (job #1840313) | Cod sursa (job #1544529) | Cod sursa (job #2260863) | Cod sursa (job #257239) | Cod sursa (job #614919)
Cod sursa(job #614919)
#include <fstream>
#include <iomanip>
#include <string.h>
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int a[110][110],n,m,i,j,ci[1000000],cj[1000000],x1,x2,x3,x4,p=1,u,x,y,nr;
char c;
int dx[9]={0, -1, 0, 1, 0, -1, 1, 1, -1};
int dy[9]={0, 0, 1, 0, -1, 1, 1, -1, -1};
void citire()
{
f >> n >> m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
{
f.get(c);
if(c==10) j--;
if (c=='X') a[i][j]=-1;
if(c==' ') a[i][j]=0;
if(c=='R')
{
x1=i;x2=j;
a[i][j]=1;
}
if(c=='J')
{
x3=i;x4=j;
a[i][j]=-3;
}
}
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
{
a[i][0]=-1;
a[0][j]=-1;
a[n+1][j]=-1;
a[i][m+1]=-1;
}
}
void afisare()
{
for(i=0;i<=n+1;i++)
{
for(j=0;j<=m+1;j++)
g <<setw(2) << a[i][j] << " ";
g << "\n";
}
}
int coada(int d, int e)
{
for(i=1;i<=9;i++)
{
if(a[d+dx[i]][e+dy[i]]==-3) return -3;
if(a[d+dx[i]][e+dy[i]]==0)
{
a[d+dx[i]][e+dy[i]]=a[d][e]+1;
ci[++u]=d+dx[i];
cj[u]=e+dy[i];
}
}
return 0;
}
void lee()
{
int m=0;
while(p<=u)
{
m=coada(ci[p],cj[p]);
p++;
if(m==-3) return;
}
}
void afis()
{
for(i=1;i<=9;i++)
{
if(a[x+dx[i]][y+dy[i]]==nr)
{
x=x+dx[i];
y=y+dy[i];
ci[--p]=x;
cj[p]=y;
}
}
nr--;
}
void drum()
{
if((u-2)%2!=0)
g << (u-2)/2+1 << ' ' << ci[(u-2)/2+1] << ' ' << cj[(u-2)/2+1];
else
return;
}
int main()
{
citire();
ci[1]=x1;
cj[1]=x2;
u++;
lee();
//afisare();
//g << "\n";
nr=a[ci[p]][cj[p]];
//g << nr <<"\n";
x=x3;y=x4;
p=nr+1;
u=nr+1;
ci[p]=x3;
cj[p]=x4;
while(nr!=1)
{
afis();
}
/*for(i=1;i<=u;i++)
{
g << '(' << ci[i] << ',' << cj[i] << ')';
}
g <<"\n";*/
drum();
return 0;
}