Advertisement
intsas

Untitled

Mar 25th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <algorithm>
  2.  
  3. namespace Game {
  4.     template<typename T>
  5.     class Coord {
  6.     public:
  7.         typedef T value_type;
  8.         T row;
  9.         T col;
  10.  
  11.         Coord(T in_row = T(), T in_col = T()) : row(in_row), col(in_col) {}
  12.     };
  13.  
  14.     template<typename T>
  15.     T dist(const Coord<T> &field, const Coord<T> &cell1, const Coord<T> &cell2) {
  16.         Coord<T> from(cell1), to(cell2);
  17.         if (from.col > to.col) {
  18.             std::swap(from.col, to.col);
  19.             std::swap(from.row, to.row);
  20.         }
  21.  
  22.         T answer = 0;
  23.         for (auto i = from.col; i <= to.col; ++i) {
  24.             if (from.row < to.row) {
  25.                 if (from.col % 2 == 0) {
  26.                     ++from.row;
  27.                 }
  28.             } else if (from.row > to.row) {
  29.                 if (from.col % 2 != 0) {
  30.                     --from.row;
  31.                 }
  32.             }
  33.             ++answer;
  34.         }
  35.         return answer + std::abs(from.row - to.row);
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement