Skip to content

Instantly share code, notes, and snippets.

@mejibyte
Created October 3, 2011 17:23
Show Gist options
  • Save mejibyte/1259660 to your computer and use it in GitHub Desktop.
Save mejibyte/1259660 to your computer and use it in GitHub Desktop.
100 120 -20 -100 -160 160
10 8 5 5 5 4
10 8 5 5 10 5
10 8 5 5 10 10
4 2 1 1 1 3
4 2 2 1 2 3
4 2 2 1 4 1
4 2 2 1 6 1
4 2 1 1 2 1
4 2 0 1 4 3
4 2 0 1 8 5
1 2 1 1 2 1
1 2 1 1 1000 1
3 2 1 -5 -1 -7
0 0 0 0 0 0
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cassert>
#include <cstdio>
using namespace std;
int B, H;
int myFloor(int a, int b) {
if ((a < 0) ^ (b < 0)) { // negative
return -(abs(a)/abs(b)) - 1;
} else {
return abs(a)/abs(b);
}
}
int mod(int a, int b) {
return (a % b + b) % b;
}
int Kh(int x, int y) {
return myFloor(y, H);
}
int Ka(int x, int y) {
return myFloor(2*H*x - B*y, 2*H*B);
}
int Kv(int x, int y) {
return myFloor(2*H*x + B*y, 2*H*B);
}
bool isVertex(int x, int y) {
assert(B % 2 == 0 and H % 2 == 0);
if (mod(y, H) != 0) return false;
int q = y / H;
if (q % 2 == 0) return mod(x, B) == 0;
return mod(x, B) == (B/2);
}
int main() {
int x1, y1, x2, y2;
while (cin >> B >> H >> x1 >> y1 >> x2 >> y2) {
if (B == 0 and H == 0 and x1 == 0 and y1 == 0 and x2 == 0 and y2 == 0) break;
B *= 2;
H *= 2;
x1 *= 2;
y1 *= 2;
x2 *= 2;
y2 *= 2;
int ans = abs(Kh(x1, y1) - Kh(x2, y2)) + abs(Ka(x1, y1) - Ka(x2, y2)) + abs(Kv(x1, y1) - Kv(x2, y2));
ans++;
int dx = x2 - x1;
int dy = y2 - y1;
int g = __gcd(abs(dx), abs(dy));
dx /= g; dy /= g;
assert(!isVertex(x1, y1));
assert(!isVertex(x2, y2));
int x = x1, y = y1;
while (x != x2 or y != y2) {
ans -= 2 * isVertex(x, y);
x += dx;
y += dy;
}
cout << ans << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment