Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <queue>
- using namespace std;
- class Node {
- public:
- int x, y, ln;
- Node();
- };
- Node::Node() {
- }
- bool vis[100][100];
- char mp[100][100];
- int main() {
- for (int i = 0; i < 100; i++) {
- for (int j = 0; j < 100; j++) {
- mp[i][j] = false;
- }
- }
- Node p;
- p.x = 0;
- p.y = 0;
- p.ln = 0;
- int y = 0;
- int x = 0;
- int e = 0;
- int tot = 0;
- cin >> y >> x >> e;
- for (int i = 0; i < y; i++) {
- string tmp = "";
- cin >> tmp;
- for (int j = 0; j < x; j++) {
- mp[i][j] = tmp[j];
- }
- }
- queue<Node> q;
- q.push(p);
- while (!q.empty() && vis[y][x] == false) {
- Node curr = q.front();
- q.pop();
- if (curr.x + 1 <= x && mp[curr.y][curr.x + 1] != '#' && vis[curr.y][curr.x + 1] == false) {
- Node next;
- next.x = curr.x + 1;
- next.y = curr.y;
- next.ln = curr.ln + 1;
- tot = curr.ln + 1;
- q.push(next);
- vis[curr.y][curr.x + 1] = true;
- }
- if (curr.y + 1 <= y && mp[curr.y + 1][curr.x] != '#' && vis[curr.y + 1][curr.x] == false) {
- Node next;
- next.x = curr.x;
- next.y = curr.y + 1;
- next.ln = curr.ln + 1;
- tot = curr.ln + 1;
- q.push(next);
- vis[curr.y + 1][curr.x] = true;
- }
- if (curr.x - 1 >= 0 && mp[curr.y][curr.x - 1] != '#' && vis[curr.y][curr.x - 1] == false) {
- Node next;
- next.x = curr.x - 1;
- next.y = curr.y;
- next.ln = curr.ln + 1;
- tot = curr.ln + 1;
- q.push(next);
- vis[curr.y][curr.x - 1] = true;
- }
- if (curr.y - 1 >= 0 && mp[curr.y - 1][curr.x] != '#' && vis[curr.y - 1][curr.x] == false) {
- Node next;
- next.x = curr.x;
- next.y = curr.y - 1;
- next.ln = curr.ln + 1;
- tot = curr.ln + 1;
- q.push(next);
- vis[curr.y - 1][curr.x] = true;
- }
- }
- if (vis[y][x] == false) {
- cout << "nej";
- } else {
- cout << tot;
- }
- #ifdef _DEBUG
- system("pause");
- #endif // _DEBUG
- }
Advertisement
Add Comment
Please, Sign In to add comment