Pagini recente » Cod sursa (job #111418) | Cod sursa (job #2074611) | Cod sursa (job #3292574) | Cod sursa (job #2153238) | Cod sursa (job #951668)
Cod sursa(job #951668)
#include <fstream>
#include <string.h>
#include <istream>
#include <utility>
#include <deque>
using namespace std;
string aux;
ifstream f("rj.in");
ofstream g("rj.out");
int m,n,mat[101][101];
deque<pair<int,int> > drum;
deque<int> drumb;
int matb[101][101];
int ii[]={-1,0,1,0};
int jj[]={0,1,0,-1};
void Lee()
{
bool ok=true,ook=true;
while(!drum.empty()&&ok)
{
for(int i=0;i<4&&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]>mat[drum.back().first][drum.back().second]&&matb[tempi][tempj]+matb[drum.back().first][drum.back().second]==0)
{g<<mat[tempi][tempj]<<' '<<tempi+1<<' '<<tempj+1;ok=ook=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';
}
}
void Print2()
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
g<<matb[i][j]<<'\t';
g<<'\n';
}
}
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();
/*
g<<'\n';
Print();
g<<'\n';
Print2();*/
f.close();
g.close();
return 0;
}