Pagini recente » Cod sursa (job #1264740) | Cod sursa (job #2352331) | Cod sursa (job #2252914) | Mihnea Andreescu | Cod sursa (job #764652)
Cod sursa(job #764652)
#include <iostream>
#include <fstream>
#include <queue>
#define N 1000
#define x first
#define y second
using namespace std;
ifstream f("barbar.in");
ofstream g("barbar.out");
int G[N][N],m,n;
queue<pair<int,int> > q;
pair<int,int> start,finish,node;
int visited[N][N];
int pathFind(int k) {
bool found = false;
int min = m*n,d;
while(!q.empty()) q.pop();
q.push(start);
min = G[start.x][start.y];
d = ++visited[start.x][start.y];
while(!q.empty()) {
node = q.front();
q.pop();
if(0 <= node.x-1 && G[node.x-1][node.y] >= k && visited[node.x-1][node.y] != d) {
min = (min > G[node.x-1][node.y]) ? G[node.x-1][node.y] : min;
visited[node.x-1][node.y] = d;
q.push(make_pair(node.x-1,node.y));
} else
if(G[node.x-1][node.y] == -3) {
found = true;
break;
}
if(m > node.x+1 && G[node.x+1][node.y] >= k && visited[node.x+1][node.y] != d) {
min = (min > G[node.x+1][node.y]) ? G[node.x+1][node.y] : min;
visited[node.x+1][node.y] = d;
q.push(make_pair(node.x+1,node.y));
} else
if(G[node.x+1][node.y] == -3) {
found = true;
break;
}
if(0 <= node.y-1 && G[node.x][node.y-1] >= k && visited[node.x][node.y-1] != d) {
min = (min > G[node.x][node.y-1]) ? G[node.x][node.y-1] : min;
visited[node.x][node.y-1] = d;
q.push(make_pair(node.x,node.y-1));
} else
if(G[node.x][node.y-1] == -3) {
found = true;
break;
}
if(n > node.y+1 && G[node.x][node.y+1] >= k && visited[node.x][node.y+1] != d) {
min = (min > G[node.x][node.y+1]) ? G[node.x][node.y+1] : min;
visited[node.x][node.y+1] = d;
q.push(make_pair(node.x,node.y+1));
} else
if(G[node.x][node.y+1] == -3) {
found = true;
break;
}
}
return (found && k <= min) ? min : -1;
}
int main() {
char ch;
int i,j,a,b,mid,path;
f>>m>>n;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++) {
f>>ch;
if(ch == '.')
G[i][j] = -1;
if(ch == 'I') {
G[i][j] = -1;
start = make_pair(i,j);
}
if(ch == 'D') {
G[i][j] = 0;
q.push(make_pair(i,j));
}
if(ch == 'O') {
G[i][j] = -3;
finish = make_pair(i,j);
}
if(ch == '*')
G[i][j] = -4;
}
while(!q.empty()) {
node = q.front();
q.pop();
if(0 <= node.x-1 && (G[node.x-1][node.y] == -1 ||
G[node.x-1][node.y]+1 < G[node.x-1][node.y])) {
G[node.x-1][node.y] = G[node.x][node.y] + 1;
q.push(make_pair(node.x-1,node.y));
}
if(m > node.x+1 && (G[node.x+1][node.y] == -1 ||
G[node.x+1][node.y]+1 < G[node.x+1][node.y])) {
G[node.x+1][node.y] = G[node.x][node.y] + 1;
q.push(make_pair(node.x+1,node.y));
}
if(0 <= node.y-1 && (G[node.x][node.y-1] == -1 ||
G[node.x][node.y-1]+1 < G[node.x][node.y-1])) {
G[node.x][node.y-1] = G[node.x][node.y] + 1;
q.push(make_pair(node.x,node.y-1));
}
if(n > node.y+1 && (G[node.x][node.y+1] == -1 ||
G[node.x][node.y+1]+1 < G[node.x][node.y+1])) {
G[node.x][node.y+1] = G[node.x][node.y] + 1;
q.push(make_pair(node.x,node.y+1));
}
}
a = 0, b = m*n;
while(b > a+1) {
mid = (a+b)>>1;
path = pathFind(mid);
if(path >= 0)
a = mid;
else
b = mid;
}
if(pathFind(a) >= 0)
g<<a;
else
g<<-1;
return 0;
}