Language: C++

#include "algorithm"
#include "array"
#include "cmath"
#include "complex"
#include "cstdio"
#include "deque"
#include "initializer_list"
#include "map"
#include "numeric"
#include "queue"
#include "regex"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "vector"

using std::accumulate;
using std::all_of;
using std::any_of;
using std::array;
using std::binary_search;
using std::copy;
using std::count_if;
using std::deque;
using std::fill;
using std::find_if;
using std::for_each;
using std::inner_product;
using std::lower_bound;
using std::map;
using std::max;
using std::min;
using std::multimap;
using std::multiset;
using std::next_permutation;
using std::none_of;
using std::priority_queue;
using std::queue;
using std::regex;
using std::search;
using std::set;
using std::sort;
using std::sregex_token_iterator;
using std::stable_sort;
using std::stack;
using std::string;
using std::swap;
using std::to_string;
using std::transform;
using std::unordered_map;
using std::upper_bound;
using std::vector;

#define INF 1000000000 // 10^10
#define FOR(i, n) for ((i) = 0; (i) < (n); ++(i))
#define FOR_FROM_NOT_ZERO(i, s, n) for ((i) = (s); (i) < (n); ++(i))
#define FOR_dec(i, n) for ((i) = (n); (i) >= 0; --(i))
#define longlong ll
#define all(x) (x).begin(), (x).end()
#define d2_arr(height, width) array<array<int, (width)>, (height)>
#define d2_vec vector<vector<int>>
#define in_int(x) scanf(" %d/n", &(x));
#define in_str(x) scanf(" %[a-zA-Z 0-9_]/n", (x));
#define ch(x) printf("debuck %d", (x))

int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dx_ar[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy_ar[] = {1, 0, -1, 1, -1, 1, 0, -1};

template <typename type> void print_array_vertical(type d1_arr);
template <typename type> void print_array_horizon(type d1_arr);
template <typename type> void print_2darray(type d2_arr);
vector<vector<int>> d2_zeros(int height, int width);
vector<vector<int>> padding(vector<vector<int>> arr, int pad);
vector<int> str_int_split(char *s);
std::vector<std::vector<int>> in_d2_arr(int N, int size = 1000);
std::vector<int> in_d1_arr(int size = 1000);
double d2_distance(int x1, int y1, int x2, int y2);
int d2_manhattan(int x1, int y1, int x2, int y2);

int main() {
  int i, j, H, W, D, Q, pre, pro, dist;
  std::vector<int> v;
  std::vector<std::vector<int>> field;
  std::vector<std::vector<int>> LR;
  std::map<string, int> finished;
  std::map<int, std::vector<int>> field_map;
  v = in_d1_arr();
  H = v[0];
  W = v[1];
  D = v[2];
  field = in_d2_arr(H);
  FOR(i, H) {
    FOR(j, W) {
      v = {i, j};
      field_map[field[i][j]] = v;
    }
  }
  in_int(Q);
  LR = in_d2_arr(Q);
  FOR(i, Q) {
    dist = 0;
    pre = LR[i][0];
    pro = pre + D;
    if (finished.count(to_string(LR[i][0]) + to_string(LR[i][1])) == 0) {
      while (pre != LR[i][1]) {
        dist += d2_manhattan(field_map[pre][0], field_map[pre][1],
                             field_map[pro][0], field_map[pro][1]);
        pre = pro;
        pro = pre + D;
      }
      finished[to_string(LR[i][0]) + to_string(LR[i][1])] = dist;
      printf("%d\n", dist);
    } else {
      printf("%d\n", finished[to_string(LR[i][0]) + to_string(LR[i][1])]);
    }
  }
}

template <typename type> void print_array_vertical(type d1_arr) {
  int i;
  string s;
  FOR(i, d1_arr.size()) {
    printf("%d\n", d1_arr[i]);
    fflush(stdout);
  }
}

template <typename type> void print_array_horizon(type d1_arr) {
  int i;
  FOR(i, d1_arr.size() - 1) { printf("%d ", d1_arr[i]); }
  printf("%d\n", d1_arr.back());
  fflush(stdout);
}

template <typename type> void print_2darray(type d2_arr) {
  int i, j;
  FOR(i, d2_arr.size()) {
    FOR(j, d2_arr[i].size() - 1) { printf("%d ", d2_arr[i][j]); }
    printf("%d\n", d2_arr[i].back());
    fflush(stdout);
  }
}

vector<vector<int>> d2_zeros(int height, int width) {
  vector<vector<int>> temp;
  temp.resize(height);
  int i, j;
  FOR(i, height) {
    temp[i].resize(width);
    FOR(j, width) { temp[i][j] = 0; }
  }
  return temp;
}

vector<vector<int>> padding(vector<vector<int>> arr, int pad) {
  vector<vector<int>> arr_c = arr;
  int height = arr_c.size();
  int width = arr_c[0].size();
  std::vector<int> v;
  v.resize(width);
  fill(v.begin(), v.end(), pad);
  arr_c.insert(arr_c.begin(), v);
  arr_c.push_back(v);
  int i;
  FOR(i, height + 2) {
    arr_c[i].insert(arr_c[i].begin(), pad);
    arr_c[i].resize(width + 2);
  }
  arr_c.shrink_to_fit();
  return arr_c;
}

vector<int> str_int_split(char *s) {
  char *tok = strtok(s, " ");
  std::vector<int> v;
  while (tok != NULL) {
    v.push_back(atoi(tok));
    tok = strtok(NULL, " "); /* 2回目以降 */
  }
  return v;
}

std::vector<std::vector<int>> in_d2_arr(int N, int size) {
  int i;
  std::vector<std::vector<int>> v;
  FOR(i, N) {
    char *lines;
    lines = new char[size];
    in_str(lines);
    v.push_back(str_int_split(lines));
    delete[] lines;
  }
  return v;
}

std::vector<int> in_d1_arr(int size) {
  std::vector<int> v;
  char *lines;
  lines = new char[size];
  in_str(lines);
  v = str_int_split(lines);
  delete[] lines;
  return v;
}

double d2_distance(int x1, int y1, int x2, int y2) {
  return sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
}

int d2_manhattan(int x1, int y1, int x2, int y2) {
  return (abs(x2 - x1) + abs(y2 - y1));
}


Compiler output:
a.cc: In function 'std::vector<int> str_int_split(char*)':
a.cc:177:15: error: 'strtok' was not declared in this scope; did you mean 'strtoq'?
  177 |   char *tok = strtok(s, " ");
      |               ^~~~~~
      |               strtoq