Pagini recente » Cod sursa (job #2620583) | Cod sursa (job #775528) | Cod sursa (job #2341652) | Cod sursa (job #2626001) | Cod sursa (job #808255)
Cod sursa(job #808255)
#include <fstream>
#include <queue>
using namespace std;
int n, m;
int cost[1001][1001];
int drum[1001][1001];
char mat[1002][1002];
queue <pair <short, short>
const int dx[4] = {0, 0, -1, 1};
const int dy[4] = {-1, 1, 0, 0};
inline bool ok(const int& x, const int& y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
int main(void) {
ifstream fin("pescari.in");
fin >> n >> m;
for(int i = 0; i < n ; ++i)
fin.getline(char[i], 1002);
fin.close();
for(int i = 0; i < n ; ++i)
for(int j = 0; j < m; ++j)
cost[i][j] = drum[i][j] = 9999999;
for(int i = 0; i < n; ++i)
for(int j = 0; j < m; ++j)
if(mat[i][j] == 'D') {
coada.push(make_pair(i, j));
cost[i][j] = 0;
}
while(!coada.empty()) {
int i = coada.front().first, j = coada.front().second;
coada.pop();
for(int k = 0; k < 4; ++k) {
int iv = i + dx[k], jv = j + dy[k];
if(ok(iv, jv) && mat[iv][jv] != '*' && mat[iv][jv] != 'D' && cost[iv][jv] > cost[i][j] + 1) {
cost[iv][jv] = cost[i][j] + 1;
coada.push(make_pair(iv, jv));
}
}
}
int intrare_x, iesire_x, intrare_y, iesire_y;
for(int i = 0;
coada.push(make_pair
while(!coada.empty()) {
int i = coada.front().first, j = coada.front().second;
coada.pop();
for(int k = 0; k < 4; ++k) {
int iv = i + dx[k], jv = j + dy[k];
if(ok(iv, jv) && mat[iv][jv] != '*' && mat[iv][jv] != 'D' && drum[iv][jv] > drum[i][j] + cost[iv][jv]) {
cost[iv][jv] = cost[i][j] + 1;
coada.push(make_pair(iv, jv));
}
}
}
}