Advertisement
anas_harby

Untitled

Jul 12th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int n, m, k, x, y;
  6. bool p[12][12][12], visited[12][12][12];
  7. struct node {
  8.     int k, x, y;
  9.     node(int a, int b, int c) {
  10.         k = a, x = b, y = c;
  11.     }
  12. };
  13.  
  14. int BFS() {
  15.     queue <node> q;
  16.     q.push(node(0, x, y));
  17.     visited[0][x][y] = true;
  18.     int res = 1;
  19.     while (!q.empty()) {
  20.         node top = q.front();
  21.         q.pop();
  22.         vector <node> adj;
  23.         if (top.k < k - 1)
  24.             adj.push_back(node(top.k + 1, top.x, top.y)); //down
  25.         if (top.k > 0)
  26.             adj.push_back(node(top.k - 1, top.x, top.y)); //up
  27.         if (top.x < n - 1)
  28.             adj.push_back(node(top.k, top.x + 1, top.y)); //right
  29.         if (top.x > 0)
  30.             adj.push_back(node(top.k, top.x - 1, top.y)); //left
  31.         if (top.y < m - 1)
  32.             adj.push_back(node(top.k, top.x, top.y + 1)); //forward
  33.         if (top.y > 0)
  34.             adj.push_back(node(top.k, top.y, top.y - 1)); //back
  35.  
  36.         for (int i = 0; i < adj.size(); i++) {
  37.             if(!visited[adj[i].k][adj[i].x][adj[i].y] && p[adj[i].k][adj[i].x][adj[i].y]) {
  38.                 q.push(node(adj[i].k, adj[i].x, adj[i].y));
  39.                 visited[adj[i].k][adj[i].x][adj[i].y] = true;
  40.                 res++;
  41.             }
  42.         }
  43.     }
  44.     return res;
  45. }
  46.  
  47. int main()
  48. {
  49.     cin >> k >> n >> m;
  50.     for (int c = 0; c < k; c++)
  51.         for (int i = 0; i < n; i++)
  52.             for (int j = 0; j < m; j++) {
  53.                 char x;
  54.                 cin >> x;
  55.                 if (x == '.')
  56.                     p[c][i][j] = true;
  57.             }
  58.     cin >> x >> y;
  59.     x--, y--;
  60.     cout << BFS();
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement