Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <queue>
- using namespace std;
- int main()
- {
- int x, y;
- int ex, ey;
- cin >> x >> y;
- cin >> ex >> ey;
- if( x + y != ex + ey)
- {
- cout << -1 << endl;
- return 0;
- }
- int zbir = x + y;
- bool vis[200001];
- memset(vis, 0, sizeof(vis));
- queue <int> q, cekori;
- q.push(x);
- cekori.push(0);
- while(!q.empty())
- {
- int temeX = q.front();
- int cek = cekori.front();
- q.pop();
- cekori.pop();
- if( temeX == ex )
- {
- cout << cek << endl;
- break;
- }
- int temeY = zbir - temeX;
- if( temeY - 1 > 0 && vis[temeX + 1] == false)
- {
- vis[temeX + 1] = true;
- q.push(temeX + 1);
- cekori.push(cek + 1);
- }
- if (temeX-1-((2*temeX)%31) > 0 && vis[temeX-1-((2*temeX)%31)]==false)
- {
- vis[temeX-1-((2*temeX)%31)] = true;
- q.push(temeX-1-((2*temeX)%31));
- cekori.push(cek + 1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement