egogoboy

Цветной лабиринт

Jul 15th, 2022
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. using namespace std;
  6.    
  7. int search(int& v, vector<int>& ways, vector<vector<int>>& rooms, vector<vector<int>>& colors, int way) {
  8.  
  9.     if (way < ways.size()) {
  10.         for (int i = 0; i < rooms[v].size(); ++i) {
  11.  
  12.             if (colors[v][i] == ways[way]) {
  13.                 int temp_v = v;
  14.                 v = rooms[v][i];
  15.                 if (search(v, ways, rooms, colors, way + 1) != -1) {
  16.                     return v;
  17.                 }
  18.                 else {
  19.                     v = temp_v;
  20.                 }
  21.             }
  22.  
  23.         }
  24.     }
  25.     else {
  26.         return v;
  27.     }
  28.  
  29.     return -1;
  30.  
  31. }
  32.  
  33. int main() {
  34.  
  35.     std::ifstream fin("input.txt");
  36.     std::ofstream fout("output.txt");
  37.  
  38.     int n, m;
  39.     fin >> n >> m;
  40.  
  41.     vector<vector<int>> rooms(n+1);
  42.     vector<vector<int>> colors(n+1);
  43.  
  44.     for (int i = 0; i < m; ++i) {
  45.         int v, u, color;
  46.         fin >> v >> u >> color;
  47.         rooms[v].push_back(u);
  48.         colors[v].push_back(color);
  49.         rooms[u].push_back(v);
  50.         colors[u].push_back(color);
  51.     }
  52.  
  53.     int way;
  54.     fin >> way;
  55.     vector<int> ways(way);
  56.     for (int i = 0; i < way; ++i) {
  57.         fin >> ways[i];
  58.     }
  59.  
  60.     int v = 1;
  61.     if (search(v, ways, rooms, colors, 0) != -1) {
  62.         fout << v << endl;
  63.         return 0;
  64.     }
  65.  
  66.     fout << "INCORRECT" << endl;
  67.  
  68.     return 0;
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment