Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <list>
- #include <queue>
- using namespace std;
- typedef vector<char> FilaChar;
- typedef vector<FilaChar> MapaChar;
- typedef vector<bool> FilaBool;
- typedef vector<FilaBool> MapaBool;
- struct PosDist {
- int x, y, dist;
- PosDist(int _x, int _y, int _dist) : x(_x), y(_y), dist(_dist) {}
- };
- void print_queue(queue<PosDist> q)
- {
- while (not q.empty()) {
- PosDist p = q.front();
- cout << "(" << p.x << ", " << p.y << ")" << endl;
- q.pop();
- }
- }
- bool busca_tresor(MapaChar &map, int n, int m, int &dist, queue<PosDist> &q)
- {
- while (not q.empty()) {
- PosDist p = q.front();
- q.pop();
- if (map[p.x][p.y] == 't') {
- dist = p.dist;
- return true;
- } else if (map[p.x][p.y] != 'X') {
- map[p.x][p.y] = 'X';
- if (p.x > 0) {
- q.push(PosDist(p.x - 1, p.y, p.dist + 1));
- }
- if (p.x < n-1) {
- q.push(PosDist(p.x + 1, p.y, p.dist + 1));
- }
- if (p.y > 0) {
- q.push(PosDist(p.x, p.y - 1, p.dist + 1));
- }
- if (p.y < m-1) {
- q.push(PosDist(p.x, p.y + 1, p.dist + 1));
- }
- }
- }
- return false;
- }
- int main()
- {
- int n, m;
- cin >> n >> m;
- MapaChar map(n, FilaChar(m));
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> map[i][j];
- }
- }
- int f, c;
- cin >> f >> c;
- int dist = 0;
- queue<PosDist> q;
- q.push(PosDist(f-1, c-1, 0));
- //MapaBool enq(n, FilaBool(m, false));
- if (busca_tresor(map, n, m, dist, q)) {
- cout << "distancia minima: " << dist << endl;
- } else {
- cout << "no es pot arribar a cap tresor" << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement