Guest User

Untitled

a guest
Dec 11th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     vector<string> v;
  6.     string s;
  7.     while (getline(cin, s))
  8.         v.push_back(s);
  9.    
  10.     int n = v.size(), m = v[0].size();
  11.     int x = -1, y = -1;
  12.  
  13.     // Find initial position of '^'
  14.     for (int i = 0; i < n; ++i) {
  15.         for (int j = 0; j < m; ++j) {
  16.             if (v[i][j] == '^') {
  17.                 x = i;
  18.                 y = j;
  19.                 break;
  20.             }
  21.         }
  22.     }
  23.  
  24.     // Direction vectors
  25.     string dir = "^>v<";
  26.     vector<int> dirx = {-1, 0, 1, 0};
  27.     vector<int> diry = {0, 1, 0, -1};
  28.  
  29.     auto is_valid = [&](int nx, int ny) {
  30.         return nx >= 0 && nx < n && ny >= 0 && ny < m && v[nx][ny] != '#';
  31.     };
  32.  
  33.     int count = 0;
  34.     while (true) {
  35.         bool moved = false;
  36.         for (int k = 0; k < 4; ++k) {
  37.             if (v[x][y] == dir[k]) {
  38.                 int nx = x + dirx[k], ny = y + diry[k];
  39.                 int nx2 = x + dirx[(k + 1) % 4] , ny2 = y + diry[(k + 1) % 4];
  40.                 int nx3 = x + dirx[(k + 2) % 4] , ny3 = y + diry[(k + 2) % 4];
  41.                
  42.                 if (is_valid(nx, ny)) {
  43.                     v[x][y] = 'X'; // Mark current position as visited
  44.                     x = nx;
  45.                     y = ny;
  46.                     v[x][y] = dir[k];
  47.                     moved = true;
  48.                     break;
  49.                 }
  50.                
  51.                 else if(nx == -1 or nx == n or ny == -1 or ny == m) {
  52.                     v[x][y] = 'X';
  53.                     break;
  54.                 }
  55.                
  56.                 else if(is_valid(nx2 , ny2)) {
  57.                     v[x][y] = 'X';
  58.                     x = nx2;
  59.                     y = ny2;
  60.                     v[x][y] =dir[(k + 1) % 4];
  61.                     moved = true;
  62.                     break;
  63.                 }
  64.                
  65.                 else if(nx2 == -1 or nx2 == n or ny2 == -1 or ny2 == m) {
  66.                     v[x][y] = 'X';
  67.                     break;
  68.                 }
  69.                
  70.                
  71.                 else if(is_valid(nx3 , ny3)) {
  72.                     v[x][y] = 'X';
  73.                     x = nx3;
  74.                     y = ny3;
  75.                     v[x][y] = dir[(k + 2) % 4];
  76.                     moved = true;
  77.                     break;
  78.                 }
  79.                
  80.                 else if(nx3 == -1 or nx3 == n or ny3 == -1 or ny3 == m) {
  81.                     v[x][y] = 'X';
  82.                     break;
  83.                 }
  84.                
  85.                
  86.                 else break;
  87.             }
  88.         }
  89.         if (!moved) break; // If no movement is possible, stop
  90.     }
  91.  
  92.     // Count 'X' marks
  93.     for (auto &row : v) {
  94.         count += count_if(row.begin(), row.end(), [](char c) { return c == 'X'; });
  95.     }
  96.  
  97.     cout << count << "\n";
  98.     return 0;
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment