Pagini recente » Cod sursa (job #428040) | Cod sursa (job #17109) | Monitorul de evaluare | Cod sursa (job #2579376) | Cod sursa (job #2470133)
//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 m, n;
int dx[4] = { -1,1,0,0 };
int dy[4] = { 0,0,1,-1 };
int obst[1005][1005];
int dist[1005][1005];
int used[1005][1005];
char c;
pair <int, int> I, O;
queue <pair<int, int>> q;
void dragon() {
while (!q.empty()) {
pair<int, int> now = q.front();
q.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 > m || y > n) {
continue;
}
if (obst[x][y]) {
continue;
}
if (dist[x][y] <= dist[now.first][now.second] + 1) {
continue;
}
dist[x][y] = dist[now.first][now.second] + 1;
q.push({ x,y });
}
}
//DEBUG
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cout << dist[i][j] << " ";
}
cout << '\n';
}
}
void lee(int cap) {
while (!q.empty()) {
pair<int, int> now = q.front();
q.pop();
for (int i = 1; i <= 3; i++) {
int x = now.first + dx[i];
int y = now.second + dy[i];
if (x < 1 || y < 1 || x > m || y > n) {
continue;
}
if (obst[x][y]) {
continue;
}
if (used[x][y]) {
continue;
}
if (dist[x][y] < cap) {
continue;
}
used[x][y] = 1;
q.push({ x,y });
}
}
}
void rezolvare() {
int ans = 0;
int st = 1;
int dr = dist[I.first][I.second];
while (st <= dr) {
int mid = (st + dr) / 2;
used[I.first][I.second] = 1;
q.push(I);
lee(mid);
if(used[O.first][O.second]){
st = mid + 1;
ans = mid;
}
else {
dr = mid - 1;
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
used[i][j] = 0;
}
}
}
cout << ans;
}
int main() {
cin >> m >> n;
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
cin >> c;
dist[i][j] = 1e9;
if (c == '*') {
obst[m][n] = 1;
}
if (c == 'D') {
q.push({ m, n });
dist[i][j] = 0;
}
if (c == 'I') {
I = { m, n };
}
if (c == 'O') {
O = { m, n };
}
}
}
dragon();
rezolvare();
return 0;
}