Pagini recente » Cod sursa (job #1414199) | Cod sursa (job #1415621) | Cod sursa (job #2978872) | Cod sursa (job #526604) | Cod sursa (job #412616)
Cod sursa(job #412616)
#include <fstream>
#include <queue>
struct punct{ int ff; int ss; };
using namespace std;
ifstream f("rj.in");
ofstream g("rj.out");
int n, m, D[101][101]={0};
char A[101][101];
punct start;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};
void citire()
{
int gasit=0;
char *p, caut='R';
f>>n>>m;
f.get();
for(int x=0;x<n;x++) {
f.getline(A[x], 101);
if(gasit==0)
{
p=strchr(A[x],caut);
if(p)
{
start.ff=x;
start.ss=A[x]-p;
D[start.ff][start.ss]=1;
gasit=1;
}
}
}
}
int lee(){
int i;
queue<punct> Q;
punct now, now2;
Q.push(start);
while (Q.empty() == false)
{
now.ff = Q.front().ff;
now.ss = Q.front().ss;
Q.pop();
if ( A[now.ff][now.ss] == 'J' ) return D[now.ff][now.ss];
for (i = 0; i < 8; i++){
if(now.ff + dx[i]>=0&&now.ss + dy[i]>=0&&now.ff + dx[i]<n&&now.ss + dy[i]<m)
{
now2.ff = now.ff + dx[i];
now2.ss= now.ss + dy[i];
}
if ( A[now2.ff][now2.ss] == ' ' || A[now2.ff][now2.ss] == 'J')
if(D[now2.ff][now2.ss]==0)
{
D[now2.ff][now2.ss] = D[now.ff][now.ss] + 1;
Q.push(now2);
}
}
}
return -1;
}
int parc(int i, int j, int nr)
{
int ii, jj, reza=0, rezb=0;
for(int x=0;x<8;x++)
{
ii=i+dx[x];
jj=j+dy[x];
if(A[ii][jj]==nr-1) reza=1;
if(A[ii][jj]==nr+1) rezb=1;
}
if(reza&&rezb) return 1;
return 0;
}
int main()
{
int nr;
citire();
nr=lee();
//for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { if(A[i][j]==nr) if(parc(i,j,nr)) g<<nr<<" "<<i<<" "<<j; i=n+1;}
if(nr%2==0)nr++;
g<<nr/2;
f.close();
g.close();
return 0;
}