Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <queue>
- using namespace std;
- class node
- {
- public:
- bool visited = false;
- bool pass = false;
- vector<node*> adj;
- };
- int main()
- {
- int r,c,h;
- while (cin >> h >> r >> c && h != 0 && r != 0 && c != 0)
- {
- node nodes[32][32][32];
- vector<node*> q;
- int e[3];
- for (int i = 1; i <= h; i++) {
- for (int j = 1; j <= r; j++) {
- string line;
- cin >> line;
- for (int k = 1; k <= c; k++) {
- char ch = line.at(k - 1);
- if (ch != '#') {
- if (ch == '.') {
- nodes[i][j][k].pass = true;
- } else if (ch == 'S') {
- q.push_back(&nodes[i][j][k]);
- nodes[i][j][k].visited = true;
- nodes[i][j][k].pass = true;
- } else if (ch == 'E') {
- nodes[i][j][k].pass = true;
- e[0] = i;
- e[1] = j;
- e[2] = k;
- } else {
- cerr << "NOPE" << endl;
- }
- if (nodes[i - 1][j][k].pass) {
- nodes[i - 1][j][k].adj.push_back(&nodes[i][j][k]);
- nodes[i][j][k].adj.push_back(&nodes[i - 1][j][k]);
- }
- if (nodes[i][j - 1][k].pass) {
- nodes[i][j - 1][k].adj.push_back(&nodes[i][j][k]);
- nodes[i][j][k].adj.push_back(&nodes[i][j - 1][k]);
- }
- if (nodes[i][j][k - 1].pass) {
- nodes[i][j][k - 1].adj.push_back(&nodes[i][j][k]);
- nodes[i][j][k].adj.push_back(&nodes[i][j][k - 1]);
- }
- }
- }
- }
- }
- int step = 0;
- while (q.size() != 0 && !nodes[e[0]][e[1]][e[2]].visited) {
- vector<node*> newq;
- for (int i = 0; i < q.size(); i++) {
- node* u = q[i];
- for (int j = 0; j < u->adj.size(); j++) {
- node* v = u->adj[j];
- if (!v->visited) {
- newq.push_back(v);
- v->visited = true;
- }
- }
- }
- q = newq;
- step++;
- }
- if (nodes[e[0]][e[1]][e[2]].visited) {
- cout << "Escaped in " << step << " minute(s)." << endl;
- } else {
- cout << "Trapped!" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement