Pagini recente » Cod sursa (job #2738818) | Cod sursa (job #649386) | Cod sursa (job #1354246) | Cod sursa (job #1374942) | Cod sursa (job #2468570)
//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");
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;
stack <pair<int, int>> D, paf;
void lee(int cap) {
paf.push(I);
while (!paf.empty()) {
pair <int, int> now = paf.top();
paf.pop();
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];
drag[I.first][I.second] = 1;
int ans = 0;
while (st <= dr) {
int mid = (st + dr) / 2;
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.top();
D.pop();
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;
}