Pagini recente » Cod sursa (job #3132058) | Cod sursa (job #1556510) | Cod sursa (job #996128) | Cod sursa (job #2511069) | Cod sursa (job #807965)
Cod sursa(job #807965)
#include <fstream>
#include <cstdio>
#include <queue>
#define NMax 105
using namespace std;
struct pozitie {
int x,y;
};
pozitie pr,pj;
queue <pozitie> q;
int matr[NMax][NMax],matj[NMax][NMax];
int n,m;
void citire() {
freopen("rj.in","r",stdin);
char crt;
scanf("%d %d",&n,&m);
getchar();
for (int x = 1;x<=m;x++) {
for (int y = 1;y <= n;y++) {
crt = getchar();
switch (crt) {
case 'R': {
matr[x][y] = 1;
pr.x = x;
pr.y = y;
break;
}
case 'J': {
matj[x][y] = 1;
pj.x = x;
pj.y = y;
break;
}
case 'X': {
matr[x][y] = matj[x][y] = -1;
break;
}
}
}
getchar();
}
}
void bordare() {
int x,y;
for (x=0;x<=m+1;x++) matr[x][0] = matj[x][0] = matr[x][n+1] = matj[x][n+1] = -1;
for (y=0;y<=n+1;y++) matr[0][y] = matj[0][y] = matr[m+1][y] = matj[m+1][y] = -1;
}
int dx[] = {0,-1,0,1,1,1,0,-1,-1};
int dy[] = {0,-1,-1,-1,0,1,1,1,0};
void lee(int x, int y,int mat[NMax][NMax]) {
pozitie pos;
for (int i = 1;i <= 8; i++) {
if (mat[x+dx[i]][y+dy[i]] == 0) {
mat[x+dx[i]][y+dy[i]] = mat[x][y] +1;
pos.x = x+dx[i];
pos.y = y+dy[i];
q.push(pos);
}
}
}
int main () {
pozitie pos;
citire();
bordare();
pos = pr;
q.push(pos);
while (!q.empty()) {
pos = q.front();
q.pop();
lee(pos.x,pos.y,matr);
}
pos = pj;
q.push(pos);
while (!q.empty()) {
pos = q.front();
q.pop();
lee(pos.x,pos.y,matj);
}
int tmin = 2000000000;
int x,y;
for (x=1;x<=m;x++)
for (y=1;y<=n;y++)
if (matj[x][y] == matr[x][y] && matr[x][y] > 0) {
if (matr[x][y] < tmin) {
tmin = matr[x][y];
pos.x = x;
pos.y = y;
}
}
ofstream out("rj.out");
out << tmin << " " << pos.x << " " << pos.y << "\n";
return 0;
}