Cod sursa(job #1651093)

Utilizator stantoniStan Antoniu stantoni Data 12 martie 2016 07:16:54
Problema Rj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

ifstream fin("date.in");

struct Element {
	int lr, lj, cr, cj;
	unsigned d;
} coada[400], x, y;

int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1}, dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int sf, inc, n, m, rx0, ry0, jx0, jy0, i, j, ok=1, timp;
char a[25][25];

int main() {
	fin >> n >> m;
	for (i=1; i<=n; i++) {
		for (j=1; j<=m; j++) {
			fin >> (a[i][j]);
			cout << a[i][j];
			if (a[i][j] == 'J') {
				rx0 = j;
				ry0 = i;
			}
			else
				if (a[i][j] == 'R') {
					jx0 = j;
					jy0 = i;
				}
		}
		cout << '\n';
	}
	cout << "\nROMEO: " << rx0 << ' ' << ry0 << '\n' << "JULIETA: " << jx0 << ' ' << jy0 << "\n\n";
	for (i=0; i<=n; i++)
		a[i][0] = a[i][m+1] = 'X';
	for (j=0; j<=m; j++)
		a[0][j] = a[n+1][j] = 'X';
	x.lr = rx0;
	x.cr = ry0;
	x.lj = jx0;
	x.cj = jy0;
	x.d = 1;
	coada[inc] = x;
	while (inc <= sf && ok == 1) {
		x = coada[inc++];
		for (i=0; i<8 && ok==1; i++) {
			for (j=0; j<8; j++) {
				y.lr = x.lr + dx[i];
				y.cr = x.cr + dy[i];
				y.lj = x.lj + dx[j];
				y.cj = x.cj + dy[j];
				y.d = x.d + 1;
				if (a[y.lr][y.cr] == 'Y' && a[y.lj][y.cj] == 'Y') 
					coada[++sf] = y;
				if (y.lr == y.lj && y.cr == y.cj) {
					timp = y.d;
					ok = 0;
				}
			}
		}
	}
	if (ok == 0)
		cout << timp;
	else
		cout << "EROARE!";
	return 0;
}