Pagini recente » Cod sursa (job #2669305) | Cod sursa (job #1387491) | Cod sursa (job #236251) | Cod sursa (job #2678196) | Cod sursa (job #1386358)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct Point {
int x,y;
};
ostream & operator<<(ostream &os, Point p) {
os << '{' << p.x << ", " << p.y << '}';
return os;
}
unsigned n,m;
vector<vector<unsigned>> map;
template<typename T>
void print_vector(ostream &os, vector<T> vec) {
for(const T& val:vec) {
os << val << ' ';
}
os << '\n';
}
const short dx[] = {-1, 1, 0, 0};
const short dy[] = { 0, 0,-1, 1};
vector<vector<unsigned>> process_dragons(vector<Point> dragons) {
vector<vector<bool>> visited(n,vector<bool>(m,false));
vector<vector<unsigned>> distance_to_nearest_dragon(n,vector<unsigned>(m,1<<31));
queue<Point> cells_to_process;
for(Point dragon:dragons) {
distance_to_nearest_dragon[dragon.x][dragon.y] = 0;
cells_to_process.push(dragon);
visited[dragon.x][dragon.y] = true;
}
while(!cells_to_process.empty()) {
Point current_cell = cells_to_process.front();
for(unsigned k = 0; k < 4; ++k) {
Point next_cell = current_cell;
next_cell.x += dx[k];
next_cell.y += dy[k];
if( 0 <= next_cell.x && next_cell.x < n &&
0 <= next_cell.y && next_cell.y < m ) {
if(!visited[next_cell.x][next_cell.y] && map[next_cell.x][next_cell.y] != -1) {
visited[next_cell.x][next_cell.y] = true;
distance_to_nearest_dragon[next_cell.x][next_cell.y] = distance_to_nearest_dragon[current_cell.x][current_cell.y]+1;
cells_to_process.push(next_cell);
}
}
}
cells_to_process.pop();
}
return distance_to_nearest_dragon;
}
int main() {
ifstream in("barbar.in");
in >> n >> m;
vector<Point> dragons;
map.resize(n,vector<unsigned>(m,0));
Point entry, exit;
in.ignore();
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
char cell;
in >> cell;
switch(cell) {
case 'D': {
dragons.push_back({i,j});
} break;
case '*': {
map[i][j] = -1;
} break;
case 'I': {
entry = {i,j};
} break;
case 'O': {
exit = {i,j};
}
case '.': break;
default: throw "Corrupted cell!";
}
}
}
map.resize(n,vector<unsigned>(m,0));
vector<vector<unsigned>> distance_to_nearest_dragon = process_dragons(dragons);
ofstream out("barbar.out");
}