Cod sursa(job #2472636)

Utilizator Alex18maiAlex Enache Alex18mai Data 12 octombrie 2019 17:24:46
Problema Barbar Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.62 kb
//ALEXANDRU MICLEA

#include <vector>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

#include <fstream>
ifstream cin("barbar.in"); ofstream cout("barbar.out");
//ifstream cin("input"); ofstream cout("output");

int r, c;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,1,-1 };
char tj[1005][1005];
int drag[1005][1005];
int tabel[1005][1005];
pair <int, int> I, O;
queue <pair<int, int>> D, paf;

void lee(int cap) {
	while (!paf.empty()) {
		pair <int, int> now = paf.front();
		paf.pop();

		//cout << "lee : " << now.first << " " << now.second << '\n';

		for (int i = 0; i <= 3; i++) {
			int x = now.first + dx[i];
			int y = now.second + dy[i];

			if (x < 1 || y < 1 || x > r || y > c) {
				continue;
			}

			if (drag[x][y] < cap) {
				continue;
			}

			if (tj[x][y] == '*') {
				continue;
			}

			if (tabel[x][y]) {
				continue;
			}

			tabel[x][y] = 1;
			paf.push({ x, y });

		}
	}
}

void rezolvare() {
	int st = 1;
	int dr = drag[I.first][I.second];

	int ans = 0;
	while (st <= dr) {
		//cout << "rezolvare : " << st << " " << dr << '\n';
		int mid = (st + dr) / 2;
		tabel[I.first][I.second] = 1;
		paf.push(I);
		lee(mid);
		if (tabel[O.first][O.second]) {
			st = mid + 1;
			ans = mid;
		}
		else {
			dr = mid - 1;
		}
		for (int i = 1; i <= r; i++) {
			for (int j = 1; j <= c; j++) {
				tabel[i][j] = 0;
			}
		}
	}


	cout << ans;
}

void dragon() {
	while (!(D.empty())) {
		
		pair <int, int> now = D.front();
		D.pop();
		//cout << "dragon : " << now.first << " " << now.second <<" "<<drag[now.first][now.second] << '\n';

		for (int i = 0; i <= 3; i++) {
			int x = now.first + dx[i];
			int y = now.second + dy[i];

			if (x < 1 || y < 1 || x > r || y > c) {
				continue;
			}

			if (tj[x][y] == '*') {
				continue;
			}

			if (drag[x][y] <= drag[now.first][now.second] + 1) {
				continue;
			}

			drag[x][y] = drag[now.first][now.second] + 1;
			D.push({ x, y });
		}
	}

}

int main() {

	cin >> r >> c;

	for (int i = 1; i <= r; i++) {
		for (int j = 1; j <= c; j++) {
			drag[i][j] = 1e9;
			cin >> tj[i][j];
			if (tj[i][j] == 'I') {
				I = { i, j };
			}
			if (tj[i][j] == 'O') {
				O = { i, j };
			}
			if (tj[i][j] == 'D') {
				D.push({ i,j });
				drag[i][j] = 0;
			}
		}
	}

	dragon();
	rezolvare();

	return 0;
}