Cod sursa(job #825808)

Utilizator Stefex09Stefan Teodorescu Stefex09 Data 29 noiembrie 2012 17:25:15
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.97 kb
#include <cstdio>
#include <queue>

using namespace std;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1,  0, -1};

int A[200][200];
int B[200][200];

int main ()
{
	freopen ("rj.in", "r", stdin);
	freopen ("rj.out", "w", stdout);
	setvbuf (stdin, NULL, _IOFBF, 1024);
	
	int N, M;
	int i, j;
	int Rx, Ry;
	int Jx, Jy;
	int nowx, nowy;
	int vx, vy;
	char S[110];
	char c;
	
	queue < pair < int, int > > Q;
	pair < int, int > now;
	
	scanf ("%d %d\n", &N, &M);
	
	for (i = 1; i <= N; i ++){
		A[0][i] = A[N + 1][i] = -1;
		B[0][i] = B[N + 1][i] = -1;
	}
	
	for (i = 1; i <= M; i ++){
		A[i][0] = A[i][M + 1] = -1;
		B[i][0] = B[i][M + 1] = -1;
	}
		
	
	for (i = 1; i <= N; i ++){
		gets (S);
		
		for (j = 0; j < M; j ++){
			c = S[j];
			
			if (c == 'R'){
				A[i][j + 1] = 1;
				B[i][j + 1] = -1;
				Rx = i;
				Ry = j + 1;
			}
			
			if (c == 'J'){
				B[i][j + 1] = 1;
				A[i][j + 1] = -1;
				Jx = i;
				Jy = j + 1;
			}
			
			if (c == 'X')
				A[i][j + 1] = B[i][j + 1] = -1;
		}
	}
		
	Q.push (make_pair (Rx, Ry));
	
	while (!Q.empty ()){
		now = Q.front ();
		Q.pop ();
		nowx = now.first;
		nowy = now.second;
		
		for (i = 0; i < 4; i ++){
			vx = nowx + dx[i];
			vy = nowy + dy[i];
			
			if (A[vx][vy] == -1)
				continue;
			
			if ((!A[vx][vy]) || (A[vx][vy] > A[nowx][nowy] + 1)){
				A[vx][vy] = A[nowx][nowy] + 1;
				Q.push (make_pair (vx, vy));
			}
		}
	}
	
	Q.push (make_pair (Jx, Jy));
	
	while (!Q.empty ()){
		now = Q.front ();
		Q.pop ();
		nowx = now.first;
		nowy = now.second;
		
		for (i = 0; i < 4; i ++){
			vx = nowx + dx[i];
			vy = nowy + dy[i];
			
			if (B[vx][vy] == -1)
				continue;
			
			if ((!B[vx][vy]) || (B[vx][vy] > B[nowx][nowy] + 1)){
				B[vx][vy] = B[nowx][nowy] + 1;
				Q.push (make_pair (vx, vy));
			}
			
			if (A[vx][vy] == B[vx][vy]){
				printf ("%d %d %d", A[vx][vy] - 1, vx, vy);
				
				return 0;
			}
		}
	}
		
	return 0;
}