Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- namespace Game {
- template<typename T>
- class Coord {
- public:
- typedef T value_type;
- T row;
- T col;
- Coord(T in_row = T(), T in_col = T()) : row(in_row), col(in_col) {}
- };
- template<typename T>
- T dist(const Coord<T> &field, const Coord<T> &cell1, const Coord<T> &cell2) {
- Coord<T> from(cell1), to(cell2);
- if (from.col > to.col) {
- std::swap(from.col, to.col);
- std::swap(from.row, to.row);
- }
- T answer = 0;
- for (auto i = from.col; i <= to.col; ++i) {
- if (from.row < to.row) {
- if (from.col % 2 == 0) {
- ++from.row;
- }
- } else if (from.row > to.row) {
- if (from.col % 2 != 0) {
- --from.row;
- }
- }
- ++answer;
- }
- return answer + std::abs(from.row - to.row);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement