Advertisement
Th3NiKo

elo

May 25th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. #include<iostream>
  2. #include <list>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. class Node{
  7. public:
  8.     int value;
  9.     int id;
  10. };
  11.  
  12. class Graph
  13. {
  14.     int V;    // No. of vertices
  15.  
  16.  
  17.     // Pointer to an array containing
  18.     // adjacency lists
  19.     list<int> *adj;
  20.  
  21.     // A recursive function used by DFS
  22.     void DFSUtil(int v, bool visited[]);
  23. public:
  24.     int suma = 0;
  25.     Graph(int V);   // Constructor
  26.  
  27.     // function to add an edge to graph
  28.     void addEdge(int v, int w);
  29.  
  30.     // DFS traversal of the vertices
  31.     // reachable from v
  32.     void DFS(int v);
  33. };
  34.  
  35. Graph::Graph(int V)
  36. {
  37.     this->V = V;
  38.     adj = new list<int>[V];
  39. }
  40.  
  41. void Graph::addEdge(int v, int w)
  42. {
  43.     adj[v].push_back(w); // Add w to vā€™s list.
  44. }
  45.  
  46. void Graph::DFSUtil(int v, bool visited[])
  47. {
  48.     // Mark the current node as visited and
  49.     // print it
  50.     visited[v] = true;
  51.     cout << v << " ";
  52.  
  53.     // Recur for all the vertices adjacent
  54.     // to this vertex
  55.     list<int>::iterator i;
  56.     for (i = adj[v].begin(); i != adj[v].end(); ++i)
  57.         if (!visited[*i]){
  58.                 suma++;
  59.                 DFSUtil(*i, visited);
  60.         }
  61. }
  62.  
  63. // DFS traversal of the vertices reachable from v.
  64. // It uses recursive DFSUtil()
  65. void Graph::DFS(int v)
  66. {
  67.     // Mark all the vertices as not visited
  68.     bool *visited = new bool[V];
  69.     for (int i = 0; i < V; i++)
  70.         visited[i] = false;
  71.  
  72.     // Call the recursive helper function
  73.     // to print DFS traversal
  74.     DFSUtil(v, visited);
  75.     cout << "Suma: " << suma << endl;
  76. }
  77.  
  78.  
  79. int main(){
  80.  
  81.  
  82.     int ile;
  83.     cin >> ile;
  84.     for(int i = 0; i < ile; i++){
  85.         int width;
  86.         int height;
  87.         cin >> width >> height;
  88.         Node tab[width][height];
  89.         Graph graph(width * height);
  90.         int counter = 0;
  91.         for(int j = 0; j < height; j++){
  92.             for(int p = 0; p < width; p++){
  93.                 Node temporary;
  94.                 temporary.value = 0;
  95.                 temporary.id = counter;
  96.                 counter++;
  97.                 char temp;
  98.                 cin >> temp;
  99.                 if(temp == '#'){
  100.                     temporary.value = 1;
  101.  
  102.                 } else {
  103.                     temporary.value = 0;
  104.                 }
  105.                 tab[j][p] = temporary;
  106.             }
  107.         }
  108.          vector <int> freeNodes;
  109.          for(int j = 0; j < height; j++){
  110.             for(int p = 0; p < width; p++){
  111.                 if(tab[j][p].value==0){
  112.                     freeNodes.push_back(tab[j][p].id);
  113.                         if(tab[j+1][p].value == 0){
  114.                             graph.addEdge(tab[j][p].id, tab[j+1][p].id);
  115.                             //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j+1][p].id << endl;
  116.                         }
  117.  
  118.                         if(tab[j-1][p].value == 0){
  119.                             graph.addEdge(tab[j][p].id, tab[j-1][p].id);
  120.                             //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j-1][p].id << endl;
  121.  
  122.                         }
  123.  
  124.                          if(tab[j][p+1].value == 0){
  125.                             graph.addEdge(tab[j][p].id, tab[j][p+1].id);
  126.                             //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j][p+1].id << endl;
  127.  
  128.                         }
  129.  
  130.                          if(tab[j][p-1].value == 0){
  131.  
  132.                             //cout << "Polaczenie miedzy: " << tab[j][p].id << " oraz " << tab[j][p-1].id << endl;
  133.                             graph.addEdge(tab[j][p].id, tab[j][p-1].id);
  134.                         }
  135.                 }
  136.             }
  137.          }
  138.  
  139.          for(int c = 0; c < freeNodes.size();c++){
  140.                 graph.suma = 0;
  141.             graph.DFS(freeNodes.at(c));
  142.             cout << endl;
  143.          }
  144.  
  145.  
  146.  
  147.  
  148.     }
  149.  
  150.     return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement