that_one_nigga_you_k

problema marciano

Aug 29th, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.29 KB | None | 0 0
  1. // Narciso Nunes de Queiroz Neto
  2.  
  3. #include <iostream>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. template <typename T>
  9. class Vector {
  10. private:
  11.     T* data;
  12.     size_t capacity;
  13.     size_t size;
  14.  
  15. public:
  16.     Vector() : data(nullptr), capacity(0), size(0) {}
  17.  
  18.     ~Vector() {
  19.         delete[] data;
  20.     }
  21.  
  22.     void resize(size_t new_capacity) {
  23.         T* new_data = new T[new_capacity];
  24.         for (size_t i = 0; i < size; ++i) {
  25.             new_data[i] = data[i];
  26.         }
  27.         delete[] data;
  28.         data = new_data;
  29.         capacity = new_capacity;
  30.     }
  31.  
  32.     void push_back(const T& value) {
  33.         if (size == capacity) {
  34.             resize(capacity == 0 ? 1 : 2 * capacity);
  35.         }
  36.         data[size++] = value;
  37.     }
  38.  
  39.     T& operator[](size_t index) {
  40.         return data[index];
  41.     }
  42.  
  43.     const T& operator[](size_t index) const {
  44.         return data[index];
  45.     }
  46.  
  47.     size_t getSize() const {
  48.         return size;
  49.     }
  50.  
  51.     void reverse() {
  52.         for (size_t i = 0; i < size / 2; ++i) {
  53.             T temp = data[i];
  54.             data[i] = data[size - i - 1];
  55.             data[size - i - 1] = temp;
  56.         }
  57.     }
  58.  
  59.     void sortDescending() {
  60.         for (size_t i = 0; i < size - 1; ++i) {
  61.             for (size_t j = i + 1; j < size; ++j) {
  62.                 if (data[i] < data[j]) {
  63.                     T temp = data[i];
  64.                     data[i] = data[j];
  65.                     data[j] = temp;
  66.                 }
  67.             }
  68.         }
  69.     }
  70. };
  71.  
  72. // Classe Queue (Fila)
  73. template <typename T>
  74. class Queue {
  75. private:
  76.     struct Node {
  77.         T data;
  78.         Node* next;
  79.         Node(const T& data) : data(data), next(nullptr) {}
  80.     };
  81.  
  82.     Node* front;
  83.     Node* back;
  84.     size_t size;
  85.  
  86. public:
  87.     Queue() : front(nullptr), back(nullptr), size(0) {}
  88.  
  89.     ~Queue() {
  90.         while (!isEmpty()) {
  91.             desenfileirar();
  92.         }
  93.     }
  94.  
  95.     void enfileirar(const T& value) {
  96.         Node* newNode = new Node(value);
  97.         if (isEmpty()) {
  98.             front = back = newNode;
  99.         } else {
  100.             back->next = newNode;
  101.             back = newNode;
  102.         }
  103.         ++size;
  104.     }
  105.  
  106.     void desenfileirar() {
  107.         if (!isEmpty()) {
  108.             Node* temp = front;
  109.             front = front->next;
  110.             delete temp;
  111.             --size;
  112.         }
  113.     }
  114.  
  115.     T& getFront() {
  116.         return front->data;
  117.     }
  118.  
  119.     const T& getFront() const {
  120.         return front->data;
  121.     }
  122.  
  123.     bool isEmpty() const {
  124.         return size == 0;
  125.     }
  126.  
  127.     size_t getSize() const {
  128.         return size;
  129.     }
  130. };
  131.  
  132. // Classe Stack (Pilha)
  133. template <typename T>
  134. class Stack {
  135. private:
  136.     struct Node {
  137.         T data;
  138.         Node* next;
  139.         Node(const T& data) : data(data), next(nullptr) {}
  140.     };
  141.  
  142.     Node* top;
  143.     size_t size;
  144.  
  145. public:
  146.     Stack() : top(nullptr), size(0) {}
  147.  
  148.     ~Stack() {
  149.         while (!isEmpty()) {
  150.             pop();
  151.         }
  152.     }
  153.  
  154.     void push(const T& value) {
  155.         Node* newNode = new Node(value);
  156.         newNode->next = top;
  157.         top = newNode;
  158.         ++size;
  159.     }
  160.  
  161.     void pop() {
  162.         if (!isEmpty()) {
  163.             Node* temp = top;
  164.             top = top->next;
  165.             delete temp;
  166.             --size;
  167.         }
  168.     }
  169.  
  170.     T& topElement() {
  171.         return top->data;
  172.     }
  173.  
  174.     const T& topElement() const {
  175.         return top->data;
  176.     }
  177.  
  178.     bool isEmpty() const {
  179.         return size == 0;
  180.     }
  181.  
  182.     size_t getSize() const {
  183.         return size;
  184.     }
  185. };
  186.  
  187. // Estrutura para representar um grafo usando lista de adjacência
  188. class Graph {
  189.     int V;
  190.     Vector<Vector<int>> adj;
  191.  
  192. public:
  193.     Graph(int V) : V(V) {
  194.         adj.resize(V);
  195.     }
  196.  
  197.     void addEdge(int v, int w) {
  198.         adj[v].push_back(w);
  199.     }
  200.  
  201.     void BFS(int start, int end) {
  202.         Vector<bool> visited;
  203.         visited.resize(V);
  204.  
  205.         Vector<int> predecessor;
  206.         predecessor.resize(V);
  207.  
  208.         for(int i = 0; i < V; i++){
  209.             visited[i] = false;
  210.             predecessor[i] = -1;
  211.         }
  212.  
  213.         Queue<int> fila;
  214.         visited[start] = true;
  215.         fila.enfileirar(start);
  216.  
  217.         while (!fila.isEmpty()) {
  218.             int current = fila.getFront();
  219.             fila.desenfileirar();
  220.  
  221.             if (current == end) {
  222.                 break;  // Parar se o destino for encontrado
  223.             }
  224.  
  225.             for (size_t i = 0; i < adj[current].getSize(); ++i) {
  226.                 int adjacent = adj[current][i];
  227.                 if (!visited[adjacent]) {
  228.                     visited[adjacent] = true;
  229.                     predecessor[adjacent] = current;  // Atualiza o predecessor
  230.                     fila.enfileirar(adjacent);
  231.                 }
  232.             }
  233.         }
  234.  
  235.         // Reconstruir o caminho do destino ao início
  236.         Vector<int> path;
  237.         if (visited[end]) {
  238.             for (int v = end; v != -1; v = predecessor[v]) {
  239.                 path.push_back(v);
  240.             }
  241.             path.reverse(); // Inverter o caminho para começar do início
  242.  
  243.             cout << "Caminho mais curto entre " << start << " e " << end << ": ";
  244.             for (size_t i = 0; i < path.getSize(); ++i) {
  245.                 cout << path[i];
  246.                 if (i < path.getSize() - 1) {
  247.                     cout << ",";
  248.                 }
  249.             }
  250.             cout << endl;
  251.         } else {
  252.             cout << "Não há caminho entre " << start << " e " << end << endl;
  253.         }
  254.     }
  255.  
  256.     Vector<int> DFS(int start) {
  257.         Vector<bool> visited;
  258.         visited.resize(V);
  259.  
  260.         for(int i = 0; i < V; i++){
  261.             visited[i] = false;
  262.         }
  263.  
  264.         Vector<int> order;
  265.         Stack<int> pilha;
  266.  
  267.         pilha.push(start);
  268.  
  269.         while (!pilha.isEmpty()) {
  270.             int current = pilha.topElement();
  271.             pilha.pop();
  272.  
  273.             if (!visited[current]) {
  274.                 visited[current] = true;
  275.                 order.push_back(current);
  276.  
  277.                 for (size_t i = 0; i < adj[current].getSize(); ++i) {
  278.                     int adjacent = adj[current][i];
  279.                     if (!visited[adjacent]) {
  280.                         pilha.push(adjacent);
  281.                     }
  282.                 }
  283.             }
  284.         }
  285.  
  286.         // Ordem do DFS já é a ordem de visita
  287.         // cout << "Ordem de visitação (DFS): ";
  288.         // for (size_t i = 0; i < order.getSize(); ++i) {
  289.         //     cout << order[i] << " ";
  290.         // }
  291.         // cout << endl;
  292.  
  293.         // Identificar vértices desconexos
  294.         Vector<int> disconnected;
  295.         for (int i = 0; i < V; ++i) {
  296.             if (!visited[i]) {
  297.                 disconnected.push_back(i);
  298.             }
  299.         }
  300.  
  301.         disconnected.sortDescending();
  302.  
  303.         // cout << "Vértices desconexos após DFS: ";
  304.         // if (disconnected.getSize() == 0) {
  305.         //     cout << "Nenhum";
  306.         // } else {
  307.         //     for (size_t i = 0; i < disconnected.getSize(); ++i) {
  308.         //         cout << disconnected[i] << " ";
  309.         //     }
  310.         // }
  311.         // cout << endl;
  312.  
  313.         for (size_t i = 0; i < disconnected.getSize(); ++i) {
  314.                 cout << disconnected[i] << " ";
  315.             }
  316.        
  317.         for (size_t i = 0; i < order.getSize(); ++i) {
  318.             cout << order[i] << " ";
  319.         }
  320.  
  321.         return disconnected;
  322.     }
  323. };
  324.  
  325. int main() {
  326.     int quant, arestas;
  327.     cin >> quant;
  328.     cin >> arestas;
  329.     Graph g(quant);
  330.     int escolha;
  331.  
  332.     cout<< "1 para DFS e 2 para BFS\n";
  333.     cin>>escolha;
  334.    
  335.     if(escolha == 1){
  336.         for (int i = 0; i < arestas; i++) {
  337.             int vertice, adjacencia;
  338.             cin >> vertice >> adjacencia;
  339.             g.addEdge(vertice, adjacencia);
  340.         }
  341.  
  342.         cout << "Busca em Profundidade (DFS) começando a partir do vértice 0:\n";
  343.         g.DFS(0);
  344.     }
  345.  
  346.     if(escolha == 2){
  347.         int começo, fim;
  348.         for (int i = 0; i < arestas+1; i++) {
  349.             int vertice, adjacencia;
  350.             cin >> vertice >> adjacencia;
  351.             g.addEdge(vertice, adjacencia);
  352.  
  353.             começo = vertice;
  354.             fim = adjacencia;
  355.         }
  356.  
  357.         cout << "A"<< começo<<" B: "<<fim;
  358.         g.BFS(começo, fim);  // Aqui você pode ajustar o destino
  359.     }
  360.    
  361.  
  362.     return 0;
  363. }
  364.  
Add Comment
Please, Sign In to add comment