Advertisement
Josif_tepe

Untitled

May 4th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <cstring>
  5. using namespace std;
  6. int main() {
  7.     int h1, m1, h2, m2;
  8.     cin >> h1 >> m1;
  9.     cin >> h2 >> m2;
  10.    
  11.     int di[] = {-1, 1, 0, 0};
  12.     int dj[] = {0, 0, -1, 1};
  13.     bool visited[24][60];
  14.     memset(visited, false, sizeof visited);
  15.     visited[h1][m1] = true;
  16.     queue<int> q;
  17.     q.push(h1);
  18.     q.push(m1);
  19.     q.push(0);
  20.    
  21.     while(!q.empty()) {
  22.         int ch = q.front();
  23.         q.pop();
  24.         int cm = q.front();
  25.         q.pop();
  26.         int buttons_clicked = q.front();
  27.         q.pop();
  28.         if(ch == h2 and cm == m2) {
  29.             cout << buttons_clicked << endl;
  30.             return 0;
  31.         }
  32.         for(int k = 0; k < 4; k++) {
  33.             int th = ch + di[k];
  34.             int tm = cm + dj[k];
  35.            
  36.             if(tm == 60) {
  37.                 tm = 0;
  38.                 th++;
  39.             }
  40.             if(tm == -1) {
  41.                 tm = 59;
  42.                 th--;
  43.             }
  44.             if(th == 24) {
  45.                 th = 0;
  46.             }
  47.             if(th == -1) {
  48.                 th = 23;
  49.             }
  50.             if(!visited[th][tm]) {
  51.                 visited[th][tm] = true;
  52.                 q.push(th);
  53.                 q.push(tm);
  54.                 q.push(buttons_clicked + 1);
  55.             }
  56.         }
  57.     }
  58.    
  59.     return 0;
  60. }
  61.  
  62. // 00:00 - 23:59
  63. // 00:00 --> 00:59
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement