Untitled

mail@pastecode.io avatar
unknown
c_cpp
3 years ago
1.9 kB
2
Indexable
Never
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 100005;
int main()
{
    int h1, m1;
    int h2, m2;
    cin >> h1 >> m1;
    cin >> h2 >> m2;
    
    queue<int> q;
    q.push(h1);
    q.push(m1);
    q.push(0);
    
    bool visited[25][65];
    for(int i = 0; i < 25; i++) {
        for(int j = 0; j < 65; j++) {
            visited[i][j] = false;
        }
    }
    visited[h1][m1] = true;
    while(!q.empty()) {
        int ch = q.front();
        q.pop();
        int cm = q.front();
        q.pop();
        int cs = q.front();
        q.pop();
        
        if(ch == h2 and cm == m2) {
            cout << cs << endl;
            break;
        }
        int sh, sm;
        sh = ch + 1;
        sm = cm;
        if(sh == 24) {
            sh = 0;
        }
        if(!visited[sh][sm]) {
            q.push(sh);
            q.push(sm);
            q.push(cs + 1);
            visited[sh][sm] = true;
        }
        
        sh = ch - 1;
        sm = cm;
        if(sh == -1) {
            sh = 23;
        }
        if(!visited[sh][sm]) {
            q.push(sh);
            q.push(sm);
            q.push(cs + 1);
            visited[sh][sm] = true;
        }
        
        sh = ch;
        sm = cm + 1;
        if(sm == 60) {
            sm = 0;
            sh++;
            if(sh == 24) {
                sh = 0;
            }
        }
        if(!visited[sh][sm]) {
            q.push(sh);
            q.push(sm);
            q.push(cs + 1);
            visited[sh][sm] = true;
        }
        
        sh = ch;
        sm = cm - 1;
        if(sm == -1) {
            sm = 59;
            sh--;
            if(sh == -1) {
                sh = 23;
            }
        }
        if(!visited[sh][sm]) {
            q.push(sh);
            q.push(sm);
            q.push(cs + 1);
            visited[sh][sm] = true;
        }
    }

    return 0;
}